1313 *******************************************************************************/
1414package org .eclipse .core .tests .runtime .jobs ;
1515
16+ import static org .hamcrest .MatcherAssert .assertThat ;
17+ import static org .hamcrest .Matchers .lessThan ;
1618import static org .junit .Assert .assertTrue ;
1719
1820import java .lang .management .ManagementFactory ;
1921import java .lang .management .ThreadInfo ;
22+ import java .time .Duration ;
2023import java .util .ArrayList ;
2124import java .util .Collection ;
2225import java .util .Collections ;
@@ -311,7 +314,7 @@ public boolean aboutToWait(Thread lockOwner) {
311314
312315 // the underlying array has to be empty
313316 assertTrue ("Locks not removed from graph." , manager .isEmpty ());
314- errors .forEach (e -> e . printStackTrace () );
317+ errors .forEach (Throwable :: printStackTrace );
315318 assertTrue ("Error happend: " + errors .stream ().map (e -> "" + e ).collect (Collectors .joining (", " )),
316319 errors .isEmpty ());
317320 }
@@ -328,20 +331,24 @@ private void execute(ArrayList<LockAcquiringRunnable> allRunnables) {
328331 thread .start ();
329332 }
330333 randomOrder .waitForEnd ();
331- long maxNano = System .nanoTime () + 5000 * 1_000_000 ;
334+ Duration timeoutTime = Duration . ofMillis ( System .currentTimeMillis ()). plusSeconds ( 5 ) ;
332335 for (Thread thread : threads ) {
333336 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 ());
342339 } catch (InterruptedException e ) {
343- throw new IllegalStateException ("interrupted" );
340+ throw new IllegalStateException (e );
344341 }
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 ()));
345352 }
346353 }
347354
0 commit comments