Skip to content

Commit

Permalink
Fix flaky tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jhalterman committed Feb 9, 2022
1 parent 40f7e6a commit 68e6c1f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/test/java/dev/failsafe/functional/TimeoutTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public void testFallbackTimeoutWithBlockedSupplier() {
fbStats.reset();
}, Failsafe.with(fallback).compose(timeout), ctx -> {
System.out.println("Executing");
Thread.sleep(100);
Thread.sleep(200);
throw new Exception();
}, (f, e) -> {
assertEquals(e.getAttemptCount(), 1);
Expand All @@ -216,11 +216,11 @@ public void testFallbackTimeoutWithBlockedSupplier() {
}, IllegalStateException.class);

// Test without interrupt
Timeout<Object> timeout = withStatsAndLogs(Timeout.builder(Duration.ofMillis(1)), timeoutStats).build();
Timeout<Object> timeout = withStatsAndLogs(Timeout.builder(Duration.ofMillis(100)), timeoutStats).build();
test.accept(timeout);

// Test with interrupt
timeout = withStatsAndLogs(Timeout.builder(Duration.ofMillis(1)).withInterrupt(), timeoutStats).build();
timeout = withStatsAndLogs(Timeout.builder(Duration.ofMillis(100)).withInterrupt(), timeoutStats).build();
test.accept(timeout);
}

Expand Down
8 changes: 6 additions & 2 deletions src/test/java/dev/failsafe/issues/Issue231Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@

import java.time.Duration;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.testng.Assert.assertTrue;

@Test
public class Issue231Test {
/**
* Timeout, even with interruption, should wait for the execution to complete.
* Timeout, even with interruption, should wait for the execution to complete before completing the future.
*/
public void shouldWaitForExecutionCompletion() {
// Use a separate executorService for this test in case the common pool is full
ExecutorService executorService = Executors.newFixedThreadPool(2);
Timeout<Object> timeout = Timeout.builder(Duration.ofMillis(100)).withInterrupt().build();
AtomicBoolean executionCompleted = new AtomicBoolean();
Asserts.assertThrows(() -> Failsafe.with(timeout).runAsync(() -> {
Asserts.assertThrows(() -> Failsafe.with(timeout).with(executorService).runAsync(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException ignore) {
Expand Down

0 comments on commit 68e6c1f

Please sign in to comment.