Search in sources :

Example 1 with WrapperMessageBuilder

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

the class ConsumersCoordinatorTest method subscribeShouldSubscribeToStreamAndDispatchesMessage_UnsubscribeShouldUnsubscribe.

@Test
void subscribeShouldSubscribeToStreamAndDispatchesMessage_UnsubscribeShouldUnsubscribe() {
    when(locator.metadata("stream")).thenReturn(metadata(null, replicas()));
    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();
    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.getValue(), 0, 0, new WrapperMessageBuilder().build());
    assertThat(messageHandlerCalls.get()).isEqualTo(1);
    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(1);
}
Also used : OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WrapperMessageBuilder(com.rabbitmq.stream.codec.WrapperMessageBuilder) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with WrapperMessageBuilder

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

the class ConsumersCoordinatorTest method metadataUpdate_shouldCloseConsumerIfStreamIsDeleted.

@Test
void metadataUpdate_shouldCloseConsumerIfStreamIsDeleted() throws Exception {
    BackOffDelayPolicy delayPolicy = fixedWithInitialDelay(ms(50), ms(50));
    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())).thenReturn(metadata("stream", null, null, Constants.RESPONSE_CODE_STREAM_DOES_NOT_EXIST));
    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() * 5);
    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) BackOffDelayPolicy(com.rabbitmq.stream.BackOffDelayPolicy) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with WrapperMessageBuilder

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

the class ConsumersCoordinatorTest method shouldRetryRedistributionIfMetadataIsNotUpdatedImmediately.

@Test
void shouldRetryRedistributionIfMetadataIsNotUpdatedImmediately() 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())).thenReturn(metadata(null, Collections.emptyList())).thenReturn(metadata(null, Collections.emptyList())).thenReturn(metadata(null, replicas()));
    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();
    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.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() + delayPolicy.delay(1).toMillis() * 5);
    verify(client, times(2)).subscribe(anyByte(), anyString(), any(OffsetSpecification.class), anyInt(), anyMap());
    assertThat(messageHandlerCalls.get()).isEqualTo(1);
    messageListener.handle(subscriptionIdCaptor.getValue(), 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);
    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)

Example 4 with WrapperMessageBuilder

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

the class ConsumersCoordinatorTest method shouldRestartWhereItLeftOffAfterDisruption.

@ParameterizedTest
@MethodSource("disruptionArguments")
void shouldRestartWhereItLeftOffAfterDisruption(Consumer<ConsumersCoordinatorTest> configurator) throws Exception {
    scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
    when(environment.scheduledExecutorService()).thenReturn(scheduledExecutorService);
    Duration retryDelay = Duration.ofMillis(100);
    when(environment.recoveryBackOffDelayPolicy()).thenReturn(BackOffDelayPolicy.fixed(retryDelay));
    when(environment.topologyUpdateBackOffDelayPolicy()).thenReturn(BackOffDelayPolicy.fixed(retryDelay));
    when(consumer.isOpen()).thenReturn(true);
    when(locator.metadata("stream")).thenReturn(metadata(null, replicas())).thenReturn(metadata(null, Collections.emptyList())).thenReturn(metadata(null, replicas()));
    ArgumentCaptor<OffsetSpecification> offsetSpecificationArgumentCaptor = ArgumentCaptor.forClass(OffsetSpecification.class);
    when(clientFactory.client(any())).thenReturn(client);
    when(client.subscribe(subscriptionIdCaptor.capture(), anyString(), offsetSpecificationArgumentCaptor.capture(), anyInt(), anyMap())).thenReturn(new Client.Response(Constants.RESPONSE_CODE_OK));
    Runnable closingRunnable = coordinator.subscribe(consumer, "stream", OffsetSpecification.first(), null, NO_OP_SUBSCRIPTION_LISTENER, (offset, message) -> {
    });
    verify(clientFactory, times(1)).client(any());
    verify(client, times(1)).subscribe(anyByte(), anyString(), any(OffsetSpecification.class), anyInt(), anyMap());
    assertThat(offsetSpecificationArgumentCaptor.getAllValues()).element(0).isEqualTo(OffsetSpecification.first());
    long lastReceivedOffset = 10;
    messageListener.handle(subscriptionIdCaptor.getValue(), lastReceivedOffset, 0, new WrapperMessageBuilder().build());
    configurator.accept(this);
    Thread.sleep(retryDelay.toMillis() * 5);
    verify(client, times(2)).subscribe(anyByte(), anyString(), any(OffsetSpecification.class), anyInt(), anyMap());
    assertThat(offsetSpecificationArgumentCaptor.getAllValues()).element(1).isEqualTo(OffsetSpecification.offset(lastReceivedOffset));
    when(client.unsubscribe(subscriptionIdCaptor.getValue())).thenReturn(new Client.Response(Constants.RESPONSE_CODE_OK));
    closingRunnable.run();
    verify(client, times(1)).unsubscribe(subscriptionIdCaptor.getValue());
}
Also used : OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) WrapperMessageBuilder(com.rabbitmq.stream.codec.WrapperMessageBuilder) Duration(java.time.Duration) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 5 with WrapperMessageBuilder

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

