@Test public void reduceForJavaApiMustWork() throws Exception { LinkedList<Future<String>> listFutures = new LinkedList<Future<String>>(); StringBuilder expected = new StringBuilder(); for (int i = 0; i < 10; i++) { expected.append("test"); listFutures.add( Futures.future( new Callable<String>() { public String call() { return "test"; } }, system.dispatcher())); } Future<String> result = Futures.reduce( listFutures, new Function2<String, String, String>() { public String apply(String r, String t) { return r + t; } }, system.dispatcher()); assertEquals(Await.result(result, timeout), expected.toString()); }
@Test public void findForJavaApiMustWork() throws Exception { LinkedList<Future<Integer>> listFutures = new LinkedList<Future<Integer>>(); for (int i = 0; i < 10; i++) { final Integer fi = i; listFutures.add( Futures.future( new Callable<Integer>() { public Integer call() { return fi; } }, system.dispatcher())); } final Integer expect = 5; Future<Option<Integer>> f = Futures.find( listFutures, new Function<Integer, Boolean>() { public Boolean apply(Integer i) { return i == 5; } }, system.dispatcher()); assertEquals(expect, Await.result(f, timeout).get()); }
// TODO: Improve this test, perhaps with an Actor @Test public void mustSequenceAFutureList() throws Exception { LinkedList<Future<String>> listFutures = new LinkedList<Future<String>>(); LinkedList<String> listExpected = new LinkedList<String>(); for (int i = 0; i < 10; i++) { listExpected.add("test"); listFutures.add( Futures.future( new Callable<String>() { public String call() { return "test"; } }, system.dispatcher())); } Future<Iterable<String>> futureList = Futures.sequence(listFutures, system.dispatcher()); assertEquals(Await.result(futureList, timeout), listExpected); }
@Test public void mustBeAbleToMapAFuture() throws Exception { Future<String> f1 = Futures.future( new Callable<String>() { public String call() { return "Hello"; } }, system.dispatcher()); Future<String> f2 = f1.map( new Mapper<String, String>() { public String apply(String s) { return s + " World"; } }, system.dispatcher()); assertEquals("Hello World", Await.result(f2, timeout)); }