/** * 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()); } }
CompletionStage<String> hello() { return foo.thenApply(BlockingFoo::getMessage); }
@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()); }
@SuppressWarnings("serial") @Test public void testAsyncReferenceDynamicLookup() throws InterruptedException, ExecutionException { BlockingFoo.reset(); BlockingBarProducer.reset(); Boss.DESTROYED.set(false); List<Boolean> stageResults = new CopyOnWriteArrayList<>(); AsyncReference<Boss> ref = weld.select(new TypeLiteral<AsyncReference<Boss>>() { }).get(); ref.thenAccept((boss) -> stageResults.add(true)); assertEquals(0, stageResults.size()); Awaitility.await().atMost(Timeouts.DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS).until(() -> ref.isDone()); BlockingFoo.complete("Foo"); BlockingBarProducer.complete(152); Awaitility.await().atMost(Timeouts.DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS).until(() -> ref.get().isReadyToTest()); assertEquals(1, stageResults.size()); ref.whenComplete((r, t) -> { if (r != null) stageResults.add(true); }); assertEquals(2, stageResults.size()); assertEquals(152, ref.get().juicyBar.get().code); weld.destroy(ref); assertTrue(Boss.DESTROYED.get()); }
/** * Gets the reference or the default value. * * @param defaultValue * @return the reference or the default value if the reference is {@code null} */ default T orElse(T defaultValue) { T value = get(); return value != null ? value : defaultValue; }
@Inject public Baz(AsyncReference<BlockingFoo> asyncRef) { asyncRef.thenAccept((foo) -> this.foo = Optional.ofNullable(foo)); }
CompletionStage<String> hello() { return alphaReference.thenCompose(alpha -> // At this point BlockingAlpha is ready // But getMessage() is also blocking worker.performBlocking(alpha::getMessage) // Finally modify the final message .thenApply(m -> "Hello " + m + "!")); }
/** * Gets the reference or the default value. * * @param defaultValue * @return the reference or the default value if the reference is {@code null} */ default T orElse(T defaultValue) { T value = get(); return value != null ? value : defaultValue; }
@Test public void testAsyncReferenceCompletionStage(TestContext context) throws InterruptedException { BlockingFoo.reset(); Async async = context.async(2); Hello hello = weld.select(Hello.class).get(); hello.hello().thenAccept((m) -> { context.assertEquals("Foo", m); async.complete(); }); BlockingFoo.complete("Foo"); Awaitility.await().atMost(Timeouts.DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS).until(() -> hello.foo.isDone()); hello.hello().thenAccept((m) -> { context.assertEquals("Foo", m); async.complete(); }); }
@SuppressWarnings("serial") @Test public void testAsyncReferenceDynamicLookupSimple(TestContext context) throws InterruptedException { BlockingFoo.reset(); Async async = context.async(); AtomicBoolean created = new AtomicBoolean(false); weld.select(new TypeLiteral<AsyncReference<BlockingFoo>>() { }).get().thenAccept((foo) -> { context.assertEquals("Foo", foo.getMessage()); created.set(true); async.complete(); }); assertFalse(created.get()); BlockingFoo.complete("Foo"); }
/** * 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()); } }
@SuppressWarnings("serial") @Test public void testAsyncReferenceNormalScoped(TestContext context) throws InterruptedException { NormalScopedBlockingFoo.reset(); Async async = context.async(); AtomicBoolean created = new AtomicBoolean(false); weld.select(new TypeLiteral<AsyncReference<NormalScopedBlockingFoo>>() { }).get().thenAccept((foo) -> { context.assertTrue(NormalScopedBlockingFoo.created.get()); context.assertEquals("Foo", foo.getMessage()); created.set(true); async.complete(); }); assertFalse(created.get()); NormalScopedBlockingFoo.complete("Foo"); Awaitility.await().atMost(Timeouts.DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS).until(() -> created.get()); } }