88import static java .util .concurrent .TimeUnit .MILLISECONDS ;
99import static org .junit .Assert .assertEquals ;
1010import static org .junit .Assert .assertFalse ;
11+ import static org .junit .Assert .assertNotSame ;
1112import static org .junit .Assert .assertSame ;
1213import static org .junit .Assert .assertThrows ;
1314import static org .junit .Assert .assertTrue ;
1415import 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 ;
1618
19+ import java .util .Arrays ;
1720import java .util .concurrent .TimeUnit ;
1821import java .util .concurrent .atomic .AtomicReference ;
1922
2023import org .junit .Test ;
2124import org .junit .function .ThrowingRunnable ;
2225import org .junit .internal .runners .statements .Fail ;
26+ import org .junit .runner .RunWith ;
27+ import org .junit .runners .Parameterized ;
2328import org .junit .runners .model .Statement ;
2429import org .junit .runners .model .TestTimedOutException ;
2530
2631
2732/**
2833 * @author Asaf Ary, Stefan Birkner
2934 */
35+ @ RunWith (Parameterized .class )
3036public class FailOnTimeoutTest {
3137 private static final long TIMEOUT = 100 ;
3238 private static final long DURATION_THAT_EXCEEDS_TIMEOUT = 60 * 60 * 1000 ; //1 hour
3339
3440 private final TestStatement statement = new TestStatement ();
3541
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+ }
3758
3859 @ Test
3960 public void throwsTestTimedOutException () {
@@ -212,14 +233,17 @@ private void notTheRealCauseOfTheTimeout() {
212233 }
213234
214235 @ Test
215- public void threadGroupNotLeaked () throws Throwable {
236+ public void lookingForStuckThread_threadGroupNotLeaked () throws Throwable {
237+ assumeTrue (lookingForStuckThread );
216238 final AtomicReference <ThreadGroup > innerThreadGroup = new AtomicReference <ThreadGroup >();
217239 final AtomicReference <Thread > innerThread = new AtomicReference <Thread >();
240+ final ThreadGroup outerThreadGroup = currentThread ().getThreadGroup ();
218241 ThrowingRunnable runnable = evaluateWithDelegate (new Statement () {
219242 @ Override
220243 public void evaluate () {
221244 innerThread .set (currentThread ());
222245 ThreadGroup group = currentThread ().getThreadGroup ();
246+ assertNotSame ("inner thread should use a different thread group" , outerThreadGroup , group );
223247 innerThreadGroup .set (group );
224248 assertTrue ("the 'FailOnTimeoutGroup' thread group should be a daemon thread group" , group .isDaemon ());
225249 }
@@ -231,4 +255,19 @@ public void evaluate() {
231255 innerThread .get ().join ();
232256 assertTrue ("the 'FailOnTimeoutGroup' thread group should be destroyed after running the test" , innerThreadGroup .get ().isDestroyed ());
233257 }
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+ }
234273}
0 commit comments