Search in sources :

Example 6 with ReactiveStreamsConsumer

use of org.springframework.integration.endpoint.ReactiveStreamsConsumer in project spring-integration by spring-projects.

the class ReactiveStreamsConsumerTests method testReactiveStreamsConsumerFluxMessageChannel.

@Test
public void testReactiveStreamsConsumerFluxMessageChannel() throws InterruptedException {
    FluxMessageChannel testChannel = new FluxMessageChannel();
    List<Message<?>> result = new LinkedList<>();
    CountDownLatch stopLatch = new CountDownLatch(2);
    MessageHandler messageHandler = m -> {
        result.add(m);
        stopLatch.countDown();
    };
    MessageHandler testSubscriber = new MethodInvokingMessageHandler(messageHandler, (String) null);
    ReactiveStreamsConsumer reactiveConsumer = new ReactiveStreamsConsumer(testChannel, testSubscriber);
    reactiveConsumer.setBeanFactory(mock(BeanFactory.class));
    reactiveConsumer.afterPropertiesSet();
    reactiveConsumer.start();
    Message<?> testMessage = new GenericMessage<>("test");
    testChannel.send(testMessage);
    reactiveConsumer.stop();
    try {
        testChannel.send(testMessage);
    } catch (Exception e) {
        assertThat(e, instanceOf(MessageDeliveryException.class));
        assertThat(e.getCause(), instanceOf(IllegalStateException.class));
        assertThat(e.getMessage(), containsString("doesn't have subscribers to accept messages"));
    }
    reactiveConsumer.start();
    Message<?> testMessage2 = new GenericMessage<>("test2");
    testChannel.send(testMessage2);
    assertTrue(stopLatch.await(10, TimeUnit.SECONDS));
    assertThat(result, Matchers.<Message<?>>contains(testMessage, testMessage2));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) QueueChannel(org.springframework.integration.channel.QueueChannel) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) ReactiveStreamsConsumer(org.springframework.integration.endpoint.ReactiveStreamsConsumer) Assert.assertSame(org.junit.Assert.assertSame) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) Assert.fail(org.junit.Assert.fail) Message(org.springframework.messaging.Message) LinkedList(java.util.LinkedList) Subscriber(org.reactivestreams.Subscriber) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) Mockito.times(org.mockito.Mockito.times) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) MethodInvokingMessageHandler(org.springframework.integration.handler.MethodInvokingMessageHandler) ConsumerEndpointFactoryBean(org.springframework.integration.config.ConsumerEndpointFactoryBean) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) BDDMockito.willAnswer(org.mockito.BDDMockito.willAnswer) Mockito.verify(org.mockito.Mockito.verify) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Mockito.never(org.mockito.Mockito.never) List(java.util.List) MessageHandler(org.springframework.messaging.MessageHandler) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Subscription(org.reactivestreams.Subscription) BeanFactory(org.springframework.beans.factory.BeanFactory) GenericMessage(org.springframework.messaging.support.GenericMessage) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) DirectChannel(org.springframework.integration.channel.DirectChannel) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.mock(org.mockito.Mockito.mock) MethodInvokingMessageHandler(org.springframework.integration.handler.MethodInvokingMessageHandler) ReactiveStreamsConsumer(org.springframework.integration.endpoint.ReactiveStreamsConsumer) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MethodInvokingMessageHandler(org.springframework.integration.handler.MethodInvokingMessageHandler) MessageHandler(org.springframework.messaging.MessageHandler) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedList(java.util.LinkedList) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) GenericMessage(org.springframework.messaging.support.GenericMessage) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) Test(org.junit.Test)

Aggregations

ReactiveStreamsConsumer (org.springframework.integration.endpoint.ReactiveStreamsConsumer)6 Message (org.springframework.messaging.Message)4 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)3 Test (org.junit.Test)3 Subscriber (org.reactivestreams.Subscriber)3 Subscription (org.reactivestreams.Subscription)3 BeanFactory (org.springframework.beans.factory.BeanFactory)3 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)3 AbstractEndpoint (org.springframework.integration.endpoint.AbstractEndpoint)3 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)3 PollingConsumer (org.springframework.integration.endpoint.PollingConsumer)3 PollableChannel (org.springframework.messaging.PollableChannel)3 SubscribableChannel (org.springframework.messaging.SubscribableChannel)3 GenericMessage (org.springframework.messaging.support.GenericMessage)3 DirectChannel (org.springframework.integration.channel.DirectChannel)2 QueueChannel (org.springframework.integration.channel.QueueChannel)2 MessageChannel (org.springframework.messaging.MessageChannel)2 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)2 LinkedList (java.util.LinkedList)1 List (java.util.List)1