the class ConsumersCoordinatorTest method shouldUseStoredOffsetOnRecovery.

@ParameterizedTest
@MethodSource("disruptionArguments")
@SuppressWarnings("unchecked")
void shouldUseStoredOffsetOnRecovery(Consumer<ConsumersCoordinatorTest> configurator) throws Exception {
    scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
    when(environment.scheduledExecutorService()).thenReturn(scheduledExecutorService);
    Duration retryDelay = Duration.ofMillis(100);
    when(environment.recoveryBackOffDelayPolicy()).thenReturn(BackOffDelayPolicy.fixed(retryDelay));
    when(environment.topologyUpdateBackOffDelayPolicy()).thenReturn(BackOffDelayPolicy.fixed(retryDelay));
    when(consumer.isOpen()).thenReturn(true);
    when(locator.metadata("stream")).thenReturn(metadata(null, replicas())).thenReturn(metadata(null, Collections.emptyList())).thenReturn(metadata(null, replicas()));
    when(clientFactory.client(any())).thenReturn(client);
    String consumerName = "consumer-name";
    long lastStoredOffset = 5;
    long lastReceivedOffset = 10;
    when(client.queryOffset(consumerName, "stream")).thenReturn(new QueryOffsetResponse(Constants.RESPONSE_CODE_OK, 0L)).thenReturn(new QueryOffsetResponse(Constants.RESPONSE_CODE_OK, lastStoredOffset));
    ArgumentCaptor<OffsetSpecification> offsetSpecificationArgumentCaptor = ArgumentCaptor.forClass(OffsetSpecification.class);
    ArgumentCaptor<Map<String, String>> subscriptionPropertiesArgumentCaptor = ArgumentCaptor.forClass(Map.class);
    when(client.subscribe(subscriptionIdCaptor.capture(), anyString(), offsetSpecificationArgumentCaptor.capture(), anyInt(), subscriptionPropertiesArgumentCaptor.capture())).thenReturn(new Client.Response(Constants.RESPONSE_CODE_OK));
    Runnable closingRunnable = coordinator.subscribe(consumer, "stream", null, consumerName, NO_OP_SUBSCRIPTION_LISTENER, (offset, message) -> {
    });
    verify(clientFactory, times(1)).client(any());
    verify(client, times(1)).subscribe(anyByte(), anyString(), any(OffsetSpecification.class), anyInt(), anyMap());
    assertThat(offsetSpecificationArgumentCaptor.getAllValues()).element(0).isEqualTo(OffsetSpecification.next());
    assertThat(subscriptionPropertiesArgumentCaptor.getAllValues()).element(0).isEqualTo(Collections.singletonMap("name", "consumer-name"));
    messageListener.handle(subscriptionIdCaptor.getValue(), lastReceivedOffset, 0, new WrapperMessageBuilder().build());
    configurator.accept(this);
    Thread.sleep(retryDelay.toMillis() * 5);
    verify(client, times(2)).subscribe(anyByte(), anyString(), any(OffsetSpecification.class), anyInt(), anyMap());
    assertThat(offsetSpecificationArgumentCaptor.getAllValues()).element(1).isEqualTo(OffsetSpecification.offset(lastStoredOffset + 1)).isNotEqualTo(OffsetSpecification.offset(lastReceivedOffset));
    assertThat(subscriptionPropertiesArgumentCaptor.getAllValues()).element(1).isEqualTo(Collections.singletonMap("name", "consumer-name"));
    when(client.unsubscribe(subscriptionIdCaptor.getValue())).thenReturn(new Client.Response(Constants.RESPONSE_CODE_OK));
    closingRunnable.run();
    verify(client, times(1)).unsubscribe(subscriptionIdCaptor.getValue());
}
Also used : OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) WrapperMessageBuilder(com.rabbitmq.stream.codec.WrapperMessageBuilder) QueryOffsetResponse(com.rabbitmq.stream.impl.Client.QueryOffsetResponse) Duration(java.time.Duration) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

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