Search in sources :

Example 1 with QueryOffsetResponse

use of com.rabbitmq.stream.impl.Client.QueryOffsetResponse 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)

Example 2 with QueryOffsetResponse

use of com.rabbitmq.stream.impl.Client.QueryOffsetResponse in project rabbitmq-stream-java-client by rabbitmq.

the class OffsetTrackingTest method shouldReturnNoOffsetIfNothingStoredForReference.

@Test
void shouldReturnNoOffsetIfNothingStoredForReference() {
    QueryOffsetResponse response = cf.get().queryOffset(UUID.randomUUID().toString(), stream);
    assertThat(response.isOk()).isFalse();
    assertThat(response.getResponseCode()).isEqualTo(Constants.RESPONSE_CODE_NO_OFFSET);
    assertThat(response.getOffset()).isEqualTo(0);
}
Also used : QueryOffsetResponse(com.rabbitmq.stream.impl.Client.QueryOffsetResponse) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

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