Search in sources :

Example 6 with WrapperMessageBuilder

use of com.rabbitmq.stream.codec.WrapperMessageBuilder in project rabbitmq-stream-java-client by rabbitmq.

the class ConsumersCoordinatorTest method shouldRedistributeConsumerIfConnectionIsLost.

@Test
void shouldRedistributeConsumerIfConnectionIsLost() throws Exception {
    scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
    when(environment.scheduledExecutorService()).thenReturn(scheduledExecutorService);
    Duration retryDelay = Duration.ofMillis(100);
    when(environment.recoveryBackOffDelayPolicy()).thenReturn(BackOffDelayPolicy.fixed(retryDelay));
    when(consumer.isOpen()).thenReturn(true);
    when(locator.metadata("stream")).thenReturn(metadata(null, replica())).thenReturn(// for the second consumer
    metadata(null, replica())).thenReturn(metadata(null, Collections.emptyList())).thenReturn(metadata(null, Collections.emptyList())).thenReturn(metadata(null, replica()));
    when(clientFactory.client(any())).thenReturn(client);
    when(client.subscribe(subscriptionIdCaptor.capture(), anyString(), any(OffsetSpecification.class), anyInt(), anyMap())).thenReturn(new Client.Response(Constants.RESPONSE_CODE_OK));
    StreamConsumer consumerClosedAfterConnectionLost = mock(StreamConsumer.class);
    when(consumerClosedAfterConnectionLost.isOpen()).thenReturn(false);
    AtomicInteger messageHandlerCalls = new AtomicInteger();
    Runnable closingRunnable = coordinator.subscribe(consumer, "stream", OffsetSpecification.first(), null, NO_OP_SUBSCRIPTION_LISTENER, (offset, message) -> messageHandlerCalls.incrementAndGet());
    verify(clientFactory, times(1)).client(any());
    verify(client, times(1)).subscribe(anyByte(), anyString(), any(OffsetSpecification.class), anyInt(), anyMap());
    assertThat(messageHandlerCalls.get()).isEqualTo(0);
    messageListener.handle(subscriptionIdCaptor.getAllValues().get(0), 1, 0, new WrapperMessageBuilder().build());
    assertThat(messageHandlerCalls.get()).isEqualTo(1);
    coordinator.subscribe(consumerClosedAfterConnectionLost, "stream", OffsetSpecification.first(), null, NO_OP_SUBSCRIPTION_LISTENER, (offset, message) -> {
    });
    verify(client, times(1 + 1)).subscribe(anyByte(), anyString(), any(OffsetSpecification.class), anyInt(), anyMap());
    shutdownListener.handle(new Client.ShutdownContext(Client.ShutdownContext.ShutdownReason.UNKNOWN));
    Thread.sleep(retryDelay.toMillis() * 5);
    // the second consumer does not re-subscribe because it returns it is not open
    verify(client, times(2 + 1)).subscribe(anyByte(), anyString(), any(OffsetSpecification.class), anyInt(), anyMap());
    assertThat(messageHandlerCalls.get()).isEqualTo(1);
    messageListener.handle(subscriptionIdCaptor.getAllValues().get(0), 0, 0, new WrapperMessageBuilder().build());
    assertThat(messageHandlerCalls.get()).isEqualTo(2);
    when(client.unsubscribe(subscriptionIdCaptor.getValue())).thenReturn(new Client.Response(Constants.RESPONSE_CODE_OK));
    closingRunnable.run();
    verify(client, times(1)).unsubscribe(subscriptionIdCaptor.getValue());
    messageListener.handle(subscriptionIdCaptor.getValue(), 0, 0, new WrapperMessageBuilder().build());
    assertThat(messageHandlerCalls.get()).isEqualTo(2);
}
Also used : OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WrapperMessageBuilder(com.rabbitmq.stream.codec.WrapperMessageBuilder) Duration(java.time.Duration) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with WrapperMessageBuilder

use of com.rabbitmq.stream.codec.WrapperMessageBuilder in project rabbitmq-stream-java-client by rabbitmq.

the class ConsumersCoordinatorTest method metadataUpdate_shouldCloseConsumerIfRetryTimeoutIsReached.

@Test
void metadataUpdate_shouldCloseConsumerIfRetryTimeoutIsReached() throws Exception {
    Duration retryTimeout = Duration.ofMillis(200);
    BackOffDelayPolicy delayPolicy = fixedWithInitialDelay(ms(50), ms(50), ms(200));
    when(environment.topologyUpdateBackOffDelayPolicy()).thenReturn(delayPolicy);
    scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
    when(environment.scheduledExecutorService()).thenReturn(scheduledExecutorService);
    when(consumer.isOpen()).thenReturn(true);
    when(locator.metadata("stream")).thenReturn(metadata(null, replicas())).thenThrow(new IllegalStateException());
    when(clientFactory.client(any())).thenReturn(client);
    when(client.subscribe(subscriptionIdCaptor.capture(), anyString(), any(OffsetSpecification.class), anyInt(), anyMap())).thenReturn(new Client.Response(Constants.RESPONSE_CODE_OK));
    AtomicInteger messageHandlerCalls = new AtomicInteger();
    coordinator.subscribe(consumer, "stream", OffsetSpecification.first(), null, NO_OP_SUBSCRIPTION_LISTENER, (offset, message) -> messageHandlerCalls.incrementAndGet());
    verify(clientFactory, times(1)).client(any());
    verify(client, times(1)).subscribe(anyByte(), anyString(), any(OffsetSpecification.class), anyInt(), anyMap());
    assertThat(messageHandlerCalls.get()).isEqualTo(0);
    messageListener.handle(subscriptionIdCaptor.getValue(), 1, 0, new WrapperMessageBuilder().build());
    assertThat(messageHandlerCalls.get()).isEqualTo(1);
    metadataListener.handle("stream", Constants.RESPONSE_CODE_STREAM_NOT_AVAILABLE);
    Thread.sleep(delayPolicy.delay(0).toMillis() + retryTimeout.toMillis() * 2);
    verify(consumer, times(1)).closeAfterStreamDeletion();
    verify(client, times(1)).subscribe(anyByte(), anyString(), any(OffsetSpecification.class), anyInt(), anyMap());
    verify(client, times(0)).unsubscribe(anyByte());
    assertThat(coordinator.poolSize()).isZero();
}
Also used : OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WrapperMessageBuilder(com.rabbitmq.stream.codec.WrapperMessageBuilder) Duration(java.time.Duration) BackOffDelayPolicy(com.rabbitmq.stream.BackOffDelayPolicy) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with WrapperMessageBuilder

