forked from krka/futures-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CancelTest.java
73 lines (55 loc) · 2.42 KB
/
CancelTest.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package se.krka.futures;
import org.junit.Test;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class CancelTest {
@Test
public void cancelSimple() {
CompletableFuture<?> future = new CompletableFuture<>();
future.cancel(true);
assertEquals(CancellationException.class, Util.exceptionFromCallback(future).getClass());
Util.assertException(future, List.of(CancellationException.class));
}
@Test
public void cancelWithException() {
CompletableFuture<?> future = new CompletableFuture<>();
future.completeExceptionally(new CancellationException());
assertEquals(CancellationException.class, Util.exceptionFromCallback(future).getClass());
Util.assertException(future, List.of(CancellationException.class));
}
@Test
public void cancelParent() {
CompletableFuture<?> future = new CompletableFuture<>();
CompletableFuture<?> future2 = future.thenApply(o -> o);
future.cancel(false);
assertTrue(future.isCancelled());
assertTrue(future.isDone());
assertTrue(future.isCompletedExceptionally());
assertTrue(future2.isDone());
assertFalse(future2.isCancelled()); // This was NOT explicitly cancelled!
assertTrue(future2.isCompletedExceptionally());
assertEquals(CancellationException.class, Util.exceptionFromCallback(future).getClass());
assertEquals(CompletionException.class, Util.exceptionFromCallback(future2).getClass());
Util.assertException(future, List.of(CancellationException.class));
Util.assertException(future2, List.of(CompletionException.class, CancellationException.class));
}
@Test
public void cancelChild() {
CompletableFuture<?> future = new CompletableFuture<>();
CompletableFuture<?> future2 = future.thenApply(o -> o);
future2.cancel(false);
assertFalse(future.isCancelled());
assertFalse(future.isDone());
assertFalse(future.isCompletedExceptionally());
assertTrue(future2.isDone());
assertTrue(future2.isCancelled());
assertTrue(future2.isCompletedExceptionally());
assertEquals(CancellationException.class, Util.exceptionFromCallback(future2).getClass());
Util.assertException(future2, List.of(CancellationException.class));
}
}