Skip to content

Commit d2d68de

Browse files
authored
Fix spurious Future tests failure on Windows runners (assertj#2649)
1 parent 2a44064 commit d2d68de

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

src/test/java/org/assertj/core/api/future/AbstractFutureTest.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,63 @@
1212
*/
1313
package org.assertj.core.api.future;
1414

15+
import static java.lang.String.format;
16+
17+
import java.lang.Thread.UncaughtExceptionHandler;
1518
import java.util.concurrent.ExecutorService;
1619
import java.util.concurrent.Executors;
1720

21+
import java.util.concurrent.ThreadFactory;
22+
import java.util.concurrent.atomic.AtomicInteger;
1823
import org.junit.jupiter.api.AfterEach;
1924
import org.junit.jupiter.api.BeforeEach;
25+
import org.junit.platform.commons.logging.Logger;
26+
import org.junit.platform.commons.logging.LoggerFactory;
2027

2128
abstract class AbstractFutureTest {
22-
2329
protected ExecutorService executorService;
30+
private final ThreadFactory factory;
31+
32+
AbstractFutureTest() {
33+
factory = new FutureTestThreadFactory();
34+
}
2435

2536
@BeforeEach
2637
void beforeEach() {
27-
executorService = Executors.newSingleThreadExecutor();
38+
executorService = Executors.newSingleThreadExecutor(factory);
2839
}
2940

3041
@AfterEach
3142
void afterEach() {
3243
executorService.shutdownNow();
3344
}
45+
46+
private class FutureTestThreadFactory implements ThreadFactory, UncaughtExceptionHandler {
47+
private final Logger logger;
48+
private final AtomicInteger count;
49+
50+
private FutureTestThreadFactory() {
51+
logger = LoggerFactory.getLogger(AbstractFutureTest.this.getClass());
52+
count = new AtomicInteger(0);
53+
}
54+
55+
@Override
56+
public Thread newThread(Runnable runnable) {
57+
Thread thread = new Thread(runnable);
58+
thread.setName(AbstractFutureTest.this.getClass().getName() + "." + count.incrementAndGet());
59+
thread.setDaemon(true);
60+
thread.setPriority(Thread.MAX_PRIORITY);
61+
thread.setUncaughtExceptionHandler(this);
62+
thread.setContextClassLoader(Thread.currentThread().getContextClassLoader());
63+
return thread;
64+
}
65+
66+
@Override
67+
public void uncaughtException(Thread thread, Throwable ex) {
68+
logger.info(
69+
ex,
70+
() -> format("Thread %s [%s] threw an exception", thread.getName(), thread.getId())
71+
);
72+
}
73+
}
3474
}

src/test/java/org/assertj/core/api/future/FutureAssert_succeedsWithin_duration_Test.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ void should_allow_assertion_on_future_result_when_completed_normally() {
4646
void should_allow_assertion_on_future_result_when_completed_normally_within_timeout() {
4747
// GIVEN
4848
String value = "done";
49-
int sleepDuration = 10;
49+
int sleepDuration = 100;
5050
Future<String> future = futureAfter(value, sleepDuration, executorService);
51+
5152
// WHEN/THEN
5253
// using the same duration would fail depending on when the thread executing the future is started
53-
assertThat(future).succeedsWithin(Duration.ofMillis(sleepDuration + 100))
54-
.isEqualTo(value);
54+
assertThat(future)
55+
.succeedsWithin(Duration.ofMillis(sleepDuration + 1_000))
56+
.isEqualTo(value);
5557
}
5658

5759
@Test

0 commit comments

Comments
 (0)