/** * If {@link #isDone()} returns <code>true</code>, invoke the specified consumer with the reference (may be {@code null}) and the exception (or {@code null} * if processed successfully). * * @param consumer */ default void ifDone(BiConsumer<? super T, ? super Throwable> consumer) { if (isDone()) { consumer.accept(get(), cause()); } }
/** * If {@link #isDone()} returns <code>true</code>, invoke the specified consumer with the reference (may be {@code null}) and the exception (or {@code null} * if processed successfully). * * @param consumer */ default void ifDone(BiConsumer<? super T, ? super Throwable> consumer) { if (isDone()) { consumer.accept(get(), cause()); } }
@Test public void testAsyncReference() throws InterruptedException, ExecutionException { BlockingFoo.reset(); BlockingBarProducer.reset(); Boss boss = weld.select(Boss.class).get(); assertFalse(boss.foo.isDone()); assertEquals("", boss.foo.orElse(BlockingFoo.EMPTY).getMessage()); boss.foo.ifDone((r, t) -> fail("BlockingFoo not complete yet")); BlockingFoo.complete("Foo"); BlockingBarProducer.complete(152); Awaitility.await().atMost(Timeouts.DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS).until(() -> boss.isReadyToTest()); assertEquals("Foo", boss.foo.get().getMessage()); boss.foo.ifDone((r, t) -> assertEquals("Foo", r.getMessage())); Throwable cause = boss.unsatisfied.cause(); assertNotNull(cause); assertTrue(cause instanceof UnsatisfiedResolutionException); boss.unsatisfied.ifDone((r, t) -> { assertNotNull(t); assertTrue(t instanceof UnsatisfiedResolutionException); }); assertNull(boss.noBing.get()); assertEquals(55, boss.juicyBing.get().value); assertNull(boss.juicyBar.cause()); assertEquals(152, boss.juicyBar.get().code); assertTrue(BlockingBarProducer.PRODUCER_USED.get()); }