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);
}
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);
}
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();
}
}
}
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());
}
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);
}
Aggregations