Skip to content

Commit

Permalink
Replace InfiniteLoop with RunForASecond
Browse files Browse the repository at this point in the history
RunForASecond finally stops and therefore avoids that we create
background threads that run forever.
  • Loading branch information
stefanbirkner committed Jan 20, 2021
1 parent 2db6394 commit 527f3a3
Showing 1 changed file with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ public void noExceptionIsThrownWhenWrappedStatementFinishesBeforeTimeoutWithoutT
public void throwsTestTimedOutException() {
assertThrows(
TestTimedOutException.class,
run(failAfter50Ms(new InfiniteLoop())));
run(failAfter50Ms(new RunForASecond())));
}

@Test
public void throwExceptionWithNiceMessageOnTimeout() {
Exception e = assertThrows(
Exception.class,
run(failAfter50Ms(new InfiniteLoop())));
run(failAfter50Ms(new RunForASecond())));
assertEquals("test timed out after 50 milliseconds", e.getMessage());
}

Expand All @@ -87,7 +87,7 @@ public void throwExceptionIfTheSecondCallToEvaluateNeedsTooMuchTime()
statement.delegate = new FastStatement();
failOnTimeout.evaluate();

statement.delegate = new InfiniteLoop();
statement.delegate = new RunForASecond();
assertThrows(
TestTimedOutException.class,
run(failOnTimeout));
Expand All @@ -104,7 +104,7 @@ public void throwTimeoutExceptionOnSecondCallAlthoughFirstCallThrowsException()
run(failOnTimeout)
);

statement.delegate = new InfiniteLoop();
statement.delegate = new RunForASecond();
assertThrows(
TestTimedOutException.class,
run(failOnTimeout));
Expand All @@ -114,24 +114,24 @@ public void throwTimeoutExceptionOnSecondCallAlthoughFirstCallThrowsException()
public void throwsExceptionWithTimeoutValueAndTimeUnitSet() {
TestTimedOutException e = assertThrows(
TestTimedOutException.class,
run(failAfter50Ms(new InfiniteLoop())));
run(failAfter50Ms(new RunForASecond())));
assertEquals(50, e.getTimeout());
assertEquals(MILLISECONDS, e.getTimeUnit());
}

@Test
public void stopEndlessStatement() throws Throwable {
InfiniteLoop infiniteLoop = new InfiniteLoop();
RunForASecond runForASecond = new RunForASecond();
assertThrows(
TestTimedOutException.class,
run(failAfter50Ms(infiniteLoop)));
run(failAfter50Ms(runForASecond)));

sleep(20); // time to interrupt the thread
infiniteLoop.stillExecuting.set(false);
runForASecond.stillExecuting.set(false);
sleep(20); // time to increment the count
assertFalse(
"Thread has not been stopped.",
infiniteLoop.stillExecuting.get());
runForASecond.stillExecuting.get());
}

@Test
Expand Down Expand Up @@ -262,12 +262,13 @@ public void evaluate() throws Throwable {
}
}

private static final class InfiniteLoop extends Statement {
private static final class RunForASecond extends Statement {
final AtomicBoolean stillExecuting = new AtomicBoolean();

@Override
public void evaluate() throws Throwable {
while (true) {
long timeout = currentTimeMillis() + 1000L;
while (currentTimeMillis() < timeout) {
sleep(10); // sleep in order to enable interrupting thread
stillExecuting.set(true);
}
Expand Down

0 comments on commit 527f3a3

Please sign in to comment.