use of org.mule.runtime.api.util.concurrent.Latch in project mule by mulesoft.
the class OnceTestCase method concurrentRun.
@Test
public void concurrentRun() {
Latch controlLath = new Latch();
Latch testLath = new Latch();
CountingRunnable runnable = new CountingRunnable();
RunOnce once = Once.of(runnable);
new Thread(() -> {
await(controlLath);
once.runOnce();
}).start();
new Thread(() -> {
controlLath.release();
once.runOnce();
testLath.release();
}).start();
await(testLath);
assertThat(runnable.getInvokationCount(), is(1));
}
use of org.mule.runtime.api.util.concurrent.Latch in project mule by mulesoft.
the class DefaultEventContextTestCase method asyncChild.
@Test
@Description("Parent EventContext only completes once response publisher completes with a value and all child contexts are complete, even when child is run async with a delay.")
public void asyncChild() throws Exception {
child = addChild(parent);
CoreEvent event = testEvent();
Scheduler testScheduler = muleContext.getSchedulerService().ioScheduler();
Latch latch1 = new Latch();
try {
testScheduler.submit(() -> {
child.success(event);
latch1.countDown();
return null;
});
assertParent(is(nullValue()), is(nullValue()), false, false);
parent.success(event);
latch1.await();
assertChild(is(event), is(nullValue()), true);
assertParent(is(event), is(nullValue()), true, true);
} finally {
testScheduler.stop();
}
}
use of org.mule.runtime.api.util.concurrent.Latch in project mule by mulesoft.
the class RegisteredObjectLifecycleTestCase method doSetUp.
@Override
protected void doSetUp() throws Exception {
bean = new DummyBean();
initLatch = new Latch();
startLatch = new Latch();
stopLatch = new Latch();
disposeLatch = new Latch();
}
use of org.mule.runtime.api.util.concurrent.Latch in project mule by mulesoft.
the class CompositeProcessorChainRouterTestCase method asyncDelegateChain.
@Test
@Description("Ensure that when an async scope is used as part of the execution of one of the composite chains then the chain does not complete and the next chains is not executed until the child context completes.")
public void asyncDelegateChain() throws Exception {
Latch latch = new Latch();
Latch asyncLatch = new Latch();
DefaultMessageProcessorChainBuilder delegateBuilder = new DefaultMessageProcessorChainBuilder();
delegateBuilder.chain(event -> {
try {
asyncLatch.countDown();
latch.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return event;
});
async = new AsyncDelegateMessageProcessor(delegateBuilder);
muleContext.getInjector().inject(async);
async.setAnnotations(getAppleFlowComponentLocationAnnotations());
chainRouter = createCompositeProcessorChainRouter(newChain(empty(), async), newChain(empty(), event -> event));
chainRouter.start();
// CompletableFuture is not returned immediately because simply invoking CompositeProcessorChainRouter there is no async
// hand-off and so this blocks until child context completes.
Future<CompletableFuture<Event>> future = scheduler.submit(() -> chainRouter.execute(testEvent()));
asyncLatch.await();
try {
future.get(BLOCK_TIMEOUT, MILLISECONDS);
fail("Timeout expected");
} catch (TimeoutException te) {
}
latch.countDown();
assertThat(future.get(BLOCK_TIMEOUT, MILLISECONDS).get().getMessage(), equalTo(testEvent().getMessage()));
}
use of org.mule.runtime.api.util.concurrent.Latch in project mule by mulesoft.
the class CompositeProcessorChainRouterTestCase method childContextChain.
@Test
@Description("Ensure that when a child context is created as part of the execution of one of the composite chains then the chain does not complete and the next chains is not executed until the child context completes.")
public void childContextChain() throws Exception {
Latch latch = new Latch();
AtomicReference<BaseEventContext> childEventContext = new AtomicReference<>();
MessageProcessorChain chainUsingChildContext = newChain(empty(), event -> {
latch.release();
childEventContext.set(child((BaseEventContext) event.getContext(), empty()));
return event;
});
chainRouter = createCompositeProcessorChainRouter(chainUsingChildContext, newChain(empty(), event -> event));
// CompletableFuture is not returned immediately because simply invoking CompositeProcessorChainRouter there is no async
// hand-off and so this blocks until child context completes.
Future<CompletableFuture<Event>> future = scheduler.submit(() -> chainRouter.execute(testEvent()));
latch.await();
try {
future.get(BLOCK_TIMEOUT, MILLISECONDS);
fail("Timeout expected");
} catch (TimeoutException te) {
}
childEventContext.get().success();
assertThat(future.get(BLOCK_TIMEOUT, MILLISECONDS).get().getMessage(), equalTo(testEvent().getMessage()));
}
Aggregations