-
Notifications
You must be signed in to change notification settings - Fork 293
/
Issue52.java
59 lines (48 loc) · 1.93 KB
/
Issue52.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package net.jodah.failsafe.issues;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.*;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.testng.annotations.Test;
import net.jodah.failsafe.Asserts;
import net.jodah.failsafe.Failsafe;
import net.jodah.failsafe.FailsafeFuture;
import net.jodah.failsafe.RetryPolicy;
@Test
public class Issue52 {
public void shouldCancelExecutionViaFuture() throws Throwable {
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
FailsafeFuture<String> proxyFuture = Failsafe.with(new RetryPolicy().withDelay(10, TimeUnit.MILLISECONDS))
.with(scheduler)
.get(exec -> {
throw new IllegalStateException();
});
Thread.sleep(100);
proxyFuture.cancel(true);
assertNull(proxyFuture.get());
}
public void shouldCancelExecutionViaCompletableFuture() throws Throwable {
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
AtomicInteger counter = new AtomicInteger();
CompletableFuture<String> proxyFuture = Failsafe.with(new RetryPolicy().withDelay(10, TimeUnit.MILLISECONDS))
.with(scheduler)
.future(exec -> {
counter.incrementAndGet();
CompletableFuture<String> result = new CompletableFuture<>();
result.completeExceptionally(new RuntimeException());
return result;
});
Thread.sleep(100);
proxyFuture.cancel(true);
int count = counter.get();
assertTrue(proxyFuture.isCancelled());
Asserts.assertThrows(() -> proxyFuture.get(), CancellationException.class);
// Assert that execution has actually stopped
Thread.sleep(20);
assertEquals(count, counter.get());
}
}