added waiting for watch to be finished in ScheduledExecutorTimeoutWatcherTest
package io.smallrye.faulttolerance.core.timeout;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.Assert.fail;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Supplier;
import org.junit.After;
import org.junit.Before;
@@ -44,7 +46,8 @@ public class ScheduledExecutorTimeoutWatcherTest {
assertThat(execution.isRunning()).isFalse();
assertThat(execution.hasFinished()).isFalse();
assertThat(execution.hasTimedOut()).isTrue();
- assertThat(watch.isRunning()).isFalse();
+
+ assertThatWithin(100, "watch not running", () -> !watch.isRunning());
}
@Test
@@ -65,7 +68,22 @@ public class ScheduledExecutorTimeoutWatcherTest {
assertThat(execution.isRunning()).isFalse();
assertThat(execution.hasFinished()).isTrue();
assertThat(execution.hasTimedOut()).isFalse();
- assertThat(watch.isRunning()).isFalse();
+ assertThatWithin(100, "watch not running", () -> !watch.isRunning());
+ }
+
+ private static void assertThatWithin(int timeoutMs, String message, Supplier<Boolean> test) {
+ long start = System.currentTimeMillis();
+ while (System.currentTimeMillis() - start < timeoutMs) {
+ try {
+ Thread.sleep(50);
+ } catch (InterruptedException e) {
+ throw new RuntimeException("Interrupted waiting for " + message);
+ }
+ if (test.get()) {
+ return;
+ }
+ }
+ fail(message + " not satisfied in " + timeoutMs + "ms");
}
private Thread run(AtomicBoolean interruptionFlag) {
