Skip to content

Commit 83f8219

Browse files
author
Dariusz Jędrzejczyk
authored
Replace sleep with CountDownLatch in tests (#126)
* Replace sleep with CountDownLatch in tests Relying on Thread.sleep(5) in ContextWrappingTests leads to assertion failures that imply broken functionality, while simply the operation would not finish in the given time. This change replaces Thread.sleep with CountDownLatch to wait for the task to finish and throws a TimeoutException in case it does not. Fixes #125. * Added a missed usage
1 parent e9bfa17 commit 83f8219

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

context-propagation/src/test/java/io/micrometer/context/ContextWrappingTests.java

+29-10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.util.Collections;
1919
import java.util.concurrent.Callable;
20+
import java.util.concurrent.CountDownLatch;
2021
import java.util.concurrent.ExecutionException;
2122
import java.util.concurrent.Executor;
2223
import java.util.concurrent.ExecutorService;
@@ -51,7 +52,7 @@ void clear() {
5152
}
5253

5354
@Test
54-
void should_instrument_runnable() throws InterruptedException {
55+
void should_instrument_runnable() throws InterruptedException, TimeoutException {
5556
StringThreadLocalHolder.setValue("hello");
5657
AtomicReference<String> valueInNewThread = new AtomicReference<>();
5758
Runnable runnable = runnable(valueInNewThread);
@@ -82,7 +83,7 @@ void should_instrument_callable() throws ExecutionException, InterruptedExceptio
8283
}
8384

8485
@Test
85-
void should_instrument_executor() throws InterruptedException {
86+
void should_instrument_executor() throws InterruptedException, TimeoutException {
8687
StringThreadLocalHolder.setValue("hello");
8788
AtomicReference<String> valueInNewThread = new AtomicReference<>();
8889
Executor executor = command -> new Thread(command).start();
@@ -139,10 +140,12 @@ void should_instrument_scheduled_executor_service()
139140
}
140141
}
141142

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));
144146
thread.start();
145-
Thread.sleep(5);
147+
148+
throwIfTimesOut(latch);
146149
}
147150

148151
private void runInNewThread(Callable<?> callable)
@@ -157,9 +160,23 @@ private void runInNewThread(Callable<?> callable)
157160
}
158161

159162
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+
}
163180
}
164181

165182
private void runInNewThread(ExecutorService executor, AtomicReference<String> valueInNewThread,
@@ -169,8 +186,10 @@ private void runInNewThread(ExecutorService executor, AtomicReference<String> va
169186
StringThreadLocalHolder.setValue("hello"); // IMPORTANT: We are setting the
170187
// thread local value as late as
171188
// 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);
174193
assertion.accept(valueInNewThread);
175194

176195
executor.submit(runnable(valueInNewThread)).get(5, TimeUnit.MILLISECONDS);

0 commit comments

Comments
 (0)