|
3 | 3 | import net.jodah.concurrentunit.Waiter; |
4 | 4 | import net.jodah.failsafe.*; |
5 | 5 | import net.jodah.failsafe.event.ExecutionCompletedEvent; |
| 6 | +import net.jodah.failsafe.function.AsyncSupplier; |
6 | 7 | import net.jodah.failsafe.function.CheckedSupplier; |
7 | 8 | import org.testng.annotations.Test; |
8 | 9 |
|
9 | 10 | import java.time.Duration; |
10 | 11 | import java.util.concurrent.*; |
11 | 12 | import java.util.concurrent.atomic.AtomicReference; |
| 13 | +import java.util.function.BiConsumer; |
12 | 14 |
|
13 | 15 | import static org.testng.Assert.*; |
14 | 16 |
|
15 | 17 | /** |
16 | | - * Tests behavior when a Failsafe Future is manually cancelled. |
| 18 | + * Tests behavior when a FailsafeFuture is manually cancelled. |
17 | 19 | */ |
18 | 20 | @Test |
19 | | -public class ManualCancellationTest extends Testing { |
| 21 | +public class FailsafeFutureCancellationTest extends Testing { |
20 | 22 | /** |
21 | 23 | * Asserts that cancelling a FailsafeFuture causes both retry policies to stop. |
22 | 24 | */ |
@@ -55,29 +57,62 @@ public void testCancelWithNestedRetries() throws Throwable { |
55 | 57 | /** |
56 | 58 | * Asserts that FailsafeFuture cancellations are propagated to a CompletionStage. |
57 | 59 | */ |
58 | | - public void shouldPropagateCancellationToCompletionStage() throws Throwable { |
| 60 | + public void shouldPropagateCancellationToStage() throws Throwable { |
59 | 61 | // Given |
60 | 62 | Policy<String> retryPolicy = new RetryPolicy<>(); |
61 | | - Waiter cancelledWaiter = new Waiter(); |
62 | | - CheckedSupplier<CompletionStage<String>> computeSomething = () -> { |
63 | | - CompletableFuture<String> future = new CompletableFuture<>(); |
64 | | - future.whenComplete((r, t) -> { |
65 | | - if (t instanceof CancellationException) |
66 | | - cancelledWaiter.resume(); |
67 | | - }); |
68 | | - return future; |
| 63 | + Waiter cancellationWaiter = new Waiter(); |
| 64 | + BiConsumer<String, Throwable> waiterResumer = (r, t) -> { |
| 65 | + if (t instanceof CancellationException) |
| 66 | + cancellationWaiter.resume(); |
| 67 | + }; |
| 68 | + CheckedSupplier<CompletionStage<String>> doWork = () -> { |
| 69 | + CompletableFuture<String> promise = new CompletableFuture<>(); |
| 70 | + promise.whenComplete(waiterResumer); |
| 71 | + |
| 72 | + // Simulate asynchronous work |
| 73 | + runInThread(() -> Thread.sleep(1000)); |
| 74 | + return promise; |
69 | 75 | }; |
70 | 76 |
|
71 | 77 | // When |
72 | | - CompletableFuture<String> future = Failsafe.with(retryPolicy).getStageAsync(computeSomething); |
73 | | - future.whenComplete((r, t) -> { |
| 78 | + CompletableFuture<String> failsafeFuture = Failsafe.with(retryPolicy).getStageAsync(doWork); |
| 79 | + failsafeFuture.whenComplete(waiterResumer); |
| 80 | + failsafeFuture.cancel(true); |
| 81 | + |
| 82 | + // Then |
| 83 | + Asserts.assertThrows(failsafeFuture::get, CancellationException.class); |
| 84 | + // Wait for the promise and failsafeFuture to complete with cancellation |
| 85 | + cancellationWaiter.await(1000, 2); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Asserts that FailsafeFuture cancellations are propagated to a CompletionStage in an async integration execution. |
| 90 | + */ |
| 91 | + public void shouldPropagateCancellationToStageAsyncExecution() throws Throwable { |
| 92 | + // Given |
| 93 | + Policy<String> retryPolicy = new RetryPolicy<>(); |
| 94 | + Waiter cancellationWaiter = new Waiter(); |
| 95 | + BiConsumer<String, Throwable> waiterResumer = (r, t) -> { |
74 | 96 | if (t instanceof CancellationException) |
75 | | - cancelledWaiter.resume(); |
76 | | - }); |
77 | | - future.cancel(true); |
| 97 | + cancellationWaiter.resume(); |
| 98 | + }; |
| 99 | + AsyncSupplier<String, CompletionStage<String>> doWork = exec -> { |
| 100 | + CompletableFuture<String> promise = new CompletableFuture<>(); |
| 101 | + promise.whenComplete(waiterResumer); |
| 102 | + |
| 103 | + // Simulate asynchronous work |
| 104 | + runInThread(() -> Thread.sleep(1000)); |
| 105 | + return promise; |
| 106 | + }; |
| 107 | + |
| 108 | + // When |
| 109 | + CompletableFuture<String> failsafeFuture = Failsafe.with(retryPolicy).getStageAsyncExecution(doWork); |
| 110 | + failsafeFuture.whenComplete(waiterResumer); |
| 111 | + failsafeFuture.cancel(true); |
78 | 112 |
|
79 | 113 | // Then |
80 | | - Asserts.assertThrows(future::get, CancellationException.class); |
81 | | - cancelledWaiter.await(1000, 2); |
| 114 | + Asserts.assertThrows(failsafeFuture::get, CancellationException.class); |
| 115 | + // Wait for the promise and failsafeFuture to complete with cancellation |
| 116 | + cancellationWaiter.await(1000, 2); |
82 | 117 | } |
83 | 118 | } |
0 commit comments