@@ -58,14 +58,14 @@ public void noExceptionIsThrownWhenWrappedStatementFinishesBeforeTimeoutWithoutT
5858 public void throwsTestTimedOutException () {
5959 assertThrows (
6060 TestTimedOutException .class ,
61- run (failAfter50Ms (new InfiniteLoop ())));
61+ run (failAfter50Ms (new RunForASecond ())));
6262 }
6363
6464 @ Test
6565 public void throwExceptionWithNiceMessageOnTimeout () {
6666 Exception e = assertThrows (
6767 Exception .class ,
68- run (failAfter50Ms (new InfiniteLoop ())));
68+ run (failAfter50Ms (new RunForASecond ())));
6969 assertEquals ("test timed out after 50 milliseconds" , e .getMessage ());
7070 }
7171
@@ -87,7 +87,7 @@ public void throwExceptionIfTheSecondCallToEvaluateNeedsTooMuchTime()
8787 statement .delegate = new FastStatement ();
8888 failOnTimeout .evaluate ();
8989
90- statement .delegate = new InfiniteLoop ();
90+ statement .delegate = new RunForASecond ();
9191 assertThrows (
9292 TestTimedOutException .class ,
9393 run (failOnTimeout ));
@@ -104,7 +104,7 @@ public void throwTimeoutExceptionOnSecondCallAlthoughFirstCallThrowsException()
104104 run (failOnTimeout )
105105 );
106106
107- statement .delegate = new InfiniteLoop ();
107+ statement .delegate = new RunForASecond ();
108108 assertThrows (
109109 TestTimedOutException .class ,
110110 run (failOnTimeout ));
@@ -114,24 +114,24 @@ public void throwTimeoutExceptionOnSecondCallAlthoughFirstCallThrowsException()
114114 public void throwsExceptionWithTimeoutValueAndTimeUnitSet () {
115115 TestTimedOutException e = assertThrows (
116116 TestTimedOutException .class ,
117- run (failAfter50Ms (new InfiniteLoop ())));
117+ run (failAfter50Ms (new RunForASecond ())));
118118 assertEquals (50 , e .getTimeout ());
119119 assertEquals (MILLISECONDS , e .getTimeUnit ());
120120 }
121121
122122 @ Test
123123 public void stopEndlessStatement () throws Throwable {
124- InfiniteLoop infiniteLoop = new InfiniteLoop ();
124+ RunForASecond runForASecond = new RunForASecond ();
125125 assertThrows (
126126 TestTimedOutException .class ,
127- run (failAfter50Ms (infiniteLoop )));
127+ run (failAfter50Ms (runForASecond )));
128128
129129 sleep (20 ); // time to interrupt the thread
130- infiniteLoop .stillExecuting .set (false );
130+ runForASecond .stillExecuting .set (false );
131131 sleep (20 ); // time to increment the count
132132 assertFalse (
133133 "Thread has not been stopped." ,
134- infiniteLoop .stillExecuting .get ());
134+ runForASecond .stillExecuting .get ());
135135 }
136136
137137 @ Test
@@ -262,12 +262,13 @@ public void evaluate() throws Throwable {
262262 }
263263 }
264264
265- private static final class InfiniteLoop extends Statement {
265+ private static final class RunForASecond extends Statement {
266266 final AtomicBoolean stillExecuting = new AtomicBoolean ();
267267
268268 @ Override
269269 public void evaluate () throws Throwable {
270- while (true ) {
270+ long timeout = currentTimeMillis () + 1000L ;
271+ while (currentTimeMillis () < timeout ) {
271272 sleep (10 ); // sleep in order to enable interrupting thread
272273 stillExecuting .set (true );
273274 }
0 commit comments