Skip to content

Commit 059eac5

Browse files
committed
Improve timeout handling for OrderedLockTest #1057
* Replace nanoTime handling with comprehensible Duration objects * Separate Thread.isAlive() check from timeout calculation to distinguish failure cases Closes #1057
1 parent 2a6c3f7 commit 059eac5

File tree

1 file changed

+18
-11
lines changed
  • runtime/tests/org.eclipse.core.tests.runtime/src/org/eclipse/core/tests/runtime/jobs

1 file changed

+18
-11
lines changed

runtime/tests/org.eclipse.core.tests.runtime/src/org/eclipse/core/tests/runtime/jobs/OrderedLockTest.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
*******************************************************************************/
1414
package org.eclipse.core.tests.runtime.jobs;
1515

16+
import static org.hamcrest.MatcherAssert.assertThat;
17+
import static org.hamcrest.Matchers.lessThan;
1618
import static org.junit.Assert.assertTrue;
1719

1820
import java.lang.management.ManagementFactory;
1921
import java.lang.management.ThreadInfo;
22+
import java.time.Duration;
2023
import java.util.ArrayList;
2124
import java.util.Collection;
2225
import 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

Comments
 (0)