forked from krka/futures-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoveExecutorTest.java
44 lines (38 loc) · 1.67 KB
/
MoveExecutorTest.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
package se.krka.futures;
import org.junit.Test;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.function.Function;
import static org.junit.Assert.assertTrue;
public class MoveExecutorTest {
@Test
public void testMoveExecutor() {
ExecutorService first = Util.newExecutor("first");
ExecutorService second = Util.newExecutor("second");
CompletableFuture<String> future = CompletableFuture
.supplyAsync(() -> Util.currThread(), first)
.thenApplyAsync(s -> Util.currThread(), second);
assertTrue(Set.of("second", "main").contains(future.join()));
}
@Test
public void testMoveExecutorWithException() {
ExecutorService first = Util.newExecutor("first");
ExecutorService second = Util.newExecutor("second");
CompletableFuture<String> future = CompletableFuture
.<String>supplyAsync(() -> Util.doThrow(new RuntimeException(Util.currThread())), first)
.thenApplyAsync(Function.identity(), second)
.exceptionally(throwable -> Util.currThread());
assertTrue(Set.of("first", "main").contains(future.join()));
}
@Test
public void testMoveExecutorWithExceptionActuallyMove() {
ExecutorService first = Util.newExecutor("first");
ExecutorService second = Util.newExecutor("second");
CompletableFuture<String> future = CompletableFuture
.<String>supplyAsync(() -> Util.doThrow(new RuntimeException(Util.currThread())), first)
.whenCompleteAsync((s, t) -> {}, second)
.exceptionally(throwable -> Util.currThread());
assertTrue(Set.of("second", "main").contains(future.join()));
}
}