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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
package fun.happyhacker;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class CompletableFutureDemoTest {
@Test
void testSimpleComplete() throws InterruptedException, ExecutionException {
Future<String> future = new CompletableFutureDemo().simpleComplete();
assertEquals("Hello", future.get());
}
@Test
void testSupplyAsync() throws InterruptedException, ExecutionException {
Future<String> future = new CompletableFutureDemo().supplyAsync();
assertEquals("Hello", future.get());
}
@Test
void testThenApply() throws InterruptedException, ExecutionException {
Future<String> future = new CompletableFutureDemo().thenApply();
assertEquals("Hello World", future.get());
}
@Test
void testThenAccept() throws InterruptedException, ExecutionException {
Future<Void> future = new CompletableFutureDemo().thenAccept();
assertEquals(null, future.get());
}
@Test
void testThenRun() throws InterruptedException, ExecutionException {
Future<Void> future = new CompletableFutureDemo().thenRun();
assertEquals(null, future.get());
}
@Test
void testThenCompose() throws InterruptedException, ExecutionException {
Future<String> future = new CompletableFutureDemo().thenCompose();
assertEquals("Hello Future World", future.get());
}
@Test
void testThenCompose2() throws InterruptedException, ExecutionException {
Future<String> future = new CompletableFutureDemo().thenCompose2();
assertEquals("Hello World", future.get());
}
@Test
void testThenCombine() throws InterruptedException, ExecutionException {
Future<String> future = new CompletableFutureDemo().thenCombine();
assertEquals("Hello Future World", future.get());
}
@Test
void testAllOf() throws InterruptedException, ExecutionException {
Future<Void> future = new CompletableFutureDemo().allOf();
future.get();
}
@Test
void testJoin() throws InterruptedException, ExecutionException {
String str = new CompletableFutureDemo().join();
assertEquals("Hello Beautiful World", str);
}
@Test
void testHandleError() throws InterruptedException, ExecutionException {
Future<String> future = new CompletableFutureDemo().handleError("World");
assertEquals("Hello World", future.get());
Future<String> future2 = new CompletableFutureDemo().handleError(null);
assertEquals("Hello Stranger", future2.get());
Future<String> future3 = new CompletableFutureDemo().handleError2(null);
assertEquals("抛异常了 java.lang.RuntimeException: Computation Error Hello Stranger", future3.get());
// Future<String> future4 = new CompletableFutureDemo().handleError3(null);
// try {
// future4.get();
// } catch (InterruptedException | ExecutionException e) {
// assertEquals("java.lang.RuntimeException: Computation Error",
// e.getMessage());
// }
}
@Test
void testThenComposeAsync() throws InterruptedException, ExecutionException {
Future<String> future = new CompletableFutureDemo().composeAsync();
assertEquals("Hello Future World", future.get());
}
@Test
void testThenComposeAsync2() throws InterruptedException, ExecutionException {
Future<String> future = new CompletableFutureDemo().composeAsync2();
assertEquals("Hello Future World", future.get());
}
@Test
void testThenComposeAsync3() throws InterruptedException, ExecutionException {
Future<String> future = new CompletableFutureDemo().composeAsync3();
future.get();
}
}
|