Search in sources :

Example 6 with Context

use of com.rabbitmq.stream.MessageHandler.Context in project spring-amqp by spring-projects.

the class StreamListenerContainerTests method testAdviceChain.

@Test
void testAdviceChain() throws Exception {
    Environment env = mock(Environment.class);
    ConsumerBuilder builder = mock(ConsumerBuilder.class);
    given(env.consumerBuilder()).willReturn(builder);
    AtomicReference<MessageHandler> handler = new AtomicReference<>();
    willAnswer(inv -> {
        handler.set(inv.getArgument(0));
        return null;
    }).given(builder).messageHandler(any());
    AtomicBoolean advised = new AtomicBoolean();
    MethodInterceptor advice = (inv) -> {
        advised.set(true);
        return inv.proceed();
    };
    StreamListenerContainer container = new StreamListenerContainer(env);
    container.setAdviceChain(advice);
    AtomicBoolean called = new AtomicBoolean();
    MessageListener ml = mock(MessageListener.class);
    willAnswer(inv -> {
        called.set(true);
        return null;
    }).given(ml).onMessage(any());
    container.setupMessageListener(ml);
    Message message = mock(Message.class);
    given(message.getBodyAsBinary()).willReturn("foo".getBytes());
    Context context = mock(Context.class);
    handler.get().handle(context, message);
    assertThat(advised.get()).isTrue();
    assertThat(called.get()).isTrue();
    advised.set(false);
    called.set(false);
    ChannelAwareMessageListener cal = mock(ChannelAwareMessageListener.class);
    willAnswer(inv -> {
        called.set(true);
        return null;
    }).given(cal).onMessage(any(), isNull());
    container.setupMessageListener(cal);
    handler.get().handle(context, message);
    assertThat(advised.get()).isTrue();
    assertThat(called.get()).isTrue();
    called.set(false);
    StreamMessageListener sml = mock(StreamMessageListener.class);
    willAnswer(inv -> {
        called.set(true);
        return null;
    }).given(sml).onStreamMessage(message, context);
    container.setupMessageListener(sml);
    handler.get().handle(context, message);
    assertThat(advised.get()).isTrue();
    assertThat(called.get()).isTrue();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.isNull(org.mockito.ArgumentMatchers.isNull) MessageListener(org.springframework.amqp.core.MessageListener) MessageHandler(com.rabbitmq.stream.MessageHandler) Message(com.rabbitmq.stream.Message) Environment(com.rabbitmq.stream.Environment) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) BDDMockito.willAnswer(org.mockito.BDDMockito.willAnswer) Test(org.junit.jupiter.api.Test) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Context(com.rabbitmq.stream.MessageHandler.Context) BDDMockito.given(org.mockito.BDDMockito.given) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) ConsumerBuilder(com.rabbitmq.stream.ConsumerBuilder) Mockito.mock(org.mockito.Mockito.mock) Context(com.rabbitmq.stream.MessageHandler.Context) MessageHandler(com.rabbitmq.stream.MessageHandler) Message(com.rabbitmq.stream.Message) MessageListener(org.springframework.amqp.core.MessageListener) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Environment(com.rabbitmq.stream.Environment) ConsumerBuilder(com.rabbitmq.stream.ConsumerBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

Context (com.rabbitmq.stream.MessageHandler.Context)6 Test (org.junit.jupiter.api.Test)6 TrackingConfiguration (com.rabbitmq.stream.impl.StreamConsumerBuilder.TrackingConfiguration)5 Duration (java.time.Duration)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Registration (com.rabbitmq.stream.impl.OffsetTrackingCoordinator.Registration)3 TestUtils.answer (com.rabbitmq.stream.impl.TestUtils.answer)3 TestUtils.latchAssert (com.rabbitmq.stream.impl.TestUtils.latchAssert)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Executors (java.util.concurrent.Executors)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Consumer (java.util.function.Consumer)3 LongConsumer (java.util.function.LongConsumer)3 IntStream (java.util.stream.IntStream)3 AfterEach (org.junit.jupiter.api.AfterEach)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 ArgumentCaptor (org.mockito.ArgumentCaptor)3 Mock (org.mockito.Mock)3 Mockito (org.mockito.Mockito)3