@Test public void test_PromiseからFutureを作りmapすることができる() throws Throwable { ExecutorService es = Executors.newCachedThreadPool(); Integer value = 10; Promise<Integer> p = PromiseFactory.successful(value); Future<String> f = p.toFuture() .map( es, new Function1<Integer, String>() { @Override public String apply(Integer value) { return "(((" + value + ")))"; } }) .map( es, new Function1<String, String>() { @Override public String apply(String value) { return "<" + value + ">"; } }); String result = f.result(); assertThat(result, is("<(((10)))>")); }
@Test public void test_成功したPromiseから値を取得できる() throws Throwable { Integer value = 10; Promise<Integer> p = PromiseFactory.successful(value); Future<Integer> f = p.toFuture(); Integer result = f.result(); assertThat(result, is(value)); }
@Test public void test_Promiseを完了させ値を取得することができる() throws Throwable { Integer value = 10; Promise<Integer> p = PromiseFactory.create(); p.complete(Try.ofSuccess(value)); Future<Integer> f = p.toFuture(); Integer result = f.result(); assertThat(result, is(value)); }
@Test public void test_失敗したPromiseから値を取得できる() throws Throwable { Integer value = 10; Promise<Integer> p = PromiseFactory.failed(new RuntimeException()); Future<Integer> f = p.toFuture(); try { f.result(); fail(); } catch (RuntimeException e) { } }
@Test public void test_PromiseからFutureを作りflatMapすることができる() throws Throwable { ExecutorService es = Executors.newCachedThreadPool(); Integer value = 10; Promise<Integer> p = PromiseFactory.successful(value); Future<String> f = p.toFuture() .flatMap( es, new Function1<Integer, Future<String>>() { @Override public Future<String> apply(Integer value) { System.out.println("--- start ---"); Future<String> result = FutureFactory.successful("--- " + value + " ---"); System.out.println("--- end ---"); return result; } }); String result = f.result(); assertThat(result, is("--- 10 ---")); }