Search in sources :

Example 6 with Latch

use of org.mule.runtime.api.util.concurrent.Latch in project mule by mulesoft.

the class ExtensionNotificationsTestCase method sourceFiresNotificationsOnBackPressure.

@Test
public void sourceFiresNotificationsOnBackPressure() throws Exception {
    Latch latch = new Latch();
    String batchFailed = "BATCH_FAILED";
    setUpListener(notification -> {
        if (batchFailed.equals(notification.getAction().getIdentifier())) {
            latch.release();
        }
    });
    Flow flow = (Flow) getFlowConstruct("sourceNotificationsBackPressure");
    flow.start();
    latch.await(10000, MILLISECONDS);
    flow.stop();
    assertThat(listener.getNotifications(), hasSize(greaterThan(3)));
    // Find first BATCH_FAILED
    ExtensionNotification backPressureNotification = listener.getNotifications().stream().filter(n -> batchFailed.equals(n.getAction().getIdentifier())).findFirst().get();
    // Find matching event notifications
    List<ExtensionNotification> notifications = listener.getNotifications().stream().filter(n -> backPressureNotification.getEvent().getCorrelationId().equals(n.getEvent().getCorrelationId())).collect(toList());
    assertThat(notifications, hasSize(4));
    int batchNumber = (Integer) backPressureNotification.getData().getValue();
    ExtensionNotification notification1 = notifications.get(0);
    verifyNewBatch(notification1, batchNumber);
    ExtensionNotification notification2 = notifications.get(1);
    verifyNextBatch(notification2, 10L);
    ExtensionNotification notification3 = notifications.get(2);
    verifyNotificationAndValue(notification3, batchFailed, batchNumber);
    ExtensionNotification notification4 = notifications.get(3);
    verifyBatchTerminated(notification4, batchNumber);
}
Also used : PersonalInfo(org.mule.test.heisenberg.extension.model.PersonalInfo) AbstractExtensionFunctionalTestCase(org.mule.test.module.extension.AbstractExtensionFunctionalTestCase) Test(org.junit.Test) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) ExtensionNotificationListener(org.mule.runtime.api.notification.ExtensionNotificationListener) ExtensionNotification(org.mule.runtime.api.notification.ExtensionNotification) Flow(org.mule.runtime.core.api.construct.Flow) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) Consumer(java.util.function.Consumer) Inject(javax.inject.Inject) NotificationListenerRegistry(org.mule.runtime.api.notification.NotificationListenerRegistry) CountDownLatch(java.util.concurrent.CountDownLatch) Collectors.toList(java.util.stream.Collectors.toList) Latch(org.mule.runtime.api.util.concurrent.Latch) List(java.util.List) SimpleKnockeableDoor(org.mule.test.heisenberg.extension.model.SimpleKnockeableDoor) Matchers.hasSize(org.hamcrest.Matchers.hasSize) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) LinkedList(java.util.LinkedList) HeisenbergExtension(org.mule.test.heisenberg.extension.HeisenbergExtension) ExtensionNotification(org.mule.runtime.api.notification.ExtensionNotification) CountDownLatch(java.util.concurrent.CountDownLatch) Latch(org.mule.runtime.api.util.concurrent.Latch) Flow(org.mule.runtime.core.api.construct.Flow) Test(org.junit.Test)

Example 7 with Latch

use of org.mule.runtime.api.util.concurrent.Latch in project mule by mulesoft.

the class ProcessorChainExecutorTestCase method doProcessAndWait.

private void doProcessAndWait(ImmutableProcessorChainExecutor chainExecutor, Consumer<Result> onSuccess, BiConsumer<Throwable, Result> onError) throws InterruptedException {
    latch = new Latch();
    chainExecutor.process(onSuccess, onError);
    latch.await(300, MILLISECONDS);
}
Also used : Latch(org.mule.runtime.api.util.concurrent.Latch)

Example 8 with Latch

use of org.mule.runtime.api.util.concurrent.Latch in project mule by mulesoft.

the class LifecycleAwareConfigurationInstanceAsyncRetryTestCase method stopWhileConnectivityTestingExecuting.

