Skip to content

Commit

Permalink
Fix CDescriptorOldTests.testDescriptorCreation instability
Browse files Browse the repository at this point in the history
This old test had a race condition. The failing test was trying
to verify that CDTPROJECT_ADDED was received, but if there
was a delay in the startup then another event would come in
later. So for this test use the first received event,
for the remaining tests use the last received event.

Part of eclipse-cdt#117
  • Loading branch information
jonahgraham committed Oct 25, 2022
1 parent 4643e42 commit fac43c5
Showing 1 changed file with 39 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package org.eclipse.cdt.core.cdescriptor.tests;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.CDescriptorEvent;
import org.eclipse.cdt.core.CProjectNature;
Expand Down Expand Up @@ -55,7 +58,7 @@ public class CDescriptorOldTests extends TestCase {
static String projectId = CTestPlugin.PLUGIN_ID + ".TestProject";
static IProject fProject;
static CDescriptorListener listener = new CDescriptorListener();
static CDescriptorEvent fLastEvent;
static final List<CDescriptorEvent> fEvents = new ArrayList<>();

/**
* Constructor for CDescriptorTest.
Expand Down Expand Up @@ -113,7 +116,7 @@ static public class CDescriptorListener implements ICDescriptorListener {

@Override
public void descriptorChanged(CDescriptorEvent event) {
fLastEvent = event;
fEvents.add(event);
}
}

Expand Down Expand Up @@ -155,11 +158,11 @@ public void run(IProgressMonitor monitor) throws CoreException {
}, null);
ICDescriptor desc = CCorePlugin.getDefault().getCProjectDescription(fProject, true);

Assert.assertNotNull(fLastEvent);
Assert.assertEquals(fLastEvent.getDescriptor(), desc);
Assert.assertEquals(fLastEvent.getType(), CDescriptorEvent.CDTPROJECT_ADDED);
Assert.assertEquals(fLastEvent.getFlags(), 0);
fLastEvent = null;
Assert.assertFalse(fEvents.isEmpty());
Assert.assertEquals(fEvents.get(0).getDescriptor(), desc);
Assert.assertEquals(fEvents.get(0).getType(), CDescriptorEvent.CDTPROJECT_ADDED);
Assert.assertEquals(fEvents.get(0).getFlags(), 0);
fEvents.clear();

Assert.assertEquals(fProject, desc.getProject());
Assert.assertEquals("*", desc.getPlatform());
Expand All @@ -185,7 +188,7 @@ public void run() {
Element data = desc.getProjectData("testElement0");
data.appendChild(data.getOwnerDocument().createElement("test"));
desc.saveProjectData();
fLastEvent = null;
fEvents.clear();
}

// https://bugs.eclipse.org/bugs/show_bug.cgi?id=185930
Expand Down Expand Up @@ -254,7 +257,7 @@ public void execute(ICDescriptor descriptor, IProgressMonitor monitor)
lastLength += threads.length; // Update last lengths to what we expect
assertEquals("Iteration count: " + i, lastLength, lengthAfter);

fLastEvent = null;
fEvents.clear();
}
}

Expand Down Expand Up @@ -298,7 +301,7 @@ public void run() {
desc.saveProjectData();
t.join();

fLastEvent = null;
fEvents.clear();
}
}

Expand All @@ -318,11 +321,11 @@ public void testExtensionCreation() throws Exception {
ICDescriptor desc = CCorePlugin.getDefault().getCProjectDescription(fProject, true);
ICExtensionReference extRef = desc.create("org.eclipse.cdt.testextension", "org.eclipse.cdt.testextensionID");

Assert.assertNotNull(fLastEvent);
Assert.assertEquals(fLastEvent.getDescriptor(), desc);
Assert.assertEquals(fLastEvent.getType(), CDescriptorEvent.CDTPROJECT_CHANGED);
Assert.assertEquals(fLastEvent.getFlags(), CDescriptorEvent.EXTENSION_CHANGED);
fLastEvent = null;
Assert.assertNotNull(fEvents);
Assert.assertEquals(fEvents.get(fEvents.size() - 1).getDescriptor(), desc);
Assert.assertEquals(fEvents.get(fEvents.size() - 1).getType(), CDescriptorEvent.CDTPROJECT_CHANGED);
Assert.assertEquals(fEvents.get(fEvents.size() - 1).getFlags(), CDescriptorEvent.EXTENSION_CHANGED);
fEvents.clear();

Assert.assertEquals("org.eclipse.cdt.testextension", extRef.getExtension());
Assert.assertEquals("org.eclipse.cdt.testextensionID", extRef.getID());
Expand All @@ -341,11 +344,11 @@ public void testExtensionData() throws Exception {
ICExtensionReference extRef[] = desc.get("org.eclipse.cdt.testextension");
extRef[0].setExtensionData("testKey", "testValue");

Assert.assertNotNull(fLastEvent);
Assert.assertEquals(fLastEvent.getDescriptor(), desc);
Assert.assertEquals(fLastEvent.getType(), CDescriptorEvent.CDTPROJECT_CHANGED);
Assert.assertEquals(fLastEvent.getFlags(), 0);
fLastEvent = null;
Assert.assertNotNull(fEvents);
Assert.assertEquals(fEvents.get(fEvents.size() - 1).getDescriptor(), desc);
Assert.assertEquals(fEvents.get(fEvents.size() - 1).getType(), CDescriptorEvent.CDTPROJECT_CHANGED);
Assert.assertEquals(fEvents.get(fEvents.size() - 1).getFlags(), 0);
fEvents.clear();

Assert.assertEquals("testValue", extRef[0].getExtensionData("testKey"));
extRef[0].setExtensionData("testKey", null);
Expand All @@ -357,11 +360,11 @@ public void testExtensionRemove() throws Exception {
ICExtensionReference extRef[] = desc.get("org.eclipse.cdt.testextension");
desc.remove(extRef[0]);

Assert.assertNotNull(fLastEvent);
Assert.assertEquals(fLastEvent.getDescriptor(), desc);
Assert.assertEquals(fLastEvent.getType(), CDescriptorEvent.CDTPROJECT_CHANGED);
Assert.assertEquals(fLastEvent.getFlags(), CDescriptorEvent.EXTENSION_CHANGED);
fLastEvent = null;
Assert.assertNotNull(fEvents);
Assert.assertEquals(fEvents.get(fEvents.size() - 1).getDescriptor(), desc);
Assert.assertEquals(fEvents.get(fEvents.size() - 1).getType(), CDescriptorEvent.CDTPROJECT_CHANGED);
Assert.assertEquals(fEvents.get(fEvents.size() - 1).getFlags(), CDescriptorEvent.EXTENSION_CHANGED);
fEvents.clear();

}

Expand All @@ -371,11 +374,11 @@ public void testProjectDataCreate() throws Exception {
data.appendChild(data.getOwnerDocument().createElement("test"));
desc.saveProjectData();

Assert.assertNotNull(fLastEvent);
Assert.assertEquals(fLastEvent.getDescriptor(), desc);
Assert.assertEquals(fLastEvent.getType(), CDescriptorEvent.CDTPROJECT_CHANGED);
Assert.assertEquals(fLastEvent.getFlags(), 0);
fLastEvent = null;
Assert.assertNotNull(fEvents);
Assert.assertEquals(fEvents.get(fEvents.size() - 1).getDescriptor(), desc);
Assert.assertEquals(fEvents.get(fEvents.size() - 1).getType(), CDescriptorEvent.CDTPROJECT_CHANGED);
Assert.assertEquals(fEvents.get(fEvents.size() - 1).getFlags(), 0);
fEvents.clear();
}

public void testProjectDataDelete() throws Exception {
Expand All @@ -386,11 +389,11 @@ public void testProjectDataDelete() throws Exception {
data.removeChild(data.getFirstChild());
desc.saveProjectData();

Assert.assertNotNull(fLastEvent);
Assert.assertEquals(fLastEvent.getDescriptor(), desc);
Assert.assertEquals(fLastEvent.getType(), CDescriptorEvent.CDTPROJECT_CHANGED);
Assert.assertEquals(fLastEvent.getFlags(), 0);
fLastEvent = null;
Assert.assertNotNull(fEvents);
Assert.assertEquals(fEvents.get(fEvents.size() - 1).getDescriptor(), desc);
Assert.assertEquals(fEvents.get(fEvents.size() - 1).getType(), CDescriptorEvent.CDTPROJECT_CHANGED);
Assert.assertEquals(fEvents.get(fEvents.size() - 1).getFlags(), 0);
fEvents.clear();
}

public void testProjectStorageDelete() throws Exception {
Expand All @@ -410,7 +413,7 @@ public void testProjectStorageDelete() throws Exception {
desc = CCorePlugin.getDefault().getCProjectDescription(fProject, true);
data = desc.getProjectData("testElement");
assertTrue(data.getChildNodes().getLength() == 0);
fLastEvent = null;
fEvents.clear();
}

}

0 comments on commit fac43c5

Please sign in to comment.