13
13
*******************************************************************************/
14
14
package org .eclipse .debug .tests .console ;
15
15
16
+ import static org .hamcrest .MatcherAssert .assertThat ;
17
+ import static org .hamcrest .Matchers .is ;
16
18
import static org .junit .Assert .assertArrayEquals ;
17
19
import static org .junit .Assert .assertEquals ;
18
20
import static org .junit .Assert .assertTrue ;
27
29
import java .nio .file .Files ;
28
30
import java .text .MessageFormat ;
29
31
import java .util .ArrayList ;
32
+ import java .util .Arrays ;
30
33
import java .util .Collections ;
31
34
import java .util .HashMap ;
32
35
import java .util .Map ;
33
36
import java .util .concurrent .TimeUnit ;
34
37
import java .util .concurrent .atomic .AtomicBoolean ;
35
38
import java .util .concurrent .atomic .AtomicInteger ;
36
-
39
+ import java .util .function .Function ;
40
+ import java .util .function .Predicate ;
37
41
import org .eclipse .core .runtime .ILogListener ;
38
42
import org .eclipse .core .runtime .IStatus ;
39
43
import org .eclipse .core .runtime .Platform ;
@@ -440,7 +444,21 @@ public void testOutput() throws Exception {
440
444
sysout .println (lines [4 ]);
441
445
mockProcess .destroy ();
442
446
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 );
444
462
445
463
for (int i = 0 ; i < lines .length ; i ++) {
446
464
IRegion lineInfo = console .getDocument ().getLineInformation (i );
@@ -478,7 +496,26 @@ public void testBinaryOutputToFile() throws Exception {
478
496
final org .eclipse .debug .internal .ui .views .console .ProcessConsole console = new org .eclipse .debug .internal .ui .views .console .ProcessConsole (process , new ConsoleColorProvider (), consoleEncoding );
479
497
try {
480
498
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 );
482
519
mockProcess .destroy ();
483
520
} finally {
484
521
console .destroy ();
@@ -488,7 +525,7 @@ public void testBinaryOutputToFile() throws Exception {
488
525
}
489
526
490
527
byte [] receivedOutput = Files .readAllBytes (outFile .toPath ());
491
- assertArrayEquals ( output , receivedOutput );
528
+ assertThat ( "unexpected output" , receivedOutput , is ( output ) );
492
529
}
493
530
494
531
/**
0 commit comments