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 @@ -36,11 +36,6 @@ public class AbstractDebugTest {

private static boolean welcomeClosed;

/**
* Default timeout in milliseconds to wait on some events
*/
protected long testTimeout = 30000;

@Rule
public TestName name = new TestName();

Expand Down Expand Up @@ -87,28 +82,9 @@ public IStatus runInUIThread(IProgressMonitor monitor) {
}
}

/**
* Waits while given condition is {@code true} for a given amount of
* milliseconds. If the actual wait time exceeds given 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 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 void waitWhile(Predicate<AbstractDebugTest> condition, long timeout, Function<AbstractDebugTest, String> errorMessage) throws Exception {
TestUtil.waitWhile(condition, this, timeout, errorMessage);
}

/**
* Waits while given condition is {@code true} for some time. If the actual
* wait time exceeds {@link #testTimeout} and condition will be still
* wait time exceeds {@link TestUtil#DEFAULT_TIMEOUT} and condition will be still
* {@code true}, throws {@link junit.framework.AssertionFailedError} with
* given message.
* <p>
Expand All @@ -121,7 +97,7 @@ public void waitWhile(Predicate<AbstractDebugTest> condition, long timeout, Func
* after given timeout
*/
public void waitWhile(Predicate<AbstractDebugTest> condition, Function<AbstractDebugTest, String> errorMessage) throws Exception {
TestUtil.waitWhile(condition, this, testTimeout, errorMessage);
TestUtil.waitWhile(condition, this, errorMessage);
}

private static void closeIntro(final IWorkbench wb) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@
import org.osgi.framework.FrameworkUtil;


