MCPcopy Create free account
hub / github.com/traneio/future / FuturePoolTest

Class FuturePoolTest

future-java/src/test/java/io/trane/future/FuturePoolTest.java:19–154  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17import org.junit.Test;
18
19public class FuturePoolTest {
20
21 private <T> T get(Future<T> future) throws CheckedFutureException {
22 return future.get(Duration.ofMillis(100));
23 }
24
25 @Test
26 public void isolate() throws CheckedFutureException {
27 ExecutorService es = Executors.newCachedThreadPool();
28 try {
29 FuturePool pool = FuturePool.apply(es);
30 Thread originalThread = Thread.currentThread();
31
32 Future<Integer> future = pool.isolate(() -> {
33 assertNotEquals(originalThread, Thread.currentThread());
34 return Future.value(1);
35 });
36
37 assertEquals(new Integer(1), get(future));
38 } finally {
39 es.shutdown();
40 }
41 }
42
43 @Test
44 public void async() throws CheckedFutureException {
45 ExecutorService es = Executors.newCachedThreadPool();
46 try {
47 FuturePool pool = FuturePool.apply(es);
48 Thread originalThread = Thread.currentThread();
49
50 Future<Integer> future = pool.async(() -> {
51 assertNotEquals(originalThread, Thread.currentThread());
52 return 1;
53 });
54
55 assertEquals(new Integer(1), get(future));
56 } finally {
57 es.shutdown();
58 }
59 }
60
61 @Test(expected = IllegalStateException.class)
62 public void asyncFailure() throws CheckedFutureException {
63 ExecutorService es = Executors.newCachedThreadPool();
64 try {
65 FuturePool pool = FuturePool.apply(es);
66 Thread originalThread = Thread.currentThread();
67
68 Future<Integer> future = pool.async(() -> {
69 assertNotEquals(originalThread, Thread.currentThread());
70 throw new IllegalStateException();
71 });
72
73 get(future);
74 } finally {
75 es.shutdown();
76 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…