Search in sources :

Example 1 with Latch

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));
}
Also used : RunOnce(org.mule.runtime.core.api.util.func.Once.RunOnce) Latch(org.mule.runtime.api.util.concurrent.Latch) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 2 with Latch

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();
    }
}
Also used : CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Scheduler(org.mule.runtime.api.scheduler.Scheduler) Latch(org.mule.runtime.api.util.concurrent.Latch) Description(io.qameta.allure.Description) Test(org.junit.Test)

Example 3 with Latch

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();
}
Also used : Latch(org.mule.runtime.api.util.concurrent.Latch)

Example 4 with 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()));
}
Also used : DefaultMessageProcessorChainBuilder(org.mule.runtime.core.privileged.processor.chain.DefaultMessageProcessorChainBuilder) CompletableFuture(java.util.concurrent.CompletableFuture) Latch(org.mule.runtime.api.util.concurrent.Latch) AsyncDelegateMessageProcessor(org.mule.runtime.core.internal.processor.AsyncDelegateMessageProcessor) TimeoutException(java.util.concurrent.TimeoutException) Description(io.qameta.allure.Description) Test(org.junit.Test)

Example 5 with Latch

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()));
}
Also used : BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) MessageProcessorChain(org.mule.runtime.core.privileged.processor.chain.MessageProcessorChain) CompletableFuture(java.util.concurrent.CompletableFuture) Latch(org.mule.runtime.api.util.concurrent.Latch) AtomicReference(java.util.concurrent.atomic.AtomicReference) TimeoutException(java.util.concurrent.TimeoutException) Description(io.qameta.allure.Description) Test(org.junit.Test)

Aggregations

Latch (org.mule.runtime.api.util.concurrent.Latch)24 Test (org.junit.Test)17 Queue (org.mule.runtime.core.api.util.queue.Queue)7 QueueManager (org.mule.runtime.core.api.util.queue.QueueManager)7 QueueSession (org.mule.runtime.core.api.util.queue.QueueSession)7 AbstractQueueManager (org.mule.runtime.core.internal.util.queue.AbstractQueueManager)7 Description (io.qameta.allure.Description)3 List (java.util.List)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 MILLISECONDS (java.util.concurrent.TimeUnit.MILLISECONDS)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Consumer (java.util.function.Consumer)3 Serializable (java.io.Serializable)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 TimeoutException (java.util.concurrent.TimeoutException)2 Assert.assertThat (org.junit.Assert.assertThat)2 ExpectedException (org.junit.rules.ExpectedException)2 MuleException (org.mule.runtime.api.exception.MuleException)2 Reference (org.mule.runtime.api.util.Reference)2 DefaultQueueConfiguration (org.mule.runtime.core.api.util.queue.DefaultQueueConfiguration)2