use of com.rabbitmq.stream.impl.MonitoringTestUtils.EnvironmentInfo in project rabbitmq-stream-java-client by rabbitmq.
the class StreamEnvironmentTest method producersAndConsumersShouldBeClosedWhenEnvironmentIsClosed.
@ParameterizedTest
@ValueSource(booleans = { false, true })
void producersAndConsumersShouldBeClosedWhenEnvironmentIsClosed(boolean lazyInit) {
Environment environment = environmentBuilder.lazyInitialization(lazyInit).build();
Collection<Producer> producers = IntStream.range(0, 2).mapToObj(i -> environment.producerBuilder().stream(stream).build()).collect(Collectors.toList());
Collection<Consumer> consumers = IntStream.range(0, 2).mapToObj(i -> environment.consumerBuilder().stream(stream).name(UUID.randomUUID().toString()).messageHandler((offset, message) -> {
}).build()).collect(Collectors.toList());
producers.forEach(producer -> assertThat(((StreamProducer) producer).isOpen()).isTrue());
consumers.forEach(consumer -> assertThat(((StreamConsumer) consumer).isOpen()).isTrue());
EnvironmentInfo environmentInfo = MonitoringTestUtils.extract(environment);
assertThat(environmentInfo.getLocator()).isNotNull();
assertThat(environmentInfo.getProducers()).hasSize(1).element(0).extracting(pool -> pool.getClients()).asList().hasSize(1);
assertThat(environmentInfo.getProducers().get(0).getClients().get(0).getProducerCount()).isEqualTo(2);
assertThat(environmentInfo.getProducers().get(0).getClients().get(0).getTrackingConsumerCount()).isEqualTo(2);
assertThat(environmentInfo.getConsumers()).hasSize(1).element(0).extracting(pool -> pool.getClients()).asList().hasSize(1);
assertThat(environmentInfo.getConsumers().get(0).getClients().get(0).getConsumerCount()).isEqualTo(2);
environment.close();
producers.forEach(producer -> assertThat(((StreamProducer) producer).isOpen()).isFalse());
consumers.forEach(consumer -> assertThat(((StreamConsumer) consumer).isOpen()).isFalse());
environmentInfo = MonitoringTestUtils.extract(environment);
assertThat(environmentInfo.getLocator()).isNull();
assertThat(environmentInfo.getProducers()).isEmpty();
assertThat(environmentInfo.getConsumers()).isEmpty();
}
use of com.rabbitmq.stream.impl.MonitoringTestUtils.EnvironmentInfo in project rabbitmq-stream-java-client by rabbitmq.
the class StreamEnvironmentTest method growShrinkResourcesWhenProducersConsumersAreOpenedAndClosed.
@Test
void growShrinkResourcesWhenProducersConsumersAreOpenedAndClosed(TestInfo info) throws Exception {
int messageCount = 100;
int streamCount = 20;
int producersCount = ProducersCoordinator.MAX_PRODUCERS_PER_CLIENT * 3 + 10;
int consumersCount = ConsumersCoordinator.MAX_SUBSCRIPTIONS_PER_CLIENT * 2 + 10;
try (Environment environment = environmentBuilder.build()) {
List<String> streams = IntStream.range(0, streamCount).mapToObj(i -> streamName(info)).map(s -> {
environment.streamCreator().stream(s).create();
return s;
}).collect(Collectors.toCollection(() -> new CopyOnWriteArrayList<>()));
CountDownLatch confirmLatch = new CountDownLatch(messageCount * producersCount);
CountDownLatch consumeLatch = new CountDownLatch(messageCount * producersCount);
List<Producer> producers = IntStream.range(0, producersCount).mapToObj(i -> {
String s = streams.get(i % streams.size());
return environment.producerBuilder().stream(s).build();
}).collect(Collectors.toList());
List<Consumer> consumers = IntStream.range(0, consumersCount).mapToObj(i -> {
String s = streams.get(new Random().nextInt(streams.size()));
return environment.consumerBuilder().stream(s).messageHandler((offset, message) -> consumeLatch.countDown()).build();
}).collect(Collectors.toList());
producers.stream().parallel().forEach(producer -> {
IntStream.range(0, messageCount).forEach(messageIndex -> {
producer.send(producer.messageBuilder().addData("".getBytes()).build(), confirmationStatus -> {
if (confirmationStatus.isConfirmed()) {
confirmLatch.countDown();
}
});
});
});
assertThat(confirmLatch.await(10, SECONDS)).isTrue();
assertThat(consumeLatch.await(10, SECONDS)).isTrue();
EnvironmentInfo environmentInfo = MonitoringTestUtils.extract(environment);
assertThat(environmentInfo.getProducers()).hasSize(1);
int producerManagerCount = environmentInfo.getProducers().get(0).getClients().size();
assertThat(producerManagerCount).isPositive();
assertThat(environmentInfo.getConsumers()).hasSize(1);
int consumerManagerCount = environmentInfo.getConsumers().get(0).getClients().size();
assertThat(consumerManagerCount).isPositive();
java.util.function.Consumer<AutoCloseable> closing = agent -> {
try {
agent.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
};
Collections.reverse(producers);
List<Producer> subProducers = producers.subList(0, ProducersCoordinator.MAX_PRODUCERS_PER_CLIENT);
subProducers.forEach(closing);
Collections.reverse(consumers);
List<Consumer> subConsumers = consumers.subList(0, ConsumersCoordinator.MAX_SUBSCRIPTIONS_PER_CLIENT);
subConsumers.forEach(closing);
producers.removeAll(subProducers);
consumers.removeAll(subConsumers);
environmentInfo = MonitoringTestUtils.extract(environment);
assertThat(environmentInfo.getProducers()).hasSize(1);
assertThat(environmentInfo.getProducers().get(0).getClients()).hasSizeLessThan(producerManagerCount);
assertThat(environmentInfo.getConsumers()).hasSize(1);
assertThat(environmentInfo.getConsumers().get(0).getClients()).hasSizeLessThan(consumerManagerCount);
producers.forEach(closing);
consumers.forEach(closing);
streams.stream().forEach(stream -> environment.deleteStream(stream));
}
}
Aggregations