Skip to content

Commit 289b4f7

Browse files
Copilotlaeubi
authored andcommitted
Fix flapping tests by replacing fixed delays with condition-based waiting
1 parent 6f2025c commit 289b4f7

File tree

6 files changed

+123
-21
lines changed

6 files changed

+123
-21
lines changed

tests/org.eclipse.e4.ui.tests/src/org/eclipse/e4/ui/tests/application/ESelectionServiceTest.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,12 +1011,12 @@ public void testBug343984() throws Exception {
10111011
selectionService.addSelectionListener(listener);
10121012

10131013
selectionService.setSelection(new Object());
1014-
Thread.sleep(1000);
1014+
waitForCondition(() -> listener.success, 5000, "Listener should be called after setSelection");
10151015
assertTrue(listener.success);
10161016

10171017
listener.reset();
10181018
selectionService.setSelection(new Object());
1019-
Thread.sleep(1000);
1019+
waitForCondition(() -> listener.success, 5000, "Listener should be called after second setSelection");
10201020
assertTrue(listener.success);
10211021
}
10221022

@@ -1064,6 +1064,40 @@ private void initialize() {
10641064
applicationContext.set(UIEventPublisher.class, ep);
10651065
}
10661066

1067+
/**
1068+
* Wait for a condition to become true, processing UI events while waiting.
1069+
*
1070+
* @param condition the condition to wait for
1071+
* @param timeoutMillis maximum time to wait in milliseconds
1072+
* @param message error message if timeout occurs
1073+
*/
1074+
private void waitForCondition(java.util.function.BooleanSupplier condition, long timeoutMillis, String message) {
1075+
Display display = Display.getCurrent();
1076+
if (display == null) {
1077+
display = Display.getDefault();
1078+
}
1079+
1080+
long startTime = System.currentTimeMillis();
1081+
while (!condition.getAsBoolean()) {
1082+
if (System.currentTimeMillis() - startTime > timeoutMillis) {
1083+
throw new AssertionError(message + " (timeout after " + timeoutMillis + "ms)");
1084+
}
1085+
1086+
// Process pending UI events
1087+
if (display.readAndDispatch()) {
1088+
continue;
1089+
}
1090+
1091+
// Small sleep to avoid busy waiting
1092+
try {
1093+
Thread.sleep(10);
1094+
} catch (InterruptedException e) {
1095+
Thread.currentThread().interrupt();
1096+
throw new AssertionError(message + " (interrupted)", e);
1097+
}
1098+
}
1099+
}
1100+
10671101
static class SelectionListener implements ISelectionListener {
10681102

10691103
private MPart part;

tests/org.eclipse.e4.ui.tests/src/org/eclipse/e4/ui/tests/workbench/PartRenderingEngineTests.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,33 @@ public void tearDown() throws Exception {
113113
}
114114

115115
private void checkLog() {
116-
try {
117-
// sleep a bit because notifications are done on another thread
118-
Thread.sleep(100);
119-
} catch (Exception e) {
120-
// ignored
116+
// Wait a bit because notifications are done on another thread
117+
// Use a short timeout to allow log events to be processed
118+
long startTime = System.currentTimeMillis();
119+
long timeout = 1000; // 1 second max wait
120+
121+
while (System.currentTimeMillis() - startTime < timeout) {
122+
// Process any pending display events if we're on the UI thread
123+
Display display = Display.getCurrent();
124+
if (display != null) {
125+
while (display.readAndDispatch()) {
126+
// Keep processing
127+
}
128+
}
129+
130+
// If already logged, no need to wait longer
131+
if (logged) {
132+
break;
133+
}
134+
135+
try {
136+
Thread.sleep(10);
137+
} catch (InterruptedException e) {
138+
Thread.currentThread().interrupt();
139+
break;
140+
}
121141
}
142+
122143
assertFalse(logged);
123144
}
124145

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/decorators/DecoratorTableTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@
1818
import org.eclipse.ui.IViewPart;
1919
import org.eclipse.ui.IWorkbenchPage;
2020
import org.eclipse.ui.PartInitException;
21-
import org.junit.Ignore;
2221

2322
/**
2423
* The DecoratorTableTest is the test for decorating tables.
2524
*/
26-
@Ignore("Disabled due to timing issues")
2725
public class DecoratorTableTest extends DecoratorViewerTest {
2826

2927
@Override

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/decorators/DecoratorTestPart.java

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@
2626
*/
2727
public abstract class DecoratorTestPart extends ViewPart {
2828

29-
private static final int DELAY_TIME = 2000;// Wait 2 seconds
29+
private static final int INITIAL_DELAY_TIME = 500;// Initial wait time in milliseconds
30+
private static final int MAX_WAIT_TIME = 10000;// Maximum wait time (10 seconds)
31+
private static final int IDLE_TIME = 200;// Time to wait after last update
3032

3133
public boolean waitingForDecoration = true;
3234

33-
private long endTime;
35+
private volatile long lastUpdateTime;
36+
private long startTime;
3437

3538
private ILabelProviderListener listener;
3639

@@ -54,22 +57,55 @@ protected DecoratingLabelProvider getLabelProvider() {
5457
* Get the listener for the suite.
5558
*/
5659
private ILabelProviderListener getDecoratorManagerListener() {
57-
// Reset the end time each time we get an update
58-
listener = event -> endTime = System.currentTimeMillis() + DELAY_TIME;
60+
// Record the time each time we get an update
61+
listener = event -> lastUpdateTime = System.currentTimeMillis();
5962

6063
return listener;
6164
}
6265

66+
/**
67+
* Process events until decorations are applied. This waits for updates to settle
68+
* by ensuring no new updates occur for IDLE_TIME milliseconds, with a maximum
69+
* wait of MAX_WAIT_TIME.
70+
*/
6371
public void readAndDispatchForUpdates() {
64-
while (System.currentTimeMillis() < endTime) {
65-
Display.getCurrent().readAndDispatch();
72+
Display display = Display.getCurrent();
73+
long elapsed = System.currentTimeMillis() - startTime;
74+
75+
// Process events and wait for updates to settle
76+
while (elapsed < MAX_WAIT_TIME) {
77+
// Process any pending UI events
78+
while (display.readAndDispatch()) {
79+
// Keep processing
80+
}
81+
82+
long timeSinceLastUpdate = System.currentTimeMillis() - lastUpdateTime;
83+
84+
// If we haven't received an update in IDLE_TIME, we're done
85+
if (timeSinceLastUpdate >= IDLE_TIME && elapsed >= INITIAL_DELAY_TIME) {
86+
break;
87+
}
88+
89+
// Small sleep to avoid busy waiting
90+
try {
91+
Thread.sleep(50);
92+
} catch (InterruptedException e) {
93+
Thread.currentThread().interrupt();
94+
break;
95+
}
96+
97+
elapsed = System.currentTimeMillis() - startTime;
98+
}
99+
100+
// Final event processing pass
101+
while (display.readAndDispatch()) {
102+
// Keep processing
66103
}
67-
68104
}
69105

70106
public void setUpForDecorators() {
71-
endTime = System.currentTimeMillis() + DELAY_TIME;
72-
107+
startTime = System.currentTimeMillis();
108+
lastUpdateTime = System.currentTimeMillis();
73109
}
74110

75111
@Override

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/decorators/DecoratorTreeTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@
1919
import org.eclipse.ui.IViewPart;
2020
import org.eclipse.ui.IWorkbenchPage;
2121
import org.eclipse.ui.PartInitException;
22-
import org.junit.Ignore;
2322

2423
/**
2524
* The DecoratorTreeTest tests the font and color support on
2625
* tree viewers.
2726
*/
28-
@Ignore("Disabled due to timing issues")
2927
public class DecoratorTreeTest extends DecoratorViewerTest {
3028

3129
@Override

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/internal/Bug78470Test.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,22 @@ public void partInputChanged(IWorkbenchPartReference partRef) {
114114
});
115115
workbench.showPerspective(MyPerspective.ID, activeWorkbenchWindow);
116116
processEvents();
117-
Thread.sleep(2000);
117+
118+
// Wait for the part to become visible, processing events
119+
long startTime = System.currentTimeMillis();
120+
long timeout = 5000; // 5 second max wait
121+
while (!partVisibleExecuted && (System.currentTimeMillis() - startTime < timeout)) {
122+
processEvents();
123+
if (!partVisibleExecuted) {
124+
try {
125+
Thread.sleep(50);
126+
} catch (InterruptedException e) {
127+
Thread.currentThread().interrupt();
128+
break;
129+
}
130+
}
131+
}
132+
118133
assertTrue("view was not made visible", partVisibleExecuted);
119134
assertNotNull(activePage.findView(MyViewPart.ID2));
120135
assertNotNull(activePage.findView(MyViewPart.ID3));

0 commit comments

Comments
 (0)