forked from krka/futures-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CombineTest.java
42 lines (33 loc) · 1.79 KB
/
CombineTest.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
package se.krka.futures;
import com.spotify.futures.CompletableFutures;
import org.junit.Test;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import static org.junit.Assert.assertEquals;
public class CombineTest {
@Test
public void testCombineWithNesting() {
CompletableFuture<String> futureA = CompletableFuture.completedFuture("A");
CompletableFuture<String> futureB = CompletableFuture.completedFuture("B");
CompletableFuture<String> futureC = CompletableFuture.completedFuture("C");
CompletableFuture<String> result = futureA.thenCompose(valueA -> futureB.thenCompose(valueB -> futureC.thenApply(valueC -> valueA + valueB + valueC)));
assertEquals("ABC", CompletableFutures.getCompleted(result));
}
@Test
public void testCombineWithThenCombine() {
CompletableFuture<String> futureA = CompletableFuture.completedFuture("A");
CompletableFuture<String> futureB = CompletableFuture.completedFuture("B");
CompletableFuture<String> futureC = CompletableFuture.completedFuture("C");
CompletableFuture<String> result = futureA.thenCombine(futureB, (valueA, valueB) -> valueA + valueB).thenCombine(futureC, (valueAB, valueC) -> valueAB + valueC);
assertEquals("ABC", CompletableFutures.getCompleted(result));
}
@Test
public void testCombineWithLibrary() {
CompletableFuture<String> futureA = CompletableFuture.completedFuture("A");
CompletableFuture<String> futureB = CompletableFuture.completedFuture("B");
CompletableFuture<String> futureC = CompletableFuture.completedFuture("C");
CompletionStage<String> result = CompletableFutures.combine(futureA, futureB, futureC,
(valueA, valueB, valueC) -> valueA + valueB + valueC);
assertEquals("ABC", CompletableFutures.getCompleted(result));
}
}