@Inject public Baz(AsyncReference<BlockingFoo> asyncRef) { asyncRef.thenAccept((foo) -> this.foo = Optional.ofNullable(foo)); }
@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"); }
@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()); } }
@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()); }