8
8
import static java .util .concurrent .TimeUnit .MILLISECONDS ;
9
9
import static org .junit .Assert .assertEquals ;
10
10
import static org .junit .Assert .assertFalse ;
11
+ import static org .junit .Assert .assertNotSame ;
11
12
import static org .junit .Assert .assertSame ;
12
13
import static org .junit .Assert .assertThrows ;
13
14
import static org .junit .Assert .assertTrue ;
14
15
import static org .junit .Assert .fail ;
15
- import static org .junit .internal .runners .statements .FailOnTimeout .builder ;
16
+ import static org .junit .Assume .assumeFalse ;
17
+ import static org .junit .Assume .assumeTrue ;
16
18
19
+ import java .util .Arrays ;
17
20
import java .util .concurrent .TimeUnit ;
18
21
import java .util .concurrent .atomic .AtomicReference ;
19
22
20
23
import org .junit .Test ;
21
24
import org .junit .function .ThrowingRunnable ;
22
25
import org .junit .internal .runners .statements .Fail ;
26
+ import org .junit .runner .RunWith ;
27
+ import org .junit .runners .Parameterized ;
23
28
import org .junit .runners .model .Statement ;
24
29
import org .junit .runners .model .TestTimedOutException ;
25
30
26
31
27
32
/**
28
33
* @author Asaf Ary, Stefan Birkner
29
34
*/
35
+ @ RunWith (Parameterized .class )
30
36
public class FailOnTimeoutTest {
31
37
private static final long TIMEOUT = 100 ;
32
38
private static final long DURATION_THAT_EXCEEDS_TIMEOUT = 60 * 60 * 1000 ; //1 hour
33
39
34
40
private final TestStatement statement = new TestStatement ();
35
41
36
- private final FailOnTimeout failOnTimeout = builder ().withTimeout (TIMEOUT , MILLISECONDS ).build (statement );
42
+ private final boolean lookingForStuckThread ;
43
+ private final FailOnTimeout failOnTimeout ;
44
+
45
+ @ Parameterized .Parameters (name = "lookingForStuckThread = {0}" )
46
+ public static Iterable <Boolean > getParameters () {
47
+ return Arrays .asList (Boolean .TRUE , Boolean .FALSE );
48
+ }
49
+
50
+ public FailOnTimeoutTest (Boolean lookingForStuckThread ) {
51
+ this .lookingForStuckThread = lookingForStuckThread ;
52
+ this .failOnTimeout = builder ().withTimeout (TIMEOUT , MILLISECONDS ).build (statement );
53
+ }
54
+
55
+ private FailOnTimeout .Builder builder () {
56
+ return FailOnTimeout .builder ().withLookingForStuckThread (lookingForStuckThread );
57
+ }
37
58
38
59
@ Test
39
60
public void throwsTestTimedOutException () {
@@ -212,14 +233,17 @@ private void notTheRealCauseOfTheTimeout() {
212
233
}
213
234
214
235
@ Test
215
- public void threadGroupNotLeaked () throws Throwable {
236
+ public void lookingForStuckThread_threadGroupNotLeaked () throws Throwable {
237
+ assumeTrue (lookingForStuckThread );
216
238
final AtomicReference <ThreadGroup > innerThreadGroup = new AtomicReference <ThreadGroup >();
217
239
final AtomicReference <Thread > innerThread = new AtomicReference <Thread >();
240
+ final ThreadGroup outerThreadGroup = currentThread ().getThreadGroup ();
218
241
ThrowingRunnable runnable = evaluateWithDelegate (new Statement () {
219
242
@ Override
220
243
public void evaluate () {
221
244
innerThread .set (currentThread ());
222
245
ThreadGroup group = currentThread ().getThreadGroup ();
246
+ assertNotSame ("inner thread should use a different thread group" , outerThreadGroup , group );
223
247
innerThreadGroup .set (group );
224
248
assertTrue ("the 'FailOnTimeoutGroup' thread group should be a daemon thread group" , group .isDaemon ());
225
249
}
@@ -231,4 +255,19 @@ public void evaluate() {
231
255
innerThread .get ().join ();
232
256
assertTrue ("the 'FailOnTimeoutGroup' thread group should be destroyed after running the test" , innerThreadGroup .get ().isDestroyed ());
233
257
}
258
+
259
+ @ Test
260
+ public void notLookingForStuckThread_usesSameThreadGroup () throws Throwable {
261
+ assumeFalse (lookingForStuckThread );
262
+ final ThreadGroup outerThreadGroup = currentThread ().getThreadGroup ();
263
+ ThrowingRunnable runnable = evaluateWithDelegate (new Statement () {
264
+ @ Override
265
+ public void evaluate () {
266
+ ThreadGroup group = currentThread ().getThreadGroup ();
267
+ assertSame ("inner thread should use the same thread group" , outerThreadGroup , group );
268
+ }
269
+ });
270
+
271
+ runnable .run ();
272
+ }
234
273
}
0 commit comments