1
0
Fork 0
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:
Markus Schorn 2006-10-27 09:47:23 +00:00
parent cd2354d539
commit 1ee1c0cb84
4 changed files with 115 additions and 63 deletions

View file

@ -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.index.IIndex;
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.IndexFilter;
import org.eclipse.cdt.core.model.ICProject;
@ -81,28 +80,6 @@ public class IndexBugsTests extends BaseTestCase {
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) {
String[] parts= qname.split("::");
Pattern[] result= new Pattern[parts.length];
@ -112,6 +89,10 @@ public class IndexBugsTests extends BaseTestCase {
return result;
}
protected void waitUntilFileIsIndexed(IFile file, int time) throws Exception {
TestSourceReader.waitUntilFileIsIndexed(fIndex, file, time);
}
// {bug162011}
// namespace ns162011 {
// class Class162011 {
@ -128,7 +109,7 @@ public class IndexBugsTests extends BaseTestCase {
int indexOfDecl = content.indexOf(funcName);
int indexOfDef = content.indexOf(funcName, indexOfDecl+1);
IFile file= createFile(getProject(), fileName, content);
waitForIndexer(file, 1000);
waitUntilFileIsIndexed(file, 1000);
// make sure the ast is correct
ITranslationUnit tu= (ITranslationUnit) fCProject.findElement(new Path(fileName));

View file

@ -24,9 +24,9 @@ import org.eclipse.cdt.core.index.IIndexFile;
import org.eclipse.cdt.core.index.IndexFilter;
import org.eclipse.cdt.core.model.ICProject;
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.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
@ -111,14 +111,16 @@ public class IndexIncludeTest extends IndexTestBase {
private void checkContext() throws Exception {
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);
waitForIndexer();
ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
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);
waitForIndexer();
TestSourceReader.waitUntilFileIsIndexed(fIndex, file, 1000);
fIndex.acquireReadLock();
try {
IIndexFile ifile= fIndex.getFile(file.getLocation());

View file

@ -18,7 +18,6 @@ import junit.framework.Test;
import org.eclipse.cdt.core.CCorePlugin;
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.IIndexChangeListener;
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.util.BaseTestCase;
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;
public class IndexListenerTest extends BaseTestCase {
protected IIndex fIndex;
private static final IProgressMonitor NPM = new NullProgressMonitor();
private ICProject fProject1;
private ICProject fProject2;
@ -51,54 +52,88 @@ public class IndexListenerTest extends BaseTestCase {
}
public void testIdleListener() throws Exception {
final Object mutex= new Object();
final int[] state= new int[] {0, 0, 0};
IIndexManager im= CCorePlugin.getIndexManager();
assertTrue(im.joinIndexer(10000, NPM));
im.addIndexerStateListener(new IIndexerStateListener() {
IIndexerStateListener listener = new IIndexerStateListener() {
public void indexChanged(IIndexerStateEvent event) {
if (event.indexerIsIdle()) {
state[0]++;
state[2]= 0;
}
else {
state[1]++;
state[2]= 1;
synchronized (mutex) {
if (event.indexerIsIdle()) {
state[0]++;
state[2]= 0;
}
else {
state[1]++;
state[2]= 1;
}
mutex.notify();
}
}
});
};
TestSourceReader.createFile(fProject1.getProject(), "test.cpp", "int a;");
Thread.sleep(200);
assertTrue(im.joinIndexer(10000, new NullProgressMonitor()));
Thread.sleep(200);
assertEquals(1, state[0]);
assertEquals(1, state[1]);
assertEquals(0, state[2]);
im.addIndexerStateListener(listener);
try {
IFile file= TestSourceReader.createFile(fProject1.getProject(), "test.cpp", "int a;");
synchronized (mutex) {
mutex.wait(1000);
if (state[0]+state[1] < 2) {
mutex.wait(1000);
}
}
assertEquals(1, state[0]);
assertEquals(1, state[1]);
assertEquals(0, state[2]);
}
finally {
im.removeIndexerStateListener(listener);
}
}
public void testChangeListener() throws Exception {
final Object mutex= new Object();
final List projects= new ArrayList();
IIndexManager im= CCorePlugin.getIndexManager();
im.addIndexChangeListener(new IIndexChangeListener() {
assertTrue(im.joinIndexer(10000, NPM));
IIndexChangeListener listener = new IIndexChangeListener() {
public void indexChanged(IIndexChangeEvent event) {
projects.add(event.getAffectedProject());
synchronized (mutex) {
projects.add(event.getAffectedProject());
mutex.notify();
}
}
});
TestSourceReader.createFile(fProject1.getProject(), "test.cpp", "int a;");
Thread.sleep(200);
assertEquals(1, projects.size());
assertTrue(projects.contains(fProject1));
projects.clear();
};
im.addIndexChangeListener(listener);
try {
IFile file= TestSourceReader.createFile(fProject1.getProject(), "test.cpp", "int a;");
synchronized (mutex) {
mutex.wait(1000);
}
assertEquals(1, projects.size());
assertTrue(projects.contains(fProject1));
projects.clear();
TestSourceReader.createFile(fProject1.getProject(), "test.cpp", "int a;");
TestSourceReader.createFile(fProject2.getProject(), "test.cpp", "int b;");
Thread.sleep(200);
assertEquals(2, projects.size());
assertTrue(projects.contains(fProject1));
assertTrue(projects.contains(fProject2));
projects.clear();
IFile file1= TestSourceReader.createFile(fProject1.getProject(), "test.cpp", "int a;");
IFile file2= TestSourceReader.createFile(fProject2.getProject(), "test.cpp", "int b;");
synchronized (mutex) {
mutex.wait(1000);
if (projects.size() < 2) {
mutex.wait(1000);
}
}
assertEquals(2, projects.size());
assertTrue(projects.contains(fProject1));
assertTrue(projects.contains(fProject2));
projects.clear();
}
finally {
im.removeIndexChangeListener(listener);
}
}
}

View file

@ -19,6 +19,9 @@ import java.io.LineNumberReader;
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.ITextFileBuffer;
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 {
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!");
}
}