Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
*******************************************************************************/
package org.eclipse.debug.tests;

import java.util.function.Function;
import java.util.function.Predicate;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
Expand Down Expand Up @@ -82,24 +79,6 @@ public IStatus runInUIThread(IProgressMonitor monitor) {
}
}

/**
* Waits while given condition is {@code true} for some time. If the actual
* wait time exceeds {@link TestUtil#DEFAULT_TIMEOUT} and condition will be still
* {@code true}, throws {@link junit.framework.AssertionFailedError} with
* given message.
* <p>
* Will process UI events while waiting in UI thread, if called from
* background thread, just waits.
*
* @param condition function which will be evaluated while waiting
* @param errorMessage message which will be used to construct the failure
* exception in case the condition will still return {@code true}
* after given timeout
*/
public void waitWhile(Predicate<AbstractDebugTest> condition, Function<AbstractDebugTest, String> errorMessage) throws Exception {
TestUtil.waitWhile(condition, this, errorMessage);
}

private static void closeIntro(final IWorkbench wb) {
IWorkbenchWindow window = wb.getActiveWorkbenchWindow();
if (window != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.function.BooleanSupplier;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
Expand Down Expand Up @@ -121,18 +122,16 @@ public static void processUIEvents(final long millis) throws Exception {
* Will process UI events while waiting in UI thread, if called from
* background thread, just waits.
*
* @param <T> type of the context
* @param condition function which will be evaluated while waiting
* @param context test context
* @param timeout max wait time in milliseconds to wait on given condition
* @param errorMessage message which will be used to construct the failure
* exception in case the condition will still return {@code true}
* after given timeout
*/
public static <T> void waitWhile(Predicate<T> condition, T context, long timeout, Function<T, String> errorMessage) throws Exception {
public static void waitWhile(BooleanSupplier condition, long timeout, Supplier<String> errorMessage) throws Exception {
long start = System.currentTimeMillis();
Display display = Display.getCurrent();
while (System.currentTimeMillis() - start < timeout && condition.test(context)) {
while (System.currentTimeMillis() - start < timeout && condition.getAsBoolean()) {
if (display != null && !display.isDisposed()) {
if (!display.readAndDispatch()) {
Thread.sleep(0);
Expand All @@ -141,9 +140,9 @@ public static <T> void waitWhile(Predicate<T> condition, T context, long timeout
Thread.sleep(5);
}
}
Boolean stillTrue = condition.test(context);
Boolean stillTrue = condition.getAsBoolean();
if (stillTrue) {
fail(errorMessage.apply(context));
fail(errorMessage.get());
}
}

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,45 +86,45 @@ public void testBug424561_undoRedoUndoGone() throws Exception {
IUndoContext context = DebugUITools.getBreakpointsUndoContext();

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

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

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

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

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

final BreakpointsView finalView = view;
final TestBreakpoint finalBp = bp;
TestUtil.waitWhile(c -> {
TestUtil.waitWhile(() -> {
TreeItem item = (TreeItem) finalView.getTreeModelViewer().testFindItem(finalBp);
return item == null || item.getText() == null || !item.getText().contains(content);
}, this, c -> "Breakpoint not restored in view");
}, () -> "Breakpoint not restored in view");
} finally {
if (!viewVisible) {
DebugUIPlugin.getActiveWorkbenchWindow().getActivePage().hideView(view);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import static java.nio.file.Files.readAllBytes;
import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.debug.tests.TestUtil.waitWhile;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

Expand All @@ -35,8 +36,8 @@
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.BooleanSupplier;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

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

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

Predicate<AbstractDebugTest> waitForLastLineWritten = __ -> {
BooleanSupplier waitForLastLineWritten = () -> {
try {
TestUtil.processUIEvents(50);
} catch (Exception e) {
// try again
}
return console.getDocument().getNumberOfLines() < lines.length;
};
Function<AbstractDebugTest, String> errorMessageProvider = __ -> {
Supplier<String> errorMessageProvider = () -> {
String expected = String.join(System.lineSeparator(), lines);
String actual = console.getDocument().get();
return "Not all lines have been written, expected: " + expected + ", was: " + actual;
Expand Down Expand Up @@ -503,7 +504,7 @@ public void testBinaryOutputToFile() throws Exception {
try {
console.initialize();

Predicate<AbstractDebugTest> waitForFileWritten = __ -> {
BooleanSupplier waitForFileWritten = () -> {
try {
TestUtil.processUIEvents(20);
return readAllBytes(outFile.toPath()).length < output.length;
Expand All @@ -512,7 +513,7 @@ public void testBinaryOutputToFile() throws Exception {
}
return false;
};
Function<AbstractDebugTest, String> errorMessageProvider = __ -> {
Supplier<String> errorMessageProvider = () -> {
byte[] actualOutput = new byte[0];
try {
actualOutput = readAllBytes(outFile.toPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void testProcessTerminated() throws Exception {
mockProcess.setExitValue(1);
mockProcess.destroy();

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

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

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

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

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

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

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.eclipse.debug.tests.launching;

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.debug.tests.TestUtil.waitWhile;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
Expand Down Expand Up @@ -1306,8 +1307,7 @@ public void launchesTerminated(ILaunch[] launches) {
IProcess process = null;
try {
process = DebugPlugin.newProcess(launch, new MockProcess(0), "test-terminate-timestamp");
waitWhile(__ -> !terminatedLaunches.contains(launch),
__ -> "Launch termination event did not occur: "+
waitWhile(() -> !terminatedLaunches.contains(launch), () -> "Launch termination event did not occur: " +
"launch termination state is \"" + launch.isTerminated() + "\" " +
"and " + terminatedLaunches.size() + " launches have terminated");
String launchTerminateTimestampUntyped = launch.getAttribute(DebugPlugin.ATTR_TERMINATE_TIMESTAMP);
Expand Down Expand Up @@ -1454,7 +1454,7 @@ public void addProcess(IProcess process) {
IProcess runtimeProcess = null;
try {
runtimeProcess = DebugPlugin.newProcess(launch, mockProcess, "test-terminate-launch-listener");
waitWhile(__ -> !launchTerminated.get(), __ -> "Launch termination event did not occur");
waitWhile(() -> !launchTerminated.get(), () -> "Launch termination event did not occur");
} finally {
DebugPlugin.getDefault().getLaunchManager().removeLaunchListener(listener);
if (launch != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*******************************************************************************/
package org.eclipse.debug.tests.viewer.model;

import java.util.function.Function;
import java.util.function.Supplier;

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

abstract protected TestModelUpdatesListener createListener(IInternalTreeModelViewer viewer);

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*******************************************************************************/
package org.eclipse.debug.tests.viewer.model;

import static org.eclipse.debug.tests.TestUtil.waitWhile;

import org.eclipse.debug.internal.ui.viewers.model.IInternalTreeModelViewer;
import org.eclipse.debug.internal.ui.viewers.model.provisional.ModelDelta;
import org.eclipse.debug.tests.viewer.model.TestModel.TestElement;
Expand Down Expand Up @@ -54,7 +56,7 @@ public void testSimpleSingleLevel() throws Exception {
fViewer.setInput(model.getRootElement());

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

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

fViewer.setInput(model.getRootElement());

waitWhile(t -> !fListener.isFinished(), createListenerErrorMessage());
waitWhile(() -> !fListener.isFinished(), createListenerErrorMessage());

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

waitWhile(t -> !fListener.isFinished(), createListenerErrorMessage());
waitWhile(() -> !fListener.isFinished(), createListenerErrorMessage());
model.validateData(fViewer, TreePath.EMPTY);

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

waitWhile(t -> !fListener.isFinished(ITestModelUpdatesListenerConstants.MODEL_CHANGED_COMPLETE), createListenerErrorMessage());
waitWhile(() -> !fListener.isFinished(ITestModelUpdatesListenerConstants.MODEL_CHANGED_COMPLETE), createListenerErrorMessage());

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

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

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

fListener.reset(elementPath, element, -1, true, false);
model.postDelta(delta);
waitWhile(t -> !fListener.isFinished(ITestModelUpdatesListenerConstants.LABEL_COMPLETE | ITestModelUpdatesListenerConstants.MODEL_CHANGED_COMPLETE), createListenerErrorMessage());
waitWhile(() -> !fListener.isFinished(ITestModelUpdatesListenerConstants.LABEL_COMPLETE | ITestModelUpdatesListenerConstants.MODEL_CHANGED_COMPLETE), createListenerErrorMessage());
model.validateData(fViewer, TreePath.EMPTY);
}
}
Loading
Loading