use of com.rabbitmq.stream.codec.WrapperMessageBuilder in project rabbitmq-stream-java-client by rabbitmq.

the class ConsumersCoordinatorTest method shouldRedistributeConsumerOnMetadataUpdate.

@Test
void shouldRedistributeConsumerOnMetadataUpdate() throws Exception {
    BackOffDelayPolicy delayPolicy = fixedWithInitialDelay(ms(100), ms(100));
    when(environment.topologyUpdateBackOffDelayPolicy()).thenReturn(delayPolicy);
    scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
    when(environment.scheduledExecutorService()).thenReturn(scheduledExecutorService);
    when(consumer.isOpen()).thenReturn(true);
    when(locator.metadata("stream")).thenReturn(metadata(null, replicas()));
    when(clientFactory.client(any())).thenReturn(client);
    StreamConsumer consumerClosedAfterMetadataUpdate = mock(StreamConsumer.class);
    when(consumerClosedAfterMetadataUpdate.isOpen()).thenReturn(false);
    when(client.subscribe(subscriptionIdCaptor.capture(), anyString(), any(OffsetSpecification.class), anyInt(), anyMap())).thenReturn(new Client.Response(Constants.RESPONSE_CODE_OK));
    AtomicInteger messageHandlerCalls = new AtomicInteger();
    Runnable closingRunnable = coordinator.subscribe(consumer, "stream", OffsetSpecification.first(), null, NO_OP_SUBSCRIPTION_LISTENER, (offset, message) -> messageHandlerCalls.incrementAndGet());
    verify(clientFactory, times(1)).client(any());
    verify(client, times(1)).subscribe(anyByte(), anyString(), any(OffsetSpecification.class), anyInt(), anyMap());
    coordinator.subscribe(consumerClosedAfterMetadataUpdate, "stream", OffsetSpecification.first(), null, NO_OP_SUBSCRIPTION_LISTENER, (offset, message) -> {
    });
    verify(client, times(1 + 1)).subscribe(anyByte(), anyString(), any(OffsetSpecification.class), anyInt(), anyMap());
    assertThat(messageHandlerCalls.get()).isEqualTo(0);
    firstMessageListener().handle(subscriptionIdCaptor.getAllValues().get(0), 1, 0, new WrapperMessageBuilder().build());
    assertThat(messageHandlerCalls.get()).isEqualTo(1);
    this.metadataListeners.forEach(ml -> ml.handle("stream", Constants.RESPONSE_CODE_STREAM_NOT_AVAILABLE));
    Thread.sleep(delayPolicy.delay(0).toMillis() * 5);
    // the second consumer does not re-subscribe because it returns it is not open
    verify(client, times(2 + 1)).subscribe(anyByte(), anyString(), any(OffsetSpecification.class), anyInt(), anyMap());
    assertThat(messageHandlerCalls.get()).isEqualTo(1);
    lastMessageListener().handle(subscriptionIdCaptor.getAllValues().get(0), 0, 0, new WrapperMessageBuilder().build());
    assertThat(messageHandlerCalls.get()).isEqualTo(2);
    when(client.unsubscribe(subscriptionIdCaptor.getValue())).thenReturn(new Client.Response(Constants.RESPONSE_CODE_OK));
    closingRunnable.run();
    verify(client, times(1)).unsubscribe(subscriptionIdCaptor.getValue());
    lastMessageListener().handle(subscriptionIdCaptor.getValue(), 0, 0, new WrapperMessageBuilder().build());
    assertThat(messageHandlerCalls.get()).isEqualTo(2);
    assertThat(coordinator.poolSize()).isZero();
}
Also used : OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WrapperMessageBuilder(com.rabbitmq.stream.codec.WrapperMessageBuilder) BackOffDelayPolicy(com.rabbitmq.stream.BackOffDelayPolicy) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

OffsetSpecification (com.rabbitmq.stream.OffsetSpecification)8 WrapperMessageBuilder (com.rabbitmq.stream.codec.WrapperMessageBuilder)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Test (org.junit.jupiter.api.Test)6 BackOffDelayPolicy (com.rabbitmq.stream.BackOffDelayPolicy)4 Duration (java.time.Duration)4 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 QueryOffsetResponse (com.rabbitmq.stream.impl.Client.QueryOffsetResponse)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ArgumentMatchers.anyMap (org.mockito.ArgumentMatchers.anyMap)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1