Skip to content

Commit bb9ed99

Browse files
committed
Fix random failing ProcessConsoleTests #770
Test test methods testOutput() and testBinaryOutputToFile() in ProcessConsoleTests randomly fail. One possible reason is the short time waiting for a process to finish output and the fixed-time waiting for UI events to be processed. This change replaces both conditions by wait operations that check for the actual conditions to be fulfilled or a timeout to occur. This will also improve performance of these tests. Fixes #770
1 parent 508558d commit bb9ed99

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

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

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

16+
import static org.hamcrest.MatcherAssert.assertThat;
17+
import static org.hamcrest.Matchers.is;
1618
import static org.junit.Assert.assertArrayEquals;
1719
import static org.junit.Assert.assertEquals;
1820
import static org.junit.Assert.assertTrue;
@@ -27,13 +29,15 @@
2729
import java.nio.file.Files;
2830
import java.text.MessageFormat;
2931
import java.util.ArrayList;
32+
import java.util.Arrays;
3033
import java.util.Collections;
3134
import java.util.HashMap;
3235
import java.util.Map;
3336
import java.util.concurrent.TimeUnit;
3437
import java.util.concurrent.atomic.AtomicBoolean;
3538
import java.util.concurrent.atomic.AtomicInteger;
36-
39+
import java.util.function.Function;
40+
import java.util.function.Predicate;
3741
import org.eclipse.core.runtime.ILogListener;
3842
import org.eclipse.core.runtime.IStatus;
3943
import org.eclipse.core.runtime.Platform;
@@ -440,7 +444,21 @@ public void testOutput() throws Exception {
440444
sysout.println(lines[4]);
441445
mockProcess.destroy();
442446
sysout.close();
443-
TestUtil.processUIEvents(200);
447+
448+
Predicate<AbstractDebugTest> waitForLastLineWritten = __ -> {
449+
try {
450+
TestUtil.processUIEvents(50);
451+
} catch (Exception e) {
452+
// try again
453+
}
454+
return console.getDocument().getNumberOfLines() < lines.length;
455+
};
456+
Function<AbstractDebugTest, String> errorMessageProvider = __ -> {
457+
String expected = String.join(System.lineSeparator(), lines);
458+
String actual = console.getDocument().get();
459+
return "Not all lines have been written, expected: " + expected + ", was: " + actual;
460+
};
461+
waitWhile(waitForLastLineWritten, testTimeout, errorMessageProvider);
444462

445463
for (int i = 0; i < lines.length; i++) {
446464
IRegion lineInfo = console.getDocument().getLineInformation(i);
@@ -478,7 +496,26 @@ public void testBinaryOutputToFile() throws Exception {
478496
final org.eclipse.debug.internal.ui.views.console.ProcessConsole console = new org.eclipse.debug.internal.ui.views.console.ProcessConsole(process, new ConsoleColorProvider(), consoleEncoding);
479497
try {
480498
console.initialize();
481-
mockProcess.waitFor(100, TimeUnit.MILLISECONDS);
499+
500+
Predicate<AbstractDebugTest> waitForFileWritten = __ -> {
501+
try {
502+
TestUtil.processUIEvents(20);
503+
return Files.readAllBytes(outFile.toPath()).length < output.length;
504+
} catch (Exception e) {
505+
// try again
506+
}
507+
return false;
508+
};
509+
Function<AbstractDebugTest, String> errorMessageProvider = __ -> {
510+
byte[] actualOutput = new byte[0];
511+
try {
512+
actualOutput = Files.readAllBytes(outFile.toPath());
513+
} catch (IOException e) {
514+
// Proceed as if output was empty
515+
}
516+
return "File has not been written, expected: " + Arrays.toString(output) + ", was: " + Arrays.toString(actualOutput);
517+
};
518+
waitWhile(waitForFileWritten, testTimeout, errorMessageProvider);
482519
mockProcess.destroy();
483520
} finally {
484521
console.destroy();
@@ -488,7 +525,7 @@ public void testBinaryOutputToFile() throws Exception {
488525
}
489526

490527
byte[] receivedOutput = Files.readAllBytes(outFile.toPath());
491-
assertArrayEquals(output, receivedOutput);
528+
assertThat("unexpected output", receivedOutput, is(output));
492529
}
493530

494531
/**

0 commit comments

Comments
 (0)