public class TestUtil {
public final class TestUtil {

/**
* The default test timeout in milliseconds
*/
public static long DEFAULT_TIMEOUT = 30_000;

private TestUtil() {
}

/**
* Call this in the tearDown method of every test to clean up state that can
Expand Down Expand Up @@ -139,6 +147,26 @@ public static <T> void waitWhile(Predicate<T> condition, T context, long timeout
}
}

/**
* Waits while given condition is {@code true} for the time defined as
* {@code #TEST_TIMEOUT}. If the actual wait time exceeds that timeout and
* condition will be still {@code true}, throws {@link AssertionError} with
* given message.
* <p>
* 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);
}

/**
* A simplified variant of
* {@link #waitWhile(Predicate, Object, long, Function)}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

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

Expand Down Expand Up @@ -85,34 +86,34 @@ public void testBug424561_undoRedoUndoGone() throws Exception {
IUndoContext context = DebugUITools.getBreakpointsUndoContext();

bpm.addBreakpoint(bp);
TestUtil.waitWhile(c -> c.getTestBreakpoints().isEmpty(), this, testTimeout, c -> "Breakpoint is not created");
TestUtil.waitWhile(c -> c.getTestBreakpoints().isEmpty(), this, c -> "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, testTimeout, c -> "Breakpoint is not deleted");
TestUtil.waitWhile(c -> !c.getTestBreakpoints().isEmpty(), this, c -> "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, testTimeout, c -> "Breakpoint is not recreated");
TestUtil.waitWhile(c -> c.getTestBreakpoints().isEmpty(), this, c -> "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, testTimeout, c -> "Breakpoint is not deleted");
TestUtil.waitWhile(c -> !c.getTestBreakpoints().isEmpty(), this, c -> "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, testTimeout, c -> "Breakpoint is not recreated");
TestUtil.waitWhile(c -> c.getTestBreakpoints().isEmpty(), this, c -> "Breakpoint is not recreated");
bp = getTestBreakpoints().get(0);
assertEquals("Breakpoint attributes not correctly restored", content, bp.getText());
assertTrue("Breakpoint marker missing", bp.getMarker().exists());
Expand All @@ -123,7 +124,7 @@ public void testBug424561_undoRedoUndoGone() throws Exception {
TestUtil.waitWhile(c -> {
TreeItem item = (TreeItem) finalView.getTreeModelViewer().testFindItem(finalBp);
return item == null || item.getText() == null || !item.getText().contains(content);
}, this, testTimeout, c -> "Breakpoint not restored in view");
}, this, c -> "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 @@ -902,7 +902,7 @@ protected IStatus run(IProgressMonitor monitor) {
};
Thread watchdog = new Thread(() -> {
try {
Thread.sleep(testTimeout);
Thread.sleep(TestUtil.DEFAULT_TIMEOUT);
synchronized (c) {
c.notifyAll();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public void testUTF8InputOdd() throws Exception {
*/
public void processConsoleUTF8Input(String prefix, int numTwoByteCharacters) throws Exception {
final String input = prefix + String.join("", Collections.nCopies(numTwoByteCharacters, "\u00F8"));
final MockProcess mockProcess = new MockProcess(input.getBytes(StandardCharsets.UTF_8).length, testTimeout);
final MockProcess mockProcess = new MockProcess(input.getBytes(StandardCharsets.UTF_8).length, TestUtil.DEFAULT_TIMEOUT);
try {
final ILaunch launch = new Launch(null, ILaunchManager.RUN_MODE, null);
launch.setAttribute(DebugPlugin.ATTR_CONSOLE_ENCODING, StandardCharsets.UTF_8.toString());
Expand All @@ -198,7 +198,7 @@ public void processConsoleUTF8Input(String prefix, int numTwoByteCharacters) thr
@SuppressWarnings("resource")
IOConsoleInputStream consoleIn = console.getInputStream();
consoleIn.appendData(input);
mockProcess.waitFor(testTimeout, TimeUnit.MILLISECONDS);
mockProcess.waitFor(TestUtil.DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
} finally {
console.destroy();
}
Expand Down Expand Up @@ -296,7 +296,7 @@ public void processTerminationTest(ILaunchConfiguration launchConfig, boolean te
if (mockProcess.isAlive()) {
mockProcess.destroy();
}
waitWhile(__ -> !terminationSignaled.get(), 10_000, __ -> "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 +395,7 @@ private IOConsole doConsoleOutputTest(byte[] testContent, Map<String, Object> la
try {
consoleManager.addConsoles(new IConsole[] { console });
mockProcess.destroy();
waitWhile(c -> !consoleFinished.get(), testTimeout, c -> "Console did not finished.");
waitWhile(c -> !consoleFinished.get(), c -> "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 @@ -465,7 +465,7 @@ public void testOutput() throws Exception {
String actual = console.getDocument().get();
return "Not all lines have been written, expected: " + expected + ", was: " + actual;
};
waitWhile(waitForLastLineWritten, testTimeout, errorMessageProvider);
waitWhile(waitForLastLineWritten, errorMessageProvider);

for (int i = 0; i < lines.length; i++) {
IRegion lineInfo = console.getDocument().getLineInformation(i);
Expand Down Expand Up @@ -521,7 +521,7 @@ public void testBinaryOutputToFile() throws Exception {
}
return "File has not been written, expected: " + Arrays.toString(output) + ", was: " + Arrays.toString(actualOutput);
};
waitWhile(waitForFileWritten, testTimeout, errorMessageProvider);
waitWhile(waitForFileWritten, errorMessageProvider);
mockProcess.destroy();
} finally {
console.destroy();
Expand All @@ -546,7 +546,7 @@ public void testBinaryInputFromFile() throws Exception {

final File inFile = createTmpFile("testinput.bin");
Files.write(inFile.toPath(), input);
final MockProcess mockProcess = new MockProcess(input.length, testTimeout);
final MockProcess mockProcess = new MockProcess(input.length, TestUtil.DEFAULT_TIMEOUT);
try {
Map<String, Object> launchConfigAttributes = new HashMap<>();
launchConfigAttributes.put(DebugPlugin.ATTR_CONSOLE_ENCODING, consoleEncoding);
Expand All @@ -556,7 +556,7 @@ public void testBinaryInputFromFile() throws Exception {
final org.eclipse.debug.internal.ui.views.console.ProcessConsole console = new org.eclipse.debug.internal.ui.views.console.ProcessConsole(process, new ConsoleColorProvider(), consoleEncoding);
try {
console.initialize();
mockProcess.waitFor(testTimeout, TimeUnit.MILLISECONDS);
mockProcess.waitFor(TestUtil.DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
} finally {
console.destroy();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public void testProcessTerminated() throws Exception {
mockProcess.setExitValue(1);
mockProcess.destroy();

TestUtil.waitWhile(p -> !p.isTerminated(), runtimeProcess, testTimeout, p -> "RuntimeProcess not terminated.");
TestUtil.waitForJobs(name.getMethodName(), 25, testTimeout);
TestUtil.waitWhile(p -> !p.isTerminated(), runtimeProcess, p -> "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 All @@ -92,8 +92,8 @@ public void testTerminateProcess() throws Exception {
runtimeProcess.terminate();
assertFalse("RuntimeProcess failed to terminate wrapped process.", mockProcess.isAlive());

TestUtil.waitWhile(p -> !p.isTerminated(), runtimeProcess, testTimeout, p -> "RuntimeProcess not terminated.");
TestUtil.waitForJobs(name.getMethodName(), 25, testTimeout);
TestUtil.waitWhile(p -> !p.isTerminated(), runtimeProcess, p -> "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, testTimeout, p -> "RuntimeProcess not terminated.");
TestUtil.waitWhile(p -> !p.isTerminated(), runtimeProcess, p -> "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, testTimeout, p -> "RuntimeProcess not terminated.");
TestUtil.waitWhile(p -> !p.isTerminated(), runtimeProcess, p -> "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, testTimeout, p -> "RuntimeProcess not terminated.");
TestUtil.waitWhile(p -> !p.isTerminated(), runtimeProcess, p -> "RuntimeProcess not terminated.");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1306,7 +1306,7 @@ public void launchesTerminated(ILaunch[] launches) {
IProcess process = null;
try {
process = DebugPlugin.newProcess(launch, new MockProcess(0), "test-terminate-timestamp");
waitWhile(__ -> !terminatedLaunches.contains(launch), testTimeout,
waitWhile(__ -> !terminatedLaunches.contains(launch),
__ -> "Launch termination event did not occur: "+
"launch termination state is \"" + launch.isTerminated() + "\" " +
"and " + terminatedLaunches.size() + " launches have terminated");
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(), testTimeout, __ -> "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 @@ -468,7 +468,7 @@ public void testCanceledUpdates3() throws Exception {
long start = System.currentTimeMillis();
// Wait for the delta to be processed.
while (!fListener.isFinished(MODEL_CHANGED_COMPLETE | CHILDREN_UPDATES_STARTED)
&& System.currentTimeMillis() - start < testTimeout) {
&& System.currentTimeMillis() - start < TestUtil.DEFAULT_TIMEOUT) {
completeQueuedUpdatesOfType(model, IChildrenCountUpdate.class);
completeQueuedUpdatesOfType(model, IHasChildrenUpdate.class);
TestUtil.processUIEvents();
Expand Down Expand Up @@ -511,7 +511,7 @@ public void testCanceledUpdates4() throws Exception {
long start = System.currentTimeMillis();
// Wait for the delta to be processed.
while (!fListener.isFinished(MODEL_CHANGED_COMPLETE | CHILDREN_UPDATES_STARTED)
&& System.currentTimeMillis() - start < testTimeout) {
&& System.currentTimeMillis() - start < TestUtil.DEFAULT_TIMEOUT) {
completeQueuedUpdatesOfType(model, IChildrenCountUpdate.class);
completeQueuedUpdatesOfType(model, IHasChildrenUpdate.class);
TestUtil.processUIEvents();
Expand Down
Loading