use of org.springframework.core.log.LogAccessor in project spring-integration by spring-projects.
the class EnableIntegrationTests method testAnnotatedServiceActivator.
@Test
public void testAnnotatedServiceActivator() throws Exception {
this.serviceActivatorEndpoint.start();
assertThat(this.inputReceiveLatch.await(10, TimeUnit.SECONDS)).isTrue();
assertThat(TestUtils.getPropertyValue(this.serviceActivatorEndpoint, "maxMessagesPerPoll")).isEqualTo(10L);
Trigger trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint, "trigger", Trigger.class);
assertThat(trigger).isInstanceOf(PeriodicTrigger.class);
assertThat(TestUtils.getPropertyValue(trigger, "period")).isEqualTo(100L);
assertThat(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class)).isFalse();
assertThat(this.annotationTestService.isRunning()).isTrue();
LogAccessor logger = spy(TestUtils.getPropertyValue(this.serviceActivatorEndpoint, "logger", LogAccessor.class));
when(logger.isDebugEnabled()).thenReturn(true);
final CountDownLatch pollerInterruptedLatch = new CountDownLatch(1);
doAnswer(invocation -> {
pollerInterruptedLatch.countDown();
invocation.callRealMethod();
return null;
}).when(logger).debug("Received no Message during the poll, returning 'false'");
new DirectFieldAccessor(this.serviceActivatorEndpoint).setPropertyValue("logger", logger);
this.serviceActivatorEndpoint.stop();
assertThat(this.annotationTestService.isRunning()).isFalse();
// wait until the service activator's poller is interrupted.
assertThat(pollerInterruptedLatch.await(10, TimeUnit.SECONDS)).isTrue();
this.serviceActivatorEndpoint.start();
assertThat(this.annotationTestService.isRunning()).isTrue();
trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint1, "trigger", Trigger.class);
assertThat(trigger).isInstanceOf(PeriodicTrigger.class);
assertThat(TestUtils.getPropertyValue(trigger, "period")).isEqualTo(100L);
assertThat(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class)).isTrue();
trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint2, "trigger", Trigger.class);
assertThat(trigger).isInstanceOf(CronTrigger.class);
assertThat(TestUtils.getPropertyValue(trigger, "expression.expression")).isEqualTo("0 5 7 * * *");
trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint3, "trigger", Trigger.class);
assertThat(trigger).isInstanceOf(PeriodicTrigger.class);
assertThat(TestUtils.getPropertyValue(trigger, "period")).isEqualTo(11L);
assertThat(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class)).isFalse();
trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint4, "trigger", Trigger.class);
assertThat(trigger).isInstanceOf(PeriodicTrigger.class);
assertThat(TestUtils.getPropertyValue(trigger, "period")).isEqualTo(1000L);
assertThat(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class)).isFalse();
assertThat(trigger).isSameAs(this.myTrigger);
trigger = TestUtils.getPropertyValue(this.transformer, "trigger", Trigger.class);
assertThat(trigger).isInstanceOf(PeriodicTrigger.class);
assertThat(TestUtils.getPropertyValue(trigger, "period")).isEqualTo(10L);
assertThat(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class)).isFalse();
this.input.send(MessageBuilder.withPayload("Foo").build());
Message<?> interceptedMessage = this.wireTapChannel.receive(10_000);
assertThat(interceptedMessage).isNotNull();
assertThat(interceptedMessage.getPayload()).isEqualTo("Foo");
Message<?> receive = this.output.receive(10_000);
assertThat(receive).isNotNull();
assertThat(receive.getPayload()).isEqualTo("FOO");
receive = this.wireTapFromOutput.receive(10_000);
assertThat(receive).isNotNull();
assertThat(receive.getPayload()).isEqualTo("FOO");
MessageHistory messageHistory = receive.getHeaders().get(MessageHistory.HEADER_NAME, MessageHistory.class);
assertThat(messageHistory).isNotNull();
String messageHistoryString = messageHistory.toString();
assertThat(messageHistoryString).contains("input");
assertThat(messageHistoryString).contains("annotationTestService.handle.serviceActivator");
assertThat(messageHistoryString).doesNotContain("output");
receive = this.publishedChannel.receive(10_000);
assertThat(receive).isNotNull();
assertThat(receive.getPayload()).isEqualTo("foo");
messageHistory = receive.getHeaders().get(MessageHistory.HEADER_NAME, MessageHistory.class);
assertThat(messageHistory).isNotNull();
messageHistoryString = messageHistory.toString();
assertThat(messageHistoryString).doesNotContain("input");
assertThat(messageHistoryString).doesNotContain("output");
assertThat(messageHistoryString).contains("publishedChannel");
assertThat(this.wireTapChannel.receive(0)).isNull();
assertThat(this.testChannelInterceptor.getInvoked()).isGreaterThan(0);
assertThat(this.fbInterceptorCounter.get()).isGreaterThan(0);
assertThat(this.context.containsBean("annotationTestService.count.inboundChannelAdapter.source")).isTrue();
Object messageSource = this.context.getBean("annotationTestService.count.inboundChannelAdapter.source");
assertThat(messageSource).isInstanceOf(MethodInvokingMessageSource.class);
assertThat(this.counterChannel.receive(10)).isNull();
SmartLifecycle countSA = this.context.getBean("annotationTestService.count.inboundChannelAdapter", SmartLifecycle.class);
assertThat(countSA.isAutoStartup()).isFalse();
assertThat(countSA.getPhase()).isEqualTo(23);
countSA.start();
for (int i = 0; i < 10; i++) {
Message<?> message = this.counterChannel.receive(10_000);
assertThat(message).isNotNull();
assertThat(message.getPayload()).isEqualTo(i + 1);
}
Message<?> message = this.fooChannel.receive(10_000);
assertThat(message).isNotNull();
assertThat(message.getPayload()).isEqualTo("foo");
message = this.fooChannel.receive(10_000);
assertThat(message).isNotNull();
assertThat(message.getPayload()).isEqualTo("foo");
assertThat(this.fooChannel.receive(10)).isNull();
message = this.messageChannel.receive(10_000);
assertThat(message).isNotNull();
assertThat(message.getPayload()).isEqualTo("bar");
assertThat(message.getHeaders().containsKey("foo")).isTrue();
assertThat(message.getHeaders().get("foo")).isEqualTo("FOO");
MessagingTemplate messagingTemplate = new MessagingTemplate(this.controlBusChannel);
assertThat(messagingTemplate.convertSendAndReceive("@pausable.isRunning()", Boolean.class)).isEqualTo(false);
this.controlBusChannel.send(new GenericMessage<>("@pausable.start()"));
assertThat(messagingTemplate.convertSendAndReceive("@pausable.isRunning()", Boolean.class)).isEqualTo(true);
this.controlBusChannel.send(new GenericMessage<>("@pausable.stop()"));
assertThat(messagingTemplate.convertSendAndReceive("@pausable.isRunning()", Boolean.class)).isEqualTo(false);
this.controlBusChannel.send(new GenericMessage<>("@pausable.pause()"));
Object pausable = this.context.getBean("pausable");
assertThat(TestUtils.getPropertyValue(pausable, "paused", Boolean.class)).isTrue();
this.controlBusChannel.send(new GenericMessage<>("@pausable.resume()"));
assertThat(TestUtils.getPropertyValue(pausable, "paused", Boolean.class)).isFalse();
Map<String, ServiceActivatingHandler> beansOfType = this.context.getBeansOfType(ServiceActivatingHandler.class);
assertThat(beansOfType.keySet().contains("enableIntegrationTests.ContextConfiguration2.controlBus.serviceActivator.handler")).isFalse();
assertThat(this.controlBusEndpoint.getBeanName()).isEqualTo(this.contextConfiguration2.controlBusEndpoint.getBeanName());
assertThat(this.controlBusEndpoint.getHandler()).isSameAs(this.contextConfiguration2.controlBusEndpoint.getHandler());
}
use of org.springframework.core.log.LogAccessor in project spring-integration by spring-projects.
the class AvroTests method testTransformers.
@Test
@LogLevels(classes = DirectChannel.class, categories = "bar", level = "DEBUG")
void testTransformers(@Autowired Config config) {
AvroTestClass1 test = new AvroTestClass1("baz", "fiz");
LogAccessor spied = spy(TestUtils.getPropertyValue(config.in1(), "logger", LogAccessor.class));
new DirectFieldAccessor(config.in1()).setPropertyValue("logger", spied);
config.in1().send(new GenericMessage<>(test));
assertThat(config.tapped().receive(0)).isNotNull().extracting(msg -> msg.getPayload()).isInstanceOf(byte[].class);
Message<?> received = config.out().receive(0);
assertThat(received).isNotNull().extracting(msg -> msg.getPayload()).isEqualTo(test).isNotSameAs(test);
assertThat(received.getHeaders().get("flow")).isEqualTo("flow1");
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(spied, atLeastOnce()).debug(captor.capture());
assertThat(captor.getAllValues()).anyMatch(s -> s.contains("preSend on channel"));
assertThat(captor.getAllValues()).anyMatch(s -> s.contains("postSend (sent=true) on channel"));
}
use of org.springframework.core.log.LogAccessor in project spring-integration by spring-projects.
the class MqttAdapterTests method testNoNPEOnReconnectAndStopRaceCondition.
@Test
public void testNoNPEOnReconnectAndStopRaceCondition() throws Exception {
final IMqttClient client = mock(IMqttClient.class);
MqttPahoMessageDrivenChannelAdapter adapter = buildAdapterIn(client, null, ConsumerStopAction.UNSUBSCRIBE_NEVER);
adapter.setRecoveryInterval(10);
MqttException mqttException = new MqttException(MqttException.REASON_CODE_SUBSCRIBE_FAILED);
willThrow(mqttException).given(client).subscribe(any(), ArgumentMatchers.<int[]>any());
LogAccessor logger = spy(TestUtils.getPropertyValue(adapter, "logger", LogAccessor.class));
new DirectFieldAccessor(adapter).setPropertyValue("logger", logger);
CountDownLatch exceptionLatch = new CountDownLatch(1);
ArgumentCaptor<MqttException> mqttExceptionArgumentCaptor = ArgumentCaptor.forClass(MqttException.class);
willAnswer(i -> {
exceptionLatch.countDown();
return null;
}).given(logger).error(mqttExceptionArgumentCaptor.capture(), eq("Exception while connecting and subscribing"));
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.initialize();
adapter.setTaskScheduler(taskScheduler);
adapter.setApplicationEventPublisher(event -> {
if (event instanceof MqttConnectionFailedEvent) {
adapter.destroy();
}
});
adapter.start();
assertThat(exceptionLatch.await(10, TimeUnit.SECONDS)).isTrue();
assertThat(mqttExceptionArgumentCaptor.getValue()).isNotNull().isSameAs(mqttException);
taskScheduler.destroy();
}
use of org.springframework.core.log.LogAccessor in project spring-integration by spring-projects.
the class DownstreamExceptionTests method testNoErrorChannel.
@Test
@SuppressWarnings("unchecked")
public void testNoErrorChannel() throws Exception {
service.n = 0;
LogAccessor logger = spy(TestUtils.getPropertyValue(noErrorChannel, "logger", LogAccessor.class));
final CountDownLatch latch = new CountDownLatch(1);
doAnswer(invocation -> {
if (((Supplier<String>) invocation.getArgument(1)).get().contains("Unhandled")) {
latch.countDown();
}
return null;
}).when(logger).error(any(Throwable.class), any(Supplier.class));
new DirectFieldAccessor(noErrorChannel).setPropertyValue("logger", logger);
MqttPahoMessageHandler adapter = new MqttPahoMessageHandler(MosquittoContainerTest.mqttUrl(), "si-test-out");
adapter.setDefaultTopic("mqtt-fooEx1");
adapter.setBeanFactory(mock(BeanFactory.class));
adapter.afterPropertiesSet();
adapter.start();
adapter.handleMessage(new GenericMessage<>("foo"));
service.barrier.await(10, TimeUnit.SECONDS);
service.barrier.reset();
adapter.handleMessage(new GenericMessage<>("foo"));
service.barrier.await(10, TimeUnit.SECONDS);
assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
verify(logger).error(any(Throwable.class), ArgumentMatchers.<Supplier<String>>argThat(logMessage -> logMessage.get().startsWith("Unhandled exception for")));
service.barrier.reset();
adapter.stop();
}
use of org.springframework.core.log.LogAccessor in project spring-integration by spring-projects.
the class SourcePollingChannelAdapterFactoryBeanTests method testInterrupted.
@Test
public void testInterrupted() throws Exception {
final CountDownLatch startLatch = new CountDownLatch(1);
MessageSource<Object> ms = () -> {
startLatch.countDown();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new MessagingException("Interrupted awaiting stopLatch", e);
}
return null;
};
SourcePollingChannelAdapter pollingChannelAdapter = new SourcePollingChannelAdapter();
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setWaitForTasksToCompleteOnShutdown(true);
taskScheduler.setAwaitTerminationSeconds(1);
taskScheduler.afterPropertiesSet();
pollingChannelAdapter.setTaskScheduler(taskScheduler);
MessagePublishingErrorHandler errorHandler = new MessagePublishingErrorHandler();
Log errorHandlerLogger = TestUtils.getPropertyValue(errorHandler, "logger", Log.class);
errorHandlerLogger = spy(errorHandlerLogger);
DirectFieldAccessor dfa = new DirectFieldAccessor(errorHandler);
dfa.setPropertyValue("logger", errorHandlerLogger);
pollingChannelAdapter.setErrorHandler(errorHandler);
pollingChannelAdapter.setSource(ms);
pollingChannelAdapter.setOutputChannel(new NullChannel());
pollingChannelAdapter.setBeanFactory(mock(BeanFactory.class));
pollingChannelAdapter.afterPropertiesSet();
LogAccessor adapterLogger = TestUtils.getPropertyValue(pollingChannelAdapter, "logger", LogAccessor.class);
adapterLogger = spy(adapterLogger);
when(adapterLogger.isDebugEnabled()).thenReturn(true);
dfa = new DirectFieldAccessor(pollingChannelAdapter);
dfa.setPropertyValue("logger", adapterLogger);
pollingChannelAdapter.start();
assertThat(startLatch.await(10, TimeUnit.SECONDS)).isTrue();
pollingChannelAdapter.stop();
taskScheduler.shutdown();
verifyNoInteractions(errorHandlerLogger);
verify(adapterLogger).debug(ArgumentMatchers.<Supplier<String>>argThat(logMessage -> logMessage.get().contains("Poll interrupted - during stop()?")));
}
Aggregations