@Test
public void stopWhileConnectivityTestingExecuting() throws Throwable {
    if (connectionProvider.isPresent()) {
        final Latch testConnectivityInvokedLatch = new Latch();
        final Latch interceptableShutdownLatch = new Latch();
        reset(connectionManager);
        AtomicBoolean stopped = new AtomicBoolean();
        AtomicReference<Throwable> thrownByTestConnectivity = new AtomicReference<>();
        AtomicBoolean testConnectivityFinished = new AtomicBoolean();
        when(connectionManager.getRetryTemplateFor(connectionProvider.get())).thenReturn(retryPolicyTemplate);
        when(connectionManager.testConnectivity(Mockito.any(ConfigurationInstance.class))).then(inv -> {
            testConnectivityInvokedLatch.countDown();
            try {
                interceptableShutdownLatch.await(10, SECONDS);
                assertThat(stopped.get(), is(false));
            } catch (Throwable t) {
                thrownByTestConnectivity.set(t);
            }
            testConnectivityFinished.set(true);
            return success();
        });
        interceptable.initialise();
        interceptable.start();
        assertThat(testConnectivityInvokedLatch.await(10, SECONDS), is(true));
        interceptable.stop();
        stopped.set(true);
        interceptableShutdownLatch.countDown();
        new PollingProber(15000, 1000).check(new JUnitLambdaProbe(() -> {
            return testConnectivityFinished.get();
        }));
        if (thrownByTestConnectivity.get() != null) {
            throw thrownByTestConnectivity.get();
        }
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JUnitLambdaProbe(org.mule.tck.probe.JUnitLambdaProbe) PollingProber(org.mule.tck.probe.PollingProber) Latch(org.mule.runtime.api.util.concurrent.Latch) AtomicReference(java.util.concurrent.atomic.AtomicReference) ConfigurationInstance(org.mule.runtime.extension.api.runtime.config.ConfigurationInstance) Test(org.junit.Test)

Example 9 with Latch

use of org.mule.runtime.api.util.concurrent.Latch in project mule by mulesoft.

the class HeisenbergRouters method concurrentRouteExecutor.

public void concurrentRouteExecutor(WhenRoute when, RouterCompletionCallback callback) {
    Consumer<Chain> processor = (chain) -> {
        final Latch latch = new Latch();
        chain.process((result -> latch.release()), (error, result) -> latch.release());
        try {
            latch.await(10000, MILLISECONDS);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    };
    Thread first = new Thread(() -> processor.accept(when.getChain()));
    Thread second = new Thread(() -> processor.accept(when.getChain()));
    first.start();
    second.start();
    try {
        first.join();
        second.join();
    } catch (Exception e) {
        callback.error(e);
    }
    callback.success(Result.builder().output("SUCCESS").build());
}
Also used : Summary(org.mule.runtime.extension.api.annotation.param.display.Summary) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) Attribute(org.mule.test.heisenberg.extension.model.Attribute) Preconditions.checkArgument(org.mule.runtime.api.util.Preconditions.checkArgument) OtherwiseRoute(org.mule.test.heisenberg.extension.route.OtherwiseRoute) RouterCompletionCallback(org.mule.runtime.extension.api.runtime.process.RouterCompletionCallback) DrugKillingRoute(org.mule.test.heisenberg.extension.route.DrugKillingRoute) AfterCall(org.mule.test.heisenberg.extension.route.AfterCall) Collectors.toMap(java.util.stream.Collectors.toMap) MuleException(org.mule.runtime.api.exception.MuleException) BeforeCall(org.mule.test.heisenberg.extension.route.BeforeCall) Map(java.util.Map) Chain(org.mule.runtime.extension.api.runtime.route.Chain) VoidCompletionCallback(org.mule.runtime.extension.api.runtime.process.VoidCompletionCallback) Optional(org.mule.runtime.extension.api.annotation.param.Optional) Parameter(org.mule.runtime.extension.api.annotation.param.Parameter) Disposable(org.mule.runtime.api.lifecycle.Disposable) Startable(org.mule.runtime.api.lifecycle.Startable) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) Result(org.mule.runtime.extension.api.runtime.operation.Result) Consumer(java.util.function.Consumer) Latch(org.mule.runtime.api.util.concurrent.Latch) List(java.util.List) WhenRoute(org.mule.test.heisenberg.extension.route.WhenRoute) Stoppable(org.mule.runtime.api.lifecycle.Stoppable) NullSafe(org.mule.runtime.extension.api.annotation.param.NullSafe) KillingRoute(org.mule.test.heisenberg.extension.route.KillingRoute) NOT_SUPPORTED(org.mule.runtime.api.meta.ExpressionSupport.NOT_SUPPORTED) Expression(org.mule.runtime.extension.api.annotation.Expression) Chain(org.mule.runtime.extension.api.runtime.route.Chain) Latch(org.mule.runtime.api.util.concurrent.Latch) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) MuleException(org.mule.runtime.api.exception.MuleException)

Example 10 with Latch

use of org.mule.runtime.api.util.concurrent.Latch in project mule by mulesoft.

the class AbstractMuleContextTestCase method startMuleContext.

private void startMuleContext() throws MuleException, InterruptedException {
    final AtomicReference<Latch> contextStartedLatch = new AtomicReference<>();
    contextStartedLatch.set(new Latch());
    // Do not inline it, otherwise the type of the listener is lost
    final MuleContextNotificationListener<MuleContextNotification> listener = new MuleContextNotificationListener<MuleContextNotification>() {

        @Override
        public boolean isBlocking() {
            return false;
        }

        @Override
        public void onNotification(MuleContextNotification notification) {
            contextStartedLatch.get().countDown();
        }
    };
    muleContext.getNotificationManager().addListener(listener);
    muleContext.start();
    contextStartedLatch.get().await(20, SECONDS);
}
Also used : Latch(org.mule.runtime.api.util.concurrent.Latch) MuleContextNotificationListener(org.mule.runtime.core.api.context.notification.MuleContextNotificationListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) MuleContextNotification(org.mule.runtime.core.api.context.notification.MuleContextNotification)

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