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);
}
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();
}
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();
}
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());
}
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());
}
Aggregations