|
| 1 | +package dev.failsafe.functional; |
| 2 | + |
| 3 | +import dev.failsafe.*; |
| 4 | +import dev.failsafe.function.CheckedConsumer; |
| 5 | +import dev.failsafe.function.ContextualRunnable; |
| 6 | +import dev.failsafe.testing.Testing; |
| 7 | +import net.jodah.concurrentunit.Waiter; |
| 8 | +import org.testng.annotations.BeforeMethod; |
| 9 | +import org.testng.annotations.Test; |
| 10 | + |
| 11 | +import java.time.Duration; |
| 12 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 13 | + |
| 14 | +import static org.testng.Assert.assertTrue; |
| 15 | + |
| 16 | +@Test |
| 17 | +public class CallCancellationTest extends Testing { |
| 18 | + Waiter waiter; |
| 19 | + |
| 20 | + @BeforeMethod |
| 21 | + void beforeMethod() { |
| 22 | + waiter = new Waiter(); |
| 23 | + } |
| 24 | + |
| 25 | + private void assertCancel(FailsafeExecutor<Void> executor, ContextualRunnable<Void> runnable, |
| 26 | + boolean testWithoutInterrupt) throws Throwable { |
| 27 | + CheckedConsumer<Boolean> test = interrupt -> { |
| 28 | + // Given |
| 29 | + Call<Void> call = executor.onComplete(e -> { |
| 30 | + waiter.assertNull(e.getResult()); |
| 31 | + if (!interrupt) |
| 32 | + waiter.assertNull(e.getException()); |
| 33 | + else |
| 34 | + waiter.assertTrue(e.getException() instanceof InterruptedException); |
| 35 | + waiter.resume(); |
| 36 | + }).newCall(runnable); |
| 37 | + |
| 38 | + // When |
| 39 | + runInThread(() -> { |
| 40 | + Testing.sleep(300); |
| 41 | + waiter.assertTrue(call.cancel(interrupt)); |
| 42 | + }); |
| 43 | + if (!interrupt) |
| 44 | + call.execute(); |
| 45 | + else |
| 46 | + assertThrows(call::execute, FailsafeException.class, InterruptedException.class); |
| 47 | + |
| 48 | + waiter.await(1000); |
| 49 | + |
| 50 | + // Then |
| 51 | + assertTrue(call.isCancelled()); |
| 52 | + }; |
| 53 | + |
| 54 | + // Test without interrupt |
| 55 | + if (testWithoutInterrupt) |
| 56 | + test.accept(false); |
| 57 | + |
| 58 | + // Test with interrupt |
| 59 | + test.accept(true); |
| 60 | + } |
| 61 | + |
| 62 | + public void shouldCancelRetriesWithBlockedExecution() throws Throwable { |
| 63 | + assertCancel(Failsafe.with(RetryPolicy.ofDefaults()), ctx -> { |
| 64 | + try { |
| 65 | + waiter.assertFalse(ctx.isCancelled()); |
| 66 | + Thread.sleep(1000); |
| 67 | + } catch (InterruptedException e) { |
| 68 | + waiter.assertTrue(ctx.isCancelled()); |
| 69 | + throw e; |
| 70 | + } |
| 71 | + }, true); |
| 72 | + } |
| 73 | + |
| 74 | + public void shouldCancelRetriesWithPendingDelay() throws Throwable { |
| 75 | + RetryPolicy<Void> retryPolicy = RetryPolicy.<Void>builder().withDelay(Duration.ofMinutes(1)).build(); |
| 76 | + assertCancel(Failsafe.with(retryPolicy), ctx -> { |
| 77 | + throw new IllegalStateException(); |
| 78 | + }, false); |
| 79 | + } |
| 80 | + |
| 81 | + public void shouldPropagateCancelToCallback() throws Throwable { |
| 82 | + AtomicBoolean callbackCalled = new AtomicBoolean(); |
| 83 | + assertCancel(Failsafe.with(RetryPolicy.ofDefaults()), ctx -> { |
| 84 | + ctx.onCancel(() -> callbackCalled.set(true)); |
| 85 | + |
| 86 | + try { |
| 87 | + waiter.assertFalse(ctx.isCancelled()); |
| 88 | + Thread.sleep(1000); |
| 89 | + } catch (InterruptedException e) { |
| 90 | + waiter.assertTrue(ctx.isCancelled()); |
| 91 | + waiter.assertTrue(callbackCalled.get()); |
| 92 | + throw e; |
| 93 | + } |
| 94 | + }, true); |
| 95 | + } |
| 96 | +} |
| 97 | + |
0 commit comments