13
13
*******************************************************************************/
14
14
package org .eclipse .core .tests .runtime .jobs ;
15
15
16
+ import static org .hamcrest .MatcherAssert .assertThat ;
17
+ import static org .hamcrest .Matchers .lessThan ;
16
18
import static org .junit .Assert .assertTrue ;
17
19
18
20
import java .lang .management .ManagementFactory ;
19
21
import java .lang .management .ThreadInfo ;
22
+ import java .time .Duration ;
20
23
import java .util .ArrayList ;
21
24
import java .util .Collection ;
22
25
import java .util .Collections ;
@@ -311,7 +314,7 @@ public boolean aboutToWait(Thread lockOwner) {
311
314
312
315
// the underlying array has to be empty
313
316
assertTrue ("Locks not removed from graph." , manager .isEmpty ());
314
- errors .forEach (e -> e . printStackTrace () );
317
+ errors .forEach (Throwable :: printStackTrace );
315
318
assertTrue ("Error happend: " + errors .stream ().map (e -> "" + e ).collect (Collectors .joining (", " )),
316
319
errors .isEmpty ());
317
320
}
@@ -328,20 +331,24 @@ private void execute(ArrayList<LockAcquiringRunnable> allRunnables) {
328
331
thread .start ();
329
332
}
330
333
randomOrder .waitForEnd ();
331
- long maxNano = System .nanoTime () + 5000 * 1_000_000 ;
334
+ Duration timeoutTime = Duration . ofMillis ( System .currentTimeMillis ()). plusSeconds ( 5 ) ;
332
335
for (Thread thread : threads ) {
333
336
try {
334
- long joinMs = (maxNano - System .nanoTime ()) / 1_000_000 ;
335
- thread .join (Math .max (joinMs , 1 ));
336
- if (thread .isAlive () || joinMs < 0 ) {
337
- throw new IllegalStateException (
338
- "Threads did not end in time. All thread infos begin: ----\n " + getThreadDump ()
339
- + "---- All thread infos end.\n " );
340
-
341
- }
337
+ Duration remainingTime = timeoutTime .minusMillis (System .currentTimeMillis ());
338
+ thread .join (remainingTime .toMillis ());
342
339
} catch (InterruptedException e ) {
343
- throw new IllegalStateException ("interrupted" );
340
+ throw new IllegalStateException (e );
344
341
}
342
+ checkTimeout (timeoutTime );
343
+ assertThat ("thread is still alive although join did not timeout" , !thread .isAlive ());
344
+ }
345
+ }
346
+
347
+ public void checkTimeout (Duration timeoutTime ) {
348
+ Duration currentTime = Duration .ofMillis (System .currentTimeMillis ());
349
+ if (timeoutTime .minus (currentTime ).toMillis () <= 0 ) {
350
+ assertThat ("Threads did not end in time. All thread infos begin: ----\n " + getThreadDump ()
351
+ + "---- All thread infos end.\n " , currentTime .toMillis (), lessThan (timeoutTime .toMillis ()));
345
352
}
346
353
}
347
354
0 commit comments