mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Makes index listener tests fail safe
This commit is contained in:
parent
cd2354d539
commit
1ee1c0cb84
4 changed files with 115 additions and 63 deletions
|
@ -25,7 +25,6 @@ import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||||
import org.eclipse.cdt.core.dom.ast.IBinding;
|
import org.eclipse.cdt.core.dom.ast.IBinding;
|
||||||
import org.eclipse.cdt.core.index.IIndex;
|
import org.eclipse.cdt.core.index.IIndex;
|
||||||
import org.eclipse.cdt.core.index.IIndexBinding;
|
import org.eclipse.cdt.core.index.IIndexBinding;
|
||||||
import org.eclipse.cdt.core.index.IIndexFile;
|
|
||||||
import org.eclipse.cdt.core.index.IIndexName;
|
import org.eclipse.cdt.core.index.IIndexName;
|
||||||
import org.eclipse.cdt.core.index.IndexFilter;
|
import org.eclipse.cdt.core.index.IndexFilter;
|
||||||
import org.eclipse.cdt.core.model.ICProject;
|
import org.eclipse.cdt.core.model.ICProject;
|
||||||
|
@ -81,28 +80,6 @@ public class IndexBugsTests extends BaseTestCase {
|
||||||
return TestSourceReader.createFile(container, new Path(fileName), contents);
|
return TestSourceReader.createFile(container, new Path(fileName), contents);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void waitForIndexer(IFile file, int maxmillis) throws Exception {
|
|
||||||
long endTime= System.currentTimeMillis() + maxmillis;
|
|
||||||
int timeLeft= maxmillis;
|
|
||||||
while (timeLeft >= 0) {
|
|
||||||
assertTrue(CCorePlugin.getIndexManager().joinIndexer(timeLeft, NPM));
|
|
||||||
fIndex.acquireReadLock();
|
|
||||||
try {
|
|
||||||
IIndexFile pfile= fIndex.getFile(file.getLocation());
|
|
||||||
if (pfile != null && pfile.getTimestamp() >= file.getLocalTimeStamp()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
fIndex.releaseReadLock();
|
|
||||||
}
|
|
||||||
|
|
||||||
Thread.sleep(50);
|
|
||||||
timeLeft= (int) (endTime-System.currentTimeMillis());
|
|
||||||
}
|
|
||||||
throw new Exception("Indexer did not complete in time!");
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Pattern[] getPattern(String qname) {
|
protected Pattern[] getPattern(String qname) {
|
||||||
String[] parts= qname.split("::");
|
String[] parts= qname.split("::");
|
||||||
Pattern[] result= new Pattern[parts.length];
|
Pattern[] result= new Pattern[parts.length];
|
||||||
|
@ -112,6 +89,10 @@ public class IndexBugsTests extends BaseTestCase {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void waitUntilFileIsIndexed(IFile file, int time) throws Exception {
|
||||||
|
TestSourceReader.waitUntilFileIsIndexed(fIndex, file, time);
|
||||||
|
}
|
||||||
|
|
||||||
// {bug162011}
|
// {bug162011}
|
||||||
// namespace ns162011 {
|
// namespace ns162011 {
|
||||||
// class Class162011 {
|
// class Class162011 {
|
||||||
|
@ -128,7 +109,7 @@ public class IndexBugsTests extends BaseTestCase {
|
||||||
int indexOfDecl = content.indexOf(funcName);
|
int indexOfDecl = content.indexOf(funcName);
|
||||||
int indexOfDef = content.indexOf(funcName, indexOfDecl+1);
|
int indexOfDef = content.indexOf(funcName, indexOfDecl+1);
|
||||||
IFile file= createFile(getProject(), fileName, content);
|
IFile file= createFile(getProject(), fileName, content);
|
||||||
waitForIndexer(file, 1000);
|
waitUntilFileIsIndexed(file, 1000);
|
||||||
|
|
||||||
// make sure the ast is correct
|
// make sure the ast is correct
|
||||||
ITranslationUnit tu= (ITranslationUnit) fCProject.findElement(new Path(fileName));
|
ITranslationUnit tu= (ITranslationUnit) fCProject.findElement(new Path(fileName));
|
||||||
|
|
|
@ -24,9 +24,9 @@ import org.eclipse.cdt.core.index.IIndexFile;
|
||||||
import org.eclipse.cdt.core.index.IndexFilter;
|
import org.eclipse.cdt.core.index.IndexFilter;
|
||||||
import org.eclipse.cdt.core.model.ICProject;
|
import org.eclipse.cdt.core.model.ICProject;
|
||||||
import org.eclipse.cdt.core.testplugin.CProjectHelper;
|
import org.eclipse.cdt.core.testplugin.CProjectHelper;
|
||||||
|
import org.eclipse.cdt.core.testplugin.util.TestSourceReader;
|
||||||
import org.eclipse.cdt.internal.core.CCoreInternals;
|
import org.eclipse.cdt.internal.core.CCoreInternals;
|
||||||
import org.eclipse.core.resources.IFile;
|
import org.eclipse.core.resources.IFile;
|
||||||
import org.eclipse.core.resources.IResource;
|
|
||||||
import org.eclipse.core.resources.IWorkspaceRunnable;
|
import org.eclipse.core.resources.IWorkspaceRunnable;
|
||||||
import org.eclipse.core.resources.ResourcesPlugin;
|
import org.eclipse.core.resources.ResourcesPlugin;
|
||||||
import org.eclipse.core.runtime.CoreException;
|
import org.eclipse.core.runtime.CoreException;
|
||||||
|
@ -111,14 +111,16 @@ public class IndexIncludeTest extends IndexTestBase {
|
||||||
|
|
||||||
private void checkContext() throws Exception {
|
private void checkContext() throws Exception {
|
||||||
final long timestamp= System.currentTimeMillis();
|
final long timestamp= System.currentTimeMillis();
|
||||||
final IResource file= fProject.getProject().findMember(new Path("included.h"));
|
final IFile file= (IFile) fProject.getProject().findMember(new Path("included.h"));
|
||||||
assertNotNull(file);
|
assertNotNull(file);
|
||||||
|
waitForIndexer();
|
||||||
|
|
||||||
ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
|
ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
|
||||||
public void run(IProgressMonitor monitor) throws CoreException {
|
public void run(IProgressMonitor monitor) throws CoreException {
|
||||||
((IFile) file).setContents(new ByteArrayInputStream( "int included; int CONTEXT;\n".getBytes()), false, false, NPM);
|
file.setContents(new ByteArrayInputStream( "int included; int CONTEXT;\n".getBytes()), false, false, NPM);
|
||||||
}
|
}
|
||||||
}, NPM);
|
}, NPM);
|
||||||
waitForIndexer();
|
TestSourceReader.waitUntilFileIsIndexed(fIndex, file, 1000);
|
||||||
fIndex.acquireReadLock();
|
fIndex.acquireReadLock();
|
||||||
try {
|
try {
|
||||||
IIndexFile ifile= fIndex.getFile(file.getLocation());
|
IIndexFile ifile= fIndex.getFile(file.getLocation());
|
||||||
|
|
|
@ -18,7 +18,6 @@ import junit.framework.Test;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.CCorePlugin;
|
import org.eclipse.cdt.core.CCorePlugin;
|
||||||
import org.eclipse.cdt.core.dom.IPDOMManager;
|
import org.eclipse.cdt.core.dom.IPDOMManager;
|
||||||
import org.eclipse.cdt.core.index.IIndex;
|
|
||||||
import org.eclipse.cdt.core.index.IIndexChangeEvent;
|
import org.eclipse.cdt.core.index.IIndexChangeEvent;
|
||||||
import org.eclipse.cdt.core.index.IIndexChangeListener;
|
import org.eclipse.cdt.core.index.IIndexChangeListener;
|
||||||
import org.eclipse.cdt.core.index.IIndexManager;
|
import org.eclipse.cdt.core.index.IIndexManager;
|
||||||
|
@ -28,10 +27,12 @@ import org.eclipse.cdt.core.model.ICProject;
|
||||||
import org.eclipse.cdt.core.testplugin.CProjectHelper;
|
import org.eclipse.cdt.core.testplugin.CProjectHelper;
|
||||||
import org.eclipse.cdt.core.testplugin.util.BaseTestCase;
|
import org.eclipse.cdt.core.testplugin.util.BaseTestCase;
|
||||||
import org.eclipse.cdt.core.testplugin.util.TestSourceReader;
|
import org.eclipse.cdt.core.testplugin.util.TestSourceReader;
|
||||||
|
import org.eclipse.core.resources.IFile;
|
||||||
|
import org.eclipse.core.runtime.IProgressMonitor;
|
||||||
import org.eclipse.core.runtime.NullProgressMonitor;
|
import org.eclipse.core.runtime.NullProgressMonitor;
|
||||||
|
|
||||||
public class IndexListenerTest extends BaseTestCase {
|
public class IndexListenerTest extends BaseTestCase {
|
||||||
protected IIndex fIndex;
|
private static final IProgressMonitor NPM = new NullProgressMonitor();
|
||||||
private ICProject fProject1;
|
private ICProject fProject1;
|
||||||
private ICProject fProject2;
|
private ICProject fProject2;
|
||||||
|
|
||||||
|
@ -51,11 +52,14 @@ public class IndexListenerTest extends BaseTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testIdleListener() throws Exception {
|
public void testIdleListener() throws Exception {
|
||||||
|
final Object mutex= new Object();
|
||||||
final int[] state= new int[] {0, 0, 0};
|
final int[] state= new int[] {0, 0, 0};
|
||||||
IIndexManager im= CCorePlugin.getIndexManager();
|
IIndexManager im= CCorePlugin.getIndexManager();
|
||||||
|
assertTrue(im.joinIndexer(10000, NPM));
|
||||||
|
|
||||||
im.addIndexerStateListener(new IIndexerStateListener() {
|
IIndexerStateListener listener = new IIndexerStateListener() {
|
||||||
public void indexChanged(IIndexerStateEvent event) {
|
public void indexChanged(IIndexerStateEvent event) {
|
||||||
|
synchronized (mutex) {
|
||||||
if (event.indexerIsIdle()) {
|
if (event.indexerIsIdle()) {
|
||||||
state[0]++;
|
state[0]++;
|
||||||
state[2]= 0;
|
state[2]= 0;
|
||||||
|
@ -64,41 +68,72 @@ public class IndexListenerTest extends BaseTestCase {
|
||||||
state[1]++;
|
state[1]++;
|
||||||
state[2]= 1;
|
state[2]= 1;
|
||||||
}
|
}
|
||||||
|
mutex.notify();
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
};
|
||||||
|
|
||||||
TestSourceReader.createFile(fProject1.getProject(), "test.cpp", "int a;");
|
im.addIndexerStateListener(listener);
|
||||||
Thread.sleep(200);
|
try {
|
||||||
assertTrue(im.joinIndexer(10000, new NullProgressMonitor()));
|
IFile file= TestSourceReader.createFile(fProject1.getProject(), "test.cpp", "int a;");
|
||||||
Thread.sleep(200);
|
synchronized (mutex) {
|
||||||
|
mutex.wait(1000);
|
||||||
|
if (state[0]+state[1] < 2) {
|
||||||
|
mutex.wait(1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
assertEquals(1, state[0]);
|
assertEquals(1, state[0]);
|
||||||
assertEquals(1, state[1]);
|
assertEquals(1, state[1]);
|
||||||
assertEquals(0, state[2]);
|
assertEquals(0, state[2]);
|
||||||
}
|
}
|
||||||
|
finally {
|
||||||
|
im.removeIndexerStateListener(listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void testChangeListener() throws Exception {
|
public void testChangeListener() throws Exception {
|
||||||
|
final Object mutex= new Object();
|
||||||
final List projects= new ArrayList();
|
final List projects= new ArrayList();
|
||||||
IIndexManager im= CCorePlugin.getIndexManager();
|
IIndexManager im= CCorePlugin.getIndexManager();
|
||||||
|
|
||||||
im.addIndexChangeListener(new IIndexChangeListener() {
|
assertTrue(im.joinIndexer(10000, NPM));
|
||||||
|
IIndexChangeListener listener = new IIndexChangeListener() {
|
||||||
public void indexChanged(IIndexChangeEvent event) {
|
public void indexChanged(IIndexChangeEvent event) {
|
||||||
|
synchronized (mutex) {
|
||||||
projects.add(event.getAffectedProject());
|
projects.add(event.getAffectedProject());
|
||||||
|
mutex.notify();
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
};
|
||||||
|
|
||||||
TestSourceReader.createFile(fProject1.getProject(), "test.cpp", "int a;");
|
im.addIndexChangeListener(listener);
|
||||||
Thread.sleep(200);
|
try {
|
||||||
|
IFile file= TestSourceReader.createFile(fProject1.getProject(), "test.cpp", "int a;");
|
||||||
|
|
||||||
|
synchronized (mutex) {
|
||||||
|
mutex.wait(1000);
|
||||||
|
}
|
||||||
assertEquals(1, projects.size());
|
assertEquals(1, projects.size());
|
||||||
assertTrue(projects.contains(fProject1));
|
assertTrue(projects.contains(fProject1));
|
||||||
projects.clear();
|
projects.clear();
|
||||||
|
|
||||||
|
|
||||||
TestSourceReader.createFile(fProject1.getProject(), "test.cpp", "int a;");
|
IFile file1= TestSourceReader.createFile(fProject1.getProject(), "test.cpp", "int a;");
|
||||||
TestSourceReader.createFile(fProject2.getProject(), "test.cpp", "int b;");
|
IFile file2= TestSourceReader.createFile(fProject2.getProject(), "test.cpp", "int b;");
|
||||||
Thread.sleep(200);
|
synchronized (mutex) {
|
||||||
|
mutex.wait(1000);
|
||||||
|
if (projects.size() < 2) {
|
||||||
|
mutex.wait(1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
assertEquals(2, projects.size());
|
assertEquals(2, projects.size());
|
||||||
assertTrue(projects.contains(fProject1));
|
assertTrue(projects.contains(fProject1));
|
||||||
assertTrue(projects.contains(fProject2));
|
assertTrue(projects.contains(fProject2));
|
||||||
projects.clear();
|
projects.clear();
|
||||||
}
|
}
|
||||||
|
finally {
|
||||||
|
im.removeIndexChangeListener(listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,9 @@ import java.io.LineNumberReader;
|
||||||
|
|
||||||
import junit.framework.Assert;
|
import junit.framework.Assert;
|
||||||
|
|
||||||
|
import org.eclipse.cdt.core.CCorePlugin;
|
||||||
|
import org.eclipse.cdt.core.index.IIndex;
|
||||||
|
import org.eclipse.cdt.core.index.IIndexFile;
|
||||||
import org.eclipse.core.filebuffers.FileBuffers;
|
import org.eclipse.core.filebuffers.FileBuffers;
|
||||||
import org.eclipse.core.filebuffers.ITextFileBuffer;
|
import org.eclipse.core.filebuffers.ITextFileBuffer;
|
||||||
import org.eclipse.core.filebuffers.ITextFileBufferManager;
|
import org.eclipse.core.filebuffers.ITextFileBufferManager;
|
||||||
|
@ -143,4 +146,35 @@ public class TestSourceReader {
|
||||||
public static IFile createFile(IContainer container, String filePath, String contents) throws CoreException {
|
public static IFile createFile(IContainer container, String filePath, String contents) throws CoreException {
|
||||||
return createFile(container, new Path(filePath), contents);
|
return createFile(container, new Path(filePath), contents);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Waits until the given file is indexed. Fails if this does not happen within the
|
||||||
|
* given time.
|
||||||
|
* @param file
|
||||||
|
* @param maxmillis
|
||||||
|
* @throws Exception
|
||||||
|
* @since 4.0
|
||||||
|
*/
|
||||||
|
public static void waitUntilFileIsIndexed(IIndex index, IFile file, int maxmillis) throws Exception {
|
||||||
|
long endTime= System.currentTimeMillis() + maxmillis;
|
||||||
|
int timeLeft= maxmillis;
|
||||||
|
while (timeLeft >= 0) {
|
||||||
|
Assert.assertTrue(CCorePlugin.getIndexManager().joinIndexer(timeLeft, new NullProgressMonitor()));
|
||||||
|
index.acquireReadLock();
|
||||||
|
try {
|
||||||
|
IIndexFile pfile= index.getFile(file.getLocation());
|
||||||
|
if (pfile != null && pfile.getTimestamp() >= file.getLocalTimeStamp()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
index.releaseReadLock();
|
||||||
|
}
|
||||||
|
|
||||||
|
Thread.sleep(50);
|
||||||
|
timeLeft= (int) (endTime-System.currentTimeMillis());
|
||||||
|
}
|
||||||
|
Assert.fail("Indexing " + file.getFullPath() + " did not complete in time!");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue