Refine search
/** * Map the result of a future to a specific {@code value}.<p> * * When this future succeeds, this {@code value} will complete the future returned by this method call.<p> * * When this future fails, the failure will be propagated to the returned future. * * @param value the value that eventually completes the mapped future * @return the mapped future */ default <V> Future<V> map(V value) { Future<V> ret = Future.future(); setHandler(ar -> { if (ar.succeeded()) { ret.complete(value); } else { ret.fail(ar.cause()); } }); return ret; }
@Test public void testCompositeFail() { Throwable cause = new Throwable(); Future<Object> f1 = Future.future(); Future<Object> f2 = Future.future(); CompositeFuture composite = CompositeFuture.all(f1, f2); Checker<CompositeFuture> checker = new Checker<>(composite); composite.fail(cause); checker.assertFailed(cause); f1.complete(); f2.complete(); checker.assertFailed(cause); }
@Test public void testToString() { assertEquals("Future{unresolved}", Future.future().toString()); assertEquals("Future{result=abc}", Future.succeededFuture("abc").toString()); assertEquals("Future{cause=It's like that, and that's the way it is}", Future.failedFuture("It's like that, and that's the way it is").toString()); Future<String> f = Future.future(); f.complete("abc"); assertEquals("Future{result=abc}", f.toString()); f = Future.future(); f.fail("abc"); assertEquals("Future{cause=abc}", f.toString()); }
@Test public void testFailFutureToHandler() { Throwable cause = new Throwable(); Consumer<Handler<AsyncResult<String>>> consumer = handler -> { handler.handle(Future.failedFuture(cause)); }; Future<String> fut = Future.future(); consumer.accept(fut); assertTrue(fut.isComplete()); assertTrue(fut.failed()); assertEquals(cause, fut.cause()); }
@Test public void testComposeFails() { RuntimeException cause = new RuntimeException(); Future<String> f1 = Future.future(); Future<Integer> f2 = Future.future(); Checker<Integer> checker = new Checker<>(f2); f1.compose(string -> { throw cause; }, f2); f1.complete("foo"); checker.assertFailed(cause); Future<String> f3 = Future.future(); Future<Integer> f4 = f3.compose(string -> { throw cause; }); checker = new Checker<>(f4); f3.complete("foo"); checker.assertFailed(cause); }
@Test public void testComposeSuccessToFailure() { Throwable cause = new Throwable(); AtomicReference<String> ref = new AtomicReference<>(); Future<Integer> c = Future.future(); Future<String> f3 = Future.future(); Future<Integer> f4 = f3.compose(string -> { ref.set(string); return c; }); Checker<Integer> checker = new Checker<>(f4); f3.complete("abcdef"); c.fail(cause); checker.assertFailed(cause); }
private void testAnyFailed(BiFunction<Future<String>, Future<Integer>, CompositeFuture> any) { Future<String> f1 = Future.future(); Future<Integer> f2 = Future.future(); CompositeFuture composite = any.apply(f1, f2); Checker<CompositeFuture> checker = new Checker<>(composite); f1.fail("failure"); checker.assertNotCompleted(); Throwable cause = new Exception(); f2.fail(cause); checker.assertFailed(cause); }
@Test public void testCompositeFutureToList() { Future<String> f1 = Future.future(); Future<Integer> f2 = Future.future(); CompositeFuture composite = CompositeFuture.all(f1, f2); assertEquals(Arrays.asList(null, null), composite.list()); f1.complete("foo"); assertEquals(Arrays.asList("foo", null), composite.list()); f2.complete(4); assertEquals(Arrays.asList("foo", 4), composite.list()); }
@Test public void testOtherwiseFails() { RuntimeException cause = new RuntimeException("throw"); Future<String> f = Future.future(); Future<String> r = f.otherwise(t -> { throw cause; }); Checker<String> checker = new Checker<>(r); checker.assertNotCompleted(); f.fail("recovered"); checker.assertFailed(cause); }