Skip to content

Commit fe29dc3

Browse files
committed
Make waitWhile() utility method in debug tests static
The waitWhile() method for waiting until a condition is fulfilled is implemented as an instance method of AbstractDebugTest. The reason is that the test class instance itself is passed as a context to that method. This context, however, is not used by any of the method's consumers. This change removes the unnecessary context from the waitWhile() method and simplifies all consumers accordingly. With that, the method in AbstractDebugTests degrades to a pure delegation to the same method in TestUtil, which is why the method is removed from the test superclass and consumers are adapted to directly call the method in TestUtil.
1 parent bd34de0 commit fe29dc3

19 files changed

+315
-323
lines changed

debug/org.eclipse.debug.tests/src/org/eclipse/debug/tests/AbstractDebugTest.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
*******************************************************************************/
1414
package org.eclipse.debug.tests;
1515

16-
import java.util.function.Function;
17-
import java.util.function.Predicate;
18-
1916
import org.eclipse.core.runtime.IProgressMonitor;
2017
import org.eclipse.core.runtime.IStatus;
2118
import org.eclipse.core.runtime.Status;
@@ -82,24 +79,6 @@ public IStatus runInUIThread(IProgressMonitor monitor) {
8279
}
8380
}
8481

85-
/**
86-
* Waits while given condition is {@code true} for some time. If the actual
87-
* wait time exceeds {@link TestUtil#DEFAULT_TIMEOUT} and condition will be still
88-
* {@code true}, throws {@link junit.framework.AssertionFailedError} with
89-
* given message.
90-
* <p>
91-
* Will process UI events while waiting in UI thread, if called from
92-
* background thread, just waits.
93-
*
94-
* @param condition function which will be evaluated while waiting
95-
* @param errorMessage message which will be used to construct the failure
96-
* exception in case the condition will still return {@code true}
97-
* after given timeout
98-
*/
99-
public void waitWhile(Predicate<AbstractDebugTest> condition, Function<AbstractDebugTest, String> errorMessage) throws Exception {
100-
TestUtil.waitWhile(condition, this, errorMessage);
101-
}
102-
10382
private static void closeIntro(final IWorkbench wb) {
10483
IWorkbenchWindow window = wb.getActiveWorkbenchWindow();
10584
if (window != null) {

debug/org.eclipse.debug.tests/src/org/eclipse/debug/tests/TestUtil.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.LinkedHashSet;
2323
import java.util.List;
2424
import java.util.Set;
25+
import java.util.function.BooleanSupplier;
2526
import java.util.function.Function;
2627
import java.util.function.Predicate;
2728
import java.util.function.Supplier;
@@ -121,18 +122,16 @@ public static void processUIEvents(final long millis) throws Exception {
121122
* Will process UI events while waiting in UI thread, if called from
122123
* background thread, just waits.
123124
*
124-
* @param <T> type of the context
125125
* @param condition function which will be evaluated while waiting
126-
* @param context test context
127126
* @param timeout max wait time in milliseconds to wait on given condition
128127
* @param errorMessage message which will be used to construct the failure
129128
* exception in case the condition will still return {@code true}
130129
* after given timeout
131130
*/
132-
public static <T> void waitWhile(Predicate<T> condition, T context, long timeout, Function<T, String> errorMessage) throws Exception {
131+
public static void waitWhile(BooleanSupplier condition, long timeout, Supplier<String> errorMessage) throws Exception {
133132
long start = System.currentTimeMillis();
134133
Display display = Display.getCurrent();
135-
while (System.currentTimeMillis() - start < timeout && condition.test(context)) {
134+
while (System.currentTimeMillis() - start < timeout && condition.getAsBoolean()) {
136135
if (display != null && !display.isDisposed()) {
137136
if (!display.readAndDispatch()) {
138137
Thread.sleep(0);
@@ -141,9 +140,9 @@ public static <T> void waitWhile(Predicate<T> condition, T context, long timeout
141140
Thread.sleep(5);
142141
}
143142
}
144-
Boolean stillTrue = condition.test(context);
143+
Boolean stillTrue = condition.getAsBoolean();
145144
if (stillTrue) {
146-
fail(errorMessage.apply(context));
145+
fail(errorMessage.get());
147146
}
148147
}
149148

@@ -156,15 +155,13 @@ public static <T> void waitWhile(Predicate<T> condition, T context, long timeout
156155
* Will process UI events while waiting in UI thread, if called from
157156
* background thread, just waits.
158157
*
159-
* @param <T> type of the context
160158
* @param condition function which will be evaluated while waiting
161-
* @param context test context
162159
* @param errorMessage message which will be used to construct the failure
163160
* exception in case the condition will still return {@code true}
164161
* after given timeout
165162
*/
166-
public static <T> void waitWhile(Predicate<T> condition, T context, Function<T, String> errorMessage) throws Exception {
167-
waitWhile(condition, context, DEFAULT_TIMEOUT, errorMessage);
163+
public static void waitWhile(BooleanSupplier condition, Supplier<String> errorMessage) throws Exception {
164+
waitWhile(condition, DEFAULT_TIMEOUT, errorMessage);
168165
}
169166

170167
/**

debug/org.eclipse.debug.tests/src/org/eclipse/debug/tests/breakpoint/BreakpointTests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,45 +86,45 @@ public void testBug424561_undoRedoUndoGone() throws Exception {
8686
IUndoContext context = DebugUITools.getBreakpointsUndoContext();
8787

8888
bpm.addBreakpoint(bp);
89-
TestUtil.waitWhile(c -> c.getTestBreakpoints().isEmpty(), this, c -> "Breakpoint is not created");
89+
TestUtil.waitWhile(() -> getTestBreakpoints().isEmpty(), () -> "Breakpoint is not created");
9090
assertTrue("Breakpoint marker missing", bp.getMarker().exists());
9191
assertTrue("Breakpoint not registered", bp.isRegistered());
9292

9393
DebugUITools.deleteBreakpoints(new IBreakpoint[] {
9494
bp }, null, null);
9595
assertTrue(operationHistory.canUndo(context));
96-
TestUtil.waitWhile(c -> !c.getTestBreakpoints().isEmpty(), this, c -> "Breakpoint is not deleted");
96+
TestUtil.waitWhile(() -> !getTestBreakpoints().isEmpty(), () -> "Breakpoint is not deleted");
9797
assertFalse("Breakpoint marker not removed", bp.getMarker().exists());
9898
assertFalse("Breakpoint still registered", bp.isRegistered());
9999

100100
operationHistory.undo(context, null, null);
101101
assertTrue(operationHistory.canRedo(context));
102-
TestUtil.waitWhile(c -> c.getTestBreakpoints().isEmpty(), this, c -> "Breakpoint is not recreated");
102+
TestUtil.waitWhile(() -> getTestBreakpoints().isEmpty(), () -> "Breakpoint is not recreated");
103103
bp = getTestBreakpoints().get(0);
104104
assertEquals("Breakpoint attributes not correctly restored", content, bp.getText());
105105
assertTrue("Breakpoint marker missing", bp.getMarker().exists());
106106
assertTrue("Breakpoint not registered", bp.isRegistered());
107107

108108
operationHistory.redo(context, null, null);
109109
assertTrue(operationHistory.canUndo(context));
110-
TestUtil.waitWhile(c -> !c.getTestBreakpoints().isEmpty(), this, c -> "Breakpoint is not deleted");
110+
TestUtil.waitWhile(() -> !getTestBreakpoints().isEmpty(), () -> "Breakpoint is not deleted");
111111
assertFalse("Breakpoint marker not removed", bp.getMarker().exists());
112112
assertFalse("Breakpoint still registered", bp.isRegistered());
113113

114114
operationHistory.undo(context, null, null);
115115
assertTrue(operationHistory.canRedo(context));
116-
TestUtil.waitWhile(c -> c.getTestBreakpoints().isEmpty(), this, c -> "Breakpoint is not recreated");
116+
TestUtil.waitWhile(() -> getTestBreakpoints().isEmpty(), () -> "Breakpoint is not recreated");
117117
bp = getTestBreakpoints().get(0);
118118
assertEquals("Breakpoint attributes not correctly restored", content, bp.getText());
119119
assertTrue("Breakpoint marker missing", bp.getMarker().exists());
120120
assertTrue("Breakpoint not registered", bp.isRegistered());
121121

122122
final BreakpointsView finalView = view;
123123
final TestBreakpoint finalBp = bp;
124-
TestUtil.waitWhile(c -> {
124+
TestUtil.waitWhile(() -> {
125125
TreeItem item = (TreeItem) finalView.getTreeModelViewer().testFindItem(finalBp);
126126
return item == null || item.getText() == null || !item.getText().contains(content);
127-
}, this, c -> "Breakpoint not restored in view");
127+
}, () -> "Breakpoint not restored in view");
128128
} finally {
129129
if (!viewVisible) {
130130
DebugUIPlugin.getActiveWorkbenchWindow().getActivePage().hideView(view);

debug/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/ProcessConsoleTests.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import static java.nio.file.Files.readAllBytes;
1717
import static org.assertj.core.api.Assertions.assertThat;
18+
import static org.eclipse.debug.tests.TestUtil.waitWhile;
1819
import static org.junit.Assert.assertEquals;
1920
import static org.junit.Assert.assertTrue;
2021

@@ -35,8 +36,8 @@
3536
import java.util.Map;
3637
import java.util.concurrent.TimeUnit;
3738
import java.util.concurrent.atomic.AtomicBoolean;
38-
import java.util.function.Function;
39-
import java.util.function.Predicate;
39+
import java.util.function.BooleanSupplier;
40+
import java.util.function.Supplier;
4041
import java.util.stream.Collectors;
4142
import java.util.stream.Stream;
4243

@@ -296,7 +297,7 @@ public void processTerminationTest(ILaunchConfiguration launchConfig, boolean te
296297
if (mockProcess.isAlive()) {
297298
mockProcess.destroy();
298299
}
299-
waitWhile(__ -> !terminationSignaled.get(), __ -> "No console complete notification received.");
300+
waitWhile(() -> !terminationSignaled.get(), () -> "No console complete notification received.");
300301
} finally {
301302
consoleManager.removeConsoles(new IConsole[] { console });
302303
TestUtil.waitForJobs(name.getMethodName(), ConsoleManager.CONSOLE_JOB_FAMILY, 0, 10000);
@@ -395,7 +396,7 @@ private IOConsole doConsoleOutputTest(byte[] testContent, Map<String, Object> la
395396
try {
396397
consoleManager.addConsoles(new IConsole[] { console });
397398
mockProcess.destroy();
398-
waitWhile(c -> !consoleFinished.get(), c -> "Console did not finished.");
399+
waitWhile(() -> !consoleFinished.get(), () -> "Console did not finished.");
399400

400401
Object value = launchConfigAttributes != null ? launchConfigAttributes.get(IDebugUIConstants.ATTR_CAPTURE_IN_FILE) : null;
401402
final File outFile = value != null ? new File((String) value) : null;
@@ -452,15 +453,15 @@ public void testOutput() throws Exception {
452453
mockProcess.destroy();
453454
sysout.close();
454455

455-
Predicate<AbstractDebugTest> waitForLastLineWritten = __ -> {
456+
BooleanSupplier waitForLastLineWritten = () -> {
456457
try {
457458
TestUtil.processUIEvents(50);
458459
} catch (Exception e) {
459460
// try again
460461
}
461462
return console.getDocument().getNumberOfLines() < lines.length;
462463
};
463-
Function<AbstractDebugTest, String> errorMessageProvider = __ -> {
464+
Supplier<String> errorMessageProvider = () -> {
464465
String expected = String.join(System.lineSeparator(), lines);
465466
String actual = console.getDocument().get();
466467
return "Not all lines have been written, expected: " + expected + ", was: " + actual;
@@ -503,7 +504,7 @@ public void testBinaryOutputToFile() throws Exception {
503504
try {
504505
console.initialize();
505506

506-
Predicate<AbstractDebugTest> waitForFileWritten = __ -> {
507+
BooleanSupplier waitForFileWritten = () -> {
507508
try {
508509
TestUtil.processUIEvents(20);
509510
return readAllBytes(outFile.toPath()).length < output.length;
@@ -512,7 +513,7 @@ public void testBinaryOutputToFile() throws Exception {
512513
}
513514
return false;
514515
};
515-
Function<AbstractDebugTest, String> errorMessageProvider = __ -> {
516+
Supplier<String> errorMessageProvider = () -> {
516517
byte[] actualOutput = new byte[0];
517518
try {
518519
actualOutput = readAllBytes(outFile.toPath());

debug/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/RuntimeProcessTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void testProcessTerminated() throws Exception {
6464
mockProcess.setExitValue(1);
6565
mockProcess.destroy();
6666

67-
TestUtil.waitWhile(p -> !p.isTerminated(), runtimeProcess, p -> "RuntimeProcess not terminated.");
67+
TestUtil.waitWhile(() -> !runtimeProcess.isTerminated(), () -> "RuntimeProcess not terminated.");
6868
TestUtil.waitForJobs(name.getMethodName(), 25, TestUtil.DEFAULT_TIMEOUT);
6969
assertEquals("Wrong number of terminate events.", 1, processTerminateEvents.get());
7070
assertEquals("RuntimeProcess reported wrong exit code.", 1, runtimeProcess.getExitValue());
@@ -92,7 +92,7 @@ public void testTerminateProcess() throws Exception {
9292
runtimeProcess.terminate();
9393
assertFalse("RuntimeProcess failed to terminate wrapped process.", mockProcess.isAlive());
9494

95-
TestUtil.waitWhile(p -> !p.isTerminated(), runtimeProcess, p -> "RuntimeProcess not terminated.");
95+
TestUtil.waitWhile(() -> !runtimeProcess.isTerminated(), () -> "RuntimeProcess not terminated.");
9696
TestUtil.waitForJobs(name.getMethodName(), 25, TestUtil.DEFAULT_TIMEOUT);
9797
assertEquals("Wrong number of terminate events.", 1, processTerminateEvents.get());
9898
assertEquals("RuntimeProcess reported wrong exit code.", 1, runtimeProcess.getExitValue());
@@ -129,7 +129,7 @@ public void testTerminateProcessWithSubProcesses() throws Exception {
129129
assertFalse("RuntimeProcess failed to terminate child of wrapped process.", childProcess2.isAlive());
130130
assertFalse("RuntimeProcess failed to terminate descendant of wrapped process.", grandChildProcess.isAlive());
131131

132-
TestUtil.waitWhile(p -> !p.isTerminated(), runtimeProcess, p -> "RuntimeProcess not terminated.");
132+
TestUtil.waitWhile(() -> !runtimeProcess.isTerminated(), () -> "RuntimeProcess not terminated.");
133133
}
134134

135135
/**
@@ -154,7 +154,7 @@ public void testTerminateProcessWithoutTerminatingDescendents() throws Exception
154154
assertFalse("RuntimeProcess failed to terminate wrapped process.", mockProcess.isAlive());
155155
assertTrue("RuntimeProcess terminated child of wrapped process, unlike configured.", childProcess.isAlive());
156156

157-
TestUtil.waitWhile(p -> !p.isTerminated(), runtimeProcess, p -> "RuntimeProcess not terminated.");
157+
TestUtil.waitWhile(() -> !runtimeProcess.isTerminated(), () -> "RuntimeProcess not terminated.");
158158
}
159159

160160
/**
@@ -173,7 +173,7 @@ public void testTerminateProcessNotSupportingProcessToHandle() throws Exception
173173
RuntimeProcess runtimeProcess = mockProcess.toRuntimeProcess();
174174
runtimeProcess.terminate(); // must not throw, even toHandle() does
175175

176-
TestUtil.waitWhile(p -> !p.isTerminated(), runtimeProcess, p -> "RuntimeProcess not terminated.");
176+
TestUtil.waitWhile(() -> !runtimeProcess.isTerminated(), () -> "RuntimeProcess not terminated.");
177177
}
178178

179179
/**

debug/org.eclipse.debug.tests/src/org/eclipse/debug/tests/launching/LaunchConfigurationTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package org.eclipse.debug.tests.launching;
1616

1717
import static org.assertj.core.api.Assertions.assertThat;
18+
import static org.eclipse.debug.tests.TestUtil.waitWhile;
1819
import static org.junit.Assert.assertEquals;
1920
import static org.junit.Assert.assertFalse;
2021
import static org.junit.Assert.assertNotEquals;
@@ -1306,8 +1307,7 @@ public void launchesTerminated(ILaunch[] launches) {
13061307
IProcess process = null;
13071308
try {
13081309
process = DebugPlugin.newProcess(launch, new MockProcess(0), "test-terminate-timestamp");
1309-
waitWhile(__ -> !terminatedLaunches.contains(launch),
1310-
__ -> "Launch termination event did not occur: "+
1310+
waitWhile(() -> !terminatedLaunches.contains(launch), () -> "Launch termination event did not occur: " +
13111311
"launch termination state is \"" + launch.isTerminated() + "\" " +
13121312
"and " + terminatedLaunches.size() + " launches have terminated");
13131313
String launchTerminateTimestampUntyped = launch.getAttribute(DebugPlugin.ATTR_TERMINATE_TIMESTAMP);
@@ -1454,7 +1454,7 @@ public void addProcess(IProcess process) {
14541454
IProcess runtimeProcess = null;
14551455
try {
14561456
runtimeProcess = DebugPlugin.newProcess(launch, mockProcess, "test-terminate-launch-listener");
1457-
waitWhile(__ -> !launchTerminated.get(), __ -> "Launch termination event did not occur");
1457+
waitWhile(() -> !launchTerminated.get(), () -> "Launch termination event did not occur");
14581458
} finally {
14591459
DebugPlugin.getDefault().getLaunchManager().removeLaunchListener(listener);
14601460
if (launch != null) {

debug/org.eclipse.debug.tests/src/org/eclipse/debug/tests/viewer/model/AbstractViewerModelTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*******************************************************************************/
1414
package org.eclipse.debug.tests.viewer.model;
1515

16-
import java.util.function.Function;
16+
import java.util.function.Supplier;
1717

1818
import org.eclipse.debug.internal.ui.viewers.model.IInternalTreeModelViewer;
1919
import org.eclipse.debug.tests.AbstractDebugTest;
@@ -62,8 +62,8 @@ public void tearDown() throws Exception {
6262

6363
abstract protected TestModelUpdatesListener createListener(IInternalTreeModelViewer viewer);
6464

65-
protected Function<AbstractDebugTest, String> createListenerErrorMessage() {
66-
return t -> "Listener not finished: " + fListener;
65+
protected Supplier<String> createListenerErrorMessage() {
66+
return () -> "Listener not finished: " + fListener;
6767
}
6868

6969
}

debug/org.eclipse.debug.tests/src/org/eclipse/debug/tests/viewer/model/CheckTests.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*******************************************************************************/
1515
package org.eclipse.debug.tests.viewer.model;
1616

17+
import static org.eclipse.debug.tests.TestUtil.waitWhile;
18+
1719
import org.eclipse.debug.internal.ui.viewers.model.IInternalTreeModelViewer;
1820
import org.eclipse.debug.internal.ui.viewers.model.provisional.ModelDelta;
1921
import org.eclipse.debug.tests.viewer.model.TestModel.TestElement;
@@ -54,7 +56,7 @@ public void testSimpleSingleLevel() throws Exception {
5456
fViewer.setInput(model.getRootElement());
5557

5658
// Wait for the updates to complete.
57-
waitWhile(t -> !fListener.isFinished(), createListenerErrorMessage());
59+
waitWhile(() -> !fListener.isFinished(), createListenerErrorMessage());
5860

5961
model.validateData(fViewer, TreePath.EMPTY);
6062
}
@@ -70,7 +72,7 @@ public void testSimpleMultiLevel() throws Exception {
7072

7173
fViewer.setInput(model.getRootElement());
7274

73-
waitWhile(t -> !fListener.isFinished(), createListenerErrorMessage());
75+
waitWhile(() -> !fListener.isFinished(), createListenerErrorMessage());
7476

7577
model.validateData(fViewer, TreePath.EMPTY);
7678
}
@@ -85,7 +87,7 @@ public void testCheckReceiver() throws Exception {
8587
fListener.reset(TreePath.EMPTY, model.getRootElement(), -1, true, false);
8688
fViewer.setInput(model.getRootElement());
8789

88-
waitWhile(t -> !fListener.isFinished(), createListenerErrorMessage());
90+
waitWhile(() -> !fListener.isFinished(), createListenerErrorMessage());
8991
model.validateData(fViewer, TreePath.EMPTY);
9092

9193
TestElement element = model.getRootElement().getChildren()[0];
@@ -96,7 +98,7 @@ public void testCheckReceiver() throws Exception {
9698
fListener.reset(elementPath, element, -1, true, false);
9799
model.postDelta(delta);
98100

99-
waitWhile(t -> !fListener.isFinished(ITestModelUpdatesListenerConstants.MODEL_CHANGED_COMPLETE), createListenerErrorMessage());
101+
waitWhile(() -> !fListener.isFinished(ITestModelUpdatesListenerConstants.MODEL_CHANGED_COMPLETE), createListenerErrorMessage());
100102

101103
Assert.assertTrue(element.getChecked() != initialCheckState);
102104
}
@@ -113,7 +115,7 @@ public void testUpdateCheck() throws Exception {
113115

114116
// Set the input into the view and update the view.
115117
fViewer.setInput(model.getRootElement());
116-
waitWhile(t -> !fListener.isFinished(), createListenerErrorMessage());
118+
waitWhile(() -> !fListener.isFinished(), createListenerErrorMessage());
117119
model.validateData(fViewer, TreePath.EMPTY);
118120

119121
// Update the model
@@ -124,7 +126,7 @@ public void testUpdateCheck() throws Exception {
124126

125127
fListener.reset(elementPath, element, -1, true, false);
126128
model.postDelta(delta);
127-
waitWhile(t -> !fListener.isFinished(ITestModelUpdatesListenerConstants.LABEL_COMPLETE | ITestModelUpdatesListenerConstants.MODEL_CHANGED_COMPLETE), createListenerErrorMessage());
129+
waitWhile(() -> !fListener.isFinished(ITestModelUpdatesListenerConstants.LABEL_COMPLETE | ITestModelUpdatesListenerConstants.MODEL_CHANGED_COMPLETE), createListenerErrorMessage());
128130
model.validateData(fViewer, TreePath.EMPTY);
129131
}
130132
}

0 commit comments

Comments
 (0)