use of org.mule.runtime.core.api.source.MessageSource in project mule by mulesoft.
the class DefaultFlowBuilderTestCase method buildsFullFlow.
@Test
public void buildsFullFlow() throws Exception {
Processor processor1 = mock(Processor.class);
Processor processor2 = mock(Processor.class);
List<Processor> messageProcessors = new ArrayList<>();
messageProcessors.add(processor1);
messageProcessors.add(processor2);
MessageSource messageSource = mock(MessageSource.class);
ProcessingStrategyFactory processingStrategyFactory = mock(ProcessingStrategyFactory.class);
ProcessingStrategy processingStrategy = mock(ProcessingStrategy.class);
when(processingStrategyFactory.create(any(), any())).thenReturn(processingStrategy);
FlowExceptionHandler exceptionListener = mock(FlowExceptionHandler.class);
Flow flow = flowBuilder.processors(messageProcessors).source(messageSource).processingStrategyFactory(processingStrategyFactory).messagingExceptionHandler(exceptionListener).build();
assertThat(flow.getName(), equalTo(FLOW_NAME));
assertThat(flow.getMuleContext(), is(muleContext));
assertThat(flow.getProcessors(), contains(processor1, processor2));
assertThat(flow.getSource(), is(messageSource));
assertThat(flow.getExceptionListener(), is(exceptionListener));
assertThat(flow.getProcessingStrategy(), sameInstance(processingStrategy));
}
use of org.mule.runtime.core.api.source.MessageSource in project mule by mulesoft.
the class ModuleFlowProcessingPhase method runPhase.
@Override
public void runPhase(final ModuleFlowProcessingPhaseTemplate template, final MessageProcessContext messageProcessContext, final PhaseResultNotifier phaseResultNotifier) {
try {
final MessageSource messageSource = messageProcessContext.getMessageSource();
final FlowConstruct flowConstruct = (FlowConstruct) componentLocator.find(messageSource.getRootContainerLocation()).get();
final ComponentLocation sourceLocation = messageSource.getLocation();
final Consumer<Either<MessagingException, CoreEvent>> terminateConsumer = getTerminateConsumer(messageSource, template);
final CompletableFuture<Void> responseCompletion = new CompletableFuture<>();
final CoreEvent templateEvent = createEvent(template, sourceLocation, responseCompletion, flowConstruct);
try {
FlowProcessor flowExecutionProcessor = new FlowProcessor(template, flowConstruct.getExceptionListener(), templateEvent);
flowExecutionProcessor.setAnnotations(flowConstruct.getAnnotations());
final SourcePolicy policy = policyManager.createSourcePolicyInstance(messageSource, templateEvent, flowExecutionProcessor, template);
final PhaseContext phaseContext = new PhaseContext(template, messageProcessContext, phaseResultNotifier, terminateConsumer);
just(templateEvent).doOnNext(onMessageReceived(template, messageProcessContext, flowConstruct)).flatMap(request -> from(policy.process(request))).flatMap(policyResult -> policyResult.reduce(policyFailure(phaseContext, flowConstruct, messageSource), policySuccess(phaseContext, flowConstruct, messageSource))).doOnSuccess(aVoid -> phaseResultNotifier.phaseSuccessfully()).doOnError(onFailure(flowConstruct, messageSource, phaseResultNotifier, terminateConsumer)).doAfterTerminate(() -> responseCompletion.complete(null)).subscribe();
} catch (Exception e) {
from(template.sendFailureResponseToClient(new MessagingExceptionResolver(messageProcessContext.getMessageSource()).resolve(new MessagingException(templateEvent, e), muleContext), template.getFailedExecutionResponseParametersFunction().apply(templateEvent))).doOnTerminate(() -> phaseResultNotifier.phaseFailure(e)).subscribe();
}
} catch (Exception t) {
phaseResultNotifier.phaseFailure(t);
}
}
use of org.mule.runtime.core.api.source.MessageSource in project mule by mulesoft.
the class DefaultFlowTestCase method testFailStartingMessageSourceOnLifecycleShouldStopStartedPipelineProcesses.
@Test
public void testFailStartingMessageSourceOnLifecycleShouldStopStartedPipelineProcesses() throws Exception {
// Need to start mule context to have endpoints started during flow start
muleContext.start();
MessageSource mockMessageSource = mock(MessageSource.class, withSettings().extraInterfaces(Startable.class, Stoppable.class));
doThrow(new LifecycleException(mock(I18nMessage.class), mockMessageSource)).when(((Startable) mockMessageSource)).start();
final List<Processor> processors = new ArrayList<>(flow.getProcessors());
Processor mockMessageProcessor = spy(new LifecycleTrackerProcessor());
processors.add(mockMessageProcessor);
after();
flow = (DefaultFlow) Flow.builder(FLOW_NAME, muleContext).source(mockMessageSource).processors(processors).build();
flow.initialise();
try {
flow.start();
fail();
} catch (LifecycleException e) {
}
verify((Startable) mockMessageProcessor, times(1)).start();
verify((Stoppable) mockMessageProcessor, times(1)).stop();
verify((Startable) mockMessageSource, times(1)).start();
verify((Stoppable) mockMessageSource, times(1)).stop();
}
use of org.mule.runtime.core.api.source.MessageSource in project mule by mulesoft.
the class AsyncRequestReplyRequesterTestCase method returnsNullWhenInterruptedWhileWaitingForReply.
@Test
@Ignore("See MULE-8830")
public void returnsNullWhenInterruptedWhileWaitingForReply() throws Exception {
final Latch fakeLatch = new Latch() {
@Override
public void await() throws InterruptedException {
throw new InterruptedException();
}
};
asyncReplyMP = new TestAsyncRequestReplyRequester(muleContext) {
@Override
protected Latch createEventLock() {
return fakeLatch;
}
};
final CountDownLatch processingLatch = new CountDownLatch(1);
Processor target = mock(Processor.class);
asyncReplyMP.setListener(target);
MessageSource messageSource = mock(MessageSource.class);
asyncReplyMP.setReplySource(messageSource);
asyncReplyMP.setMuleContext(muleContext);
final boolean[] exceptionThrown = new boolean[1];
final Object[] responseEvent = new Object[1];
Thread thread = new Thread(() -> {
try {
responseEvent[0] = asyncReplyMP.process(testEvent());
} catch (MuleException e) {
exceptionThrown[0] = true;
} finally {
processingLatch.countDown();
}
});
thread.start();
assertTrue(processingLatch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
assertFalse(exceptionThrown[0]);
assertNull(responseEvent[0]);
}
use of org.mule.runtime.core.api.source.MessageSource in project mule by mulesoft.
the class BackPressureConfigTestCase method assertStrategy.
private void assertStrategy(String flowName, BackPressureStrategy expected) {
MessageSource source = (MessageSource) componentLocator.find(builderFromStringRepresentation(flowName + "/source").build()).get();
assertThat(source.getBackPressureStrategy(), is(expected));
}
Aggregations