17
17
18
18
import java .util .Collections ;
19
19
import java .util .concurrent .Callable ;
20
+ import java .util .concurrent .CountDownLatch ;
20
21
import java .util .concurrent .ExecutionException ;
21
22
import java .util .concurrent .Executor ;
22
23
import java .util .concurrent .ExecutorService ;
@@ -51,7 +52,7 @@ void clear() {
51
52
}
52
53
53
54
@ Test
54
- void should_instrument_runnable () throws InterruptedException {
55
+ void should_instrument_runnable () throws InterruptedException , TimeoutException {
55
56
StringThreadLocalHolder .setValue ("hello" );
56
57
AtomicReference <String > valueInNewThread = new AtomicReference <>();
57
58
Runnable runnable = runnable (valueInNewThread );
@@ -82,7 +83,7 @@ void should_instrument_callable() throws ExecutionException, InterruptedExceptio
82
83
}
83
84
84
85
@ Test
85
- void should_instrument_executor () throws InterruptedException {
86
+ void should_instrument_executor () throws InterruptedException , TimeoutException {
86
87
StringThreadLocalHolder .setValue ("hello" );
87
88
AtomicReference <String > valueInNewThread = new AtomicReference <>();
88
89
Executor executor = command -> new Thread (command ).start ();
@@ -139,10 +140,12 @@ void should_instrument_scheduled_executor_service()
139
140
}
140
141
}
141
142
142
- private void runInNewThread (Runnable runnable ) throws InterruptedException {
143
- Thread thread = new Thread (runnable );
143
+ private void runInNewThread (Runnable runnable ) throws InterruptedException , TimeoutException {
144
+ CountDownLatch latch = new CountDownLatch (1 );
145
+ Thread thread = new Thread (countDownWhenDone (runnable , latch ));
144
146
thread .start ();
145
- Thread .sleep (5 );
147
+
148
+ throwIfTimesOut (latch );
146
149
}
147
150
148
151
private void runInNewThread (Callable <?> callable )
@@ -157,9 +160,23 @@ private void runInNewThread(Callable<?> callable)
157
160
}
158
161
159
162
private void runInNewThread (Executor executor , AtomicReference <String > valueInNewThread )
160
- throws InterruptedException {
161
- executor .execute (runnable (valueInNewThread ));
162
- Thread .sleep (5 );
163
+ throws InterruptedException , TimeoutException {
164
+ CountDownLatch latch = new CountDownLatch (1 );
165
+ executor .execute (countDownWhenDone (runnable (valueInNewThread ), latch ));
166
+ throwIfTimesOut (latch );
167
+ }
168
+
169
+ private Runnable countDownWhenDone (Runnable runnable , CountDownLatch latch ) {
170
+ return () -> {
171
+ runnable .run ();
172
+ latch .countDown ();
173
+ };
174
+ }
175
+
176
+ private void throwIfTimesOut (CountDownLatch latch ) throws InterruptedException , TimeoutException {
177
+ if (!latch .await (5 , TimeUnit .MILLISECONDS )) {
178
+ throw new TimeoutException ("Waiting for executed task timed out" );
179
+ }
163
180
}
164
181
165
182
private void runInNewThread (ExecutorService executor , AtomicReference <String > valueInNewThread ,
@@ -169,8 +186,10 @@ private void runInNewThread(ExecutorService executor, AtomicReference<String> va
169
186
StringThreadLocalHolder .setValue ("hello" ); // IMPORTANT: We are setting the
170
187
// thread local value as late as
171
188
// possible
172
- executor .execute (runnable (valueInNewThread ));
173
- Thread .sleep (5 );
189
+
190
+ CountDownLatch latch = new CountDownLatch (1 );
191
+ executor .execute (countDownWhenDone (runnable (valueInNewThread ), latch ));
192
+ throwIfTimesOut (latch );
174
193
assertion .accept (valueInNewThread );
175
194
176
195
executor .submit (runnable (valueInNewThread )).get (5 , TimeUnit .MILLISECONDS );
0 commit comments