14
14
import static org .junit .Assert .fail ;
15
15
import static org .junit .internal .runners .statements .FailOnTimeout .builder ;
16
16
17
- import java .util .Arrays ;
18
- import java .util .Collection ;
19
- import java .util .HashSet ;
20
- import java .util .Set ;
21
17
import java .util .concurrent .TimeUnit ;
18
+ import java .util .concurrent .atomic .AtomicReference ;
22
19
23
20
import org .junit .Test ;
24
21
import org .junit .function .ThrowingRunnable ;
22
+ import org .junit .internal .runners .statements .Fail ;
25
23
import org .junit .runners .model .Statement ;
26
24
import org .junit .runners .model .TestTimedOutException ;
27
25
26
+
28
27
/**
29
28
* @author Asaf Ary, Stefan Birkner
30
29
*/
@@ -91,20 +90,24 @@ public void throwsExceptionWithTimeoutValueAndTimeUnitSet() {
91
90
assertEquals (TimeUnit .MILLISECONDS , e .getTimeUnit ());
92
91
}
93
92
94
- private ThrowingRunnable evaluateWithException (final Exception exception ) {
93
+ private ThrowingRunnable evaluateWithDelegate (final Statement delegate ) {
95
94
return new ThrowingRunnable () {
96
95
public void run () throws Throwable {
97
- statement .nextException = exception ;
96
+ statement .nextStatement = delegate ;
98
97
statement .waitDuration = 0 ;
99
98
failOnTimeout .evaluate ();
100
99
}
101
100
};
102
101
}
103
102
103
+ private ThrowingRunnable evaluateWithException (Exception exception ) {
104
+ return evaluateWithDelegate (new Fail (exception ));
105
+ }
106
+
104
107
private ThrowingRunnable evaluateWithWaitDuration (final long waitDuration ) {
105
108
return new ThrowingRunnable () {
106
109
public void run () throws Throwable {
107
- statement .nextException = null ;
110
+ statement .nextStatement = null ;
108
111
statement .waitDuration = waitDuration ;
109
112
failOnTimeout .evaluate ();
110
113
}
@@ -114,13 +117,13 @@ public void run() throws Throwable {
114
117
private static final class TestStatement extends Statement {
115
118
long waitDuration ;
116
119
117
- Exception nextException ;
120
+ Statement nextStatement ;
118
121
119
122
@ Override
120
123
public void evaluate () throws Throwable {
121
124
sleep (waitDuration );
122
- if (nextException != null ) {
123
- throw nextException ;
125
+ if (nextStatement != null ) {
126
+ nextStatement . evaluate () ;
124
127
}
125
128
}
126
129
}
@@ -210,20 +213,22 @@ private void notTheRealCauseOfTheTimeout() {
210
213
211
214
@ Test
212
215
public void threadGroupNotLeaked () throws Throwable {
213
- Collection <ThreadGroup > groupsBeforeSet = subGroupsOfCurrentThread ();
214
-
215
- evaluateWithWaitDuration (0 );
216
-
217
- for (ThreadGroup group : subGroupsOfCurrentThread ()) {
218
- if (!groupsBeforeSet .contains (group ) && "FailOnTimeoutGroup" .equals (group .getName ())) {
219
- fail ("A 'FailOnTimeoutGroup' thread group remains referenced after the test execution." );
216
+ final AtomicReference <ThreadGroup > innerThreadGroup = new AtomicReference <ThreadGroup >();
217
+ final AtomicReference <Thread > innerThread = new AtomicReference <Thread >();
218
+ ThrowingRunnable runnable = evaluateWithDelegate (new Statement () {
219
+ @ Override
220
+ public void evaluate () {
221
+ innerThread .set (currentThread ());
222
+ ThreadGroup group = currentThread ().getThreadGroup ();
223
+ innerThreadGroup .set (group );
224
+ assertTrue ("the 'FailOnTimeoutGroup' thread group should be a daemon thread group" , group .isDaemon ());
220
225
}
221
- }
222
- }
223
-
224
- private Collection < ThreadGroup > subGroupsOfCurrentThread () {
225
- ThreadGroup [] subGroups = new ThreadGroup [ 256 ] ;
226
- int numGroups = currentThread (). getThreadGroup ().enumerate ( subGroups );
227
- return Arrays . asList ( subGroups ). subList ( 0 , numGroups );
226
+ });
227
+
228
+ runnable . run ();
229
+
230
+ assertTrue ( "the Statement was never run" , innerThread . get () != null ) ;
231
+ innerThread . get ().join ( );
232
+ assertTrue ( "the 'FailOnTimeoutGroup' thread group should be destroyed after running the test" , innerThreadGroup . get (). isDestroyed () );
228
233
}
229
234
}
0 commit comments