use of com.rabbitmq.stream.impl.MonitoringTestUtils.ConsumersPoolInfo in project rabbitmq-stream-java-client by rabbitmq.
the class ConsumersCoordinatorTest method shouldRemoveClientSubscriptionManagerFromPoolIfEmptyAfterMetadataUpdate.
@Test
void shouldRemoveClientSubscriptionManagerFromPoolIfEmptyAfterMetadataUpdate() 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().subList(0, 1)));
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));
int extraSubscriptionCount = ConsumersCoordinator.MAX_SUBSCRIPTIONS_PER_CLIENT / 5;
int subscriptionCount = ConsumersCoordinator.MAX_SUBSCRIPTIONS_PER_CLIENT + extraSubscriptionCount;
IntStream.range(0, subscriptionCount).forEach(i -> {
coordinator.subscribe(consumer, "stream", OffsetSpecification.first(), null, NO_OP_SUBSCRIPTION_LISTENER, (offset, message) -> {
});
});
// the extra is allocated on another client from the same pool
verify(clientFactory, times(2)).client(any());
verify(client, times(subscriptionCount)).subscribe(anyByte(), anyString(), any(OffsetSpecification.class), anyInt(), anyMap());
List<ConsumersPoolInfo> info = MonitoringTestUtils.extract(coordinator);
assertThat(info).hasSize(1).element(0).extracting(pool -> pool.consumerCount()).isEqualTo(subscriptionCount);
// let's kill the first client connection
metadataListeners.get(0).handle("stream", Constants.RESPONSE_CODE_STREAM_NOT_AVAILABLE);
Thread.sleep(delayPolicy.delay(0).toMillis() * 5);
info = MonitoringTestUtils.extract(coordinator);
assertThat(info).hasSize(1).element(0).extracting(pool -> pool.consumerCount()).isEqualTo(subscriptionCount);
// the MAX consumers must have been re-allocated to the existing client and a new one
// let's add a new subscription to make sure we are still using the same pool
coordinator.subscribe(consumer, "stream", OffsetSpecification.first(), null, NO_OP_SUBSCRIPTION_LISTENER, (offset, message) -> {
});
verify(clientFactory, times(2 + 1)).client(any());
verify(client, times(subscriptionCount + ConsumersCoordinator.MAX_SUBSCRIPTIONS_PER_CLIENT + 1)).subscribe(anyByte(), anyString(), any(OffsetSpecification.class), anyInt(), anyMap());
info = MonitoringTestUtils.extract(coordinator);
assertThat(info).hasSize(1).element(0).extracting(pool -> pool.consumerCount()).isEqualTo(subscriptionCount + 1);
}
Aggregations