forked from coderbruis/JavaSourceCodeLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompletableFuturePrincipleTest.java
More file actions
159 lines (124 loc) · 5.53 KB
/
Copy pathCompletableFuturePrincipleTest.java
File metadata and controls
159 lines (124 loc) · 5.53 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.learnjava.concurrent;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;;
/**
* @Author : haiyang.luo
* @Date : 2026/6/28 21:44
* @Description :
*/
@Timeout(30)
public class CompletableFuturePrincipleTest {
@Test
public void testCompleteTriggersDependentStageRegisteredBeforeCompletion() {
CompletableFuture<Integer> source = new CompletableFuture<>();
CompletableFuture<Integer> dependent = source.thenApply(value -> value + 1);
assertFalse(dependent.isDone());
assertTrue(source.complete(10));
assertEquals(11, dependent.join());
assertTrue(dependent.isDone());
}
@Test
public void testDependentStageRegisteredAfterCompletionRunsOnCallerThread() {
CompletableFuture<Integer> source = CompletableFuture.completedFuture(10);
String callerThreadName = Thread.currentThread().getName();
AtomicReference<String> callbackThreadName = new AtomicReference<>();
CompletableFuture<Integer> dependent = source.thenApply(value -> {
callbackThreadName.set(Thread.currentThread().getName());
return value + 1;
});
assertEquals(11, dependent.join());
assertEquals(callerThreadName, callbackThreadName.get());
}
@Test
public void testAsyncDependentStageRunsOnExecutor() {
ExecutorService executor = Executors.newSingleThreadExecutor(runnable -> {
Thread thread = new Thread(runnable);
thread.setName("completable-future-principle-test");
return thread;
});
try {
CompletableFuture<Integer> source = new CompletableFuture<>();
AtomicReference<String> callbackThreadName = new AtomicReference<>();
CompletableFuture<Integer> dependent = source.thenApplyAsync(value -> {
callbackThreadName.set(Thread.currentThread().getName());
return value + 1;
}, executor);
assertTrue(source.complete(10));
assertEquals(11, dependent.join());
assertEquals("completable-future-principle-test", callbackThreadName.get());
} finally {
executor.shutdownNow();
}
}
@Test
public void testThenComposeCompletesAfterReturnedFutureCompletes() {
CompletableFuture<Integer> source = new CompletableFuture<>();
CompletableFuture<Integer> inner = new CompletableFuture<>();
CompletableFuture<Integer> composed = source.thenCompose(value -> inner.thenApply(innerValue -> value + innerValue));
assertTrue(source.complete(10));
assertFalse(composed.isDone());
assertTrue(inner.complete(5));
assertEquals(15, composed.join());
}
@Test
public void testExceptionalCompletionSkipsThenApplyAndCanRecover() {
CompletableFuture<Integer> source = new CompletableFuture<>();
AtomicBoolean thenApplyCalled = new AtomicBoolean(false);
CompletableFuture<Integer> recovered = source
.thenApply(value -> {
thenApplyCalled.set(true);
return value + 1;
})
.exceptionally(exception -> 0);
RuntimeException failure = new RuntimeException("boom");
assertTrue(source.completeExceptionally(failure));
assertEquals(0, recovered.join());
assertFalse(thenApplyCalled.get());
}
@Test
public void testOnlyFirstCompletionWins() {
CompletableFuture<String> future = new CompletableFuture<>();
assertTrue(future.complete("first"));
assertFalse(future.complete("second"));
assertFalse(future.completeExceptionally(new RuntimeException("boom")));
assertEquals("first", future.join());
}
@Test
public void testJoinWaitsForCompletion() throws Exception {
CompletableFuture<String> future = new CompletableFuture<>();
CountDownLatch joinStarted = new CountDownLatch(1);
AtomicReference<String> joinedValue = new AtomicReference<>();
Thread waitingThread = new Thread(() -> {
joinStarted.countDown();
joinedValue.set(future.join());
});
waitingThread.start();
assertTrue(joinStarted.await(5, TimeUnit.SECONDS));
assertFalse(future.isDone());
assertTrue(future.complete("done"));
waitingThread.join(TimeUnit.SECONDS.toMillis(5));
assertFalse(waitingThread.isAlive());
assertEquals("done", joinedValue.get());
}
@Test
public void testJoinWrapsFailureInCompletionException() {
CompletableFuture<String> future = new CompletableFuture<>();
RuntimeException failure = new RuntimeException("boom");
assertTrue(future.completeExceptionally(failure));
CompletionException exception = assertThrows(CompletionException.class, future::join);
assertSame(failure, exception.getCause());
}
}