use of com.rabbitmq.stream.impl.TestUtils.waitAtMost in project rabbitmq-stream-java-client by rabbitmq.
the class StreamConsumerTest method consumerShouldBeClosedWhenStreamGetsDeleted.
@Test
void consumerShouldBeClosedWhenStreamGetsDeleted(TestInfo info) throws Exception {
String s = streamName(info);
environment.streamCreator().stream(s).create();
int messageCount = 10_000;
CountDownLatch publishLatch = new CountDownLatch(messageCount);
Producer producer = environment.producerBuilder().stream(s).build();
IntStream.range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().addData("".getBytes()).build(), confirmationStatus -> publishLatch.countDown()));
assertThat(publishLatch.await(10, TimeUnit.SECONDS)).isTrue();
CountDownLatch consumeLatch = new CountDownLatch(messageCount);
StreamConsumer consumer = (StreamConsumer) environment.consumerBuilder().stream(s).offset(OffsetSpecification.first()).messageHandler((offset, message) -> consumeLatch.countDown()).build();
assertThat(consumeLatch.await(10, TimeUnit.SECONDS)).isTrue();
assertThat(consumer.isOpen()).isTrue();
environment.deleteStream(s);
TestUtils.waitAtMost(10, () -> !consumer.isOpen());
assertThat(consumer.isOpen()).isFalse();
}
use of com.rabbitmq.stream.impl.TestUtils.waitAtMost in project rabbitmq-stream-java-client by rabbitmq.
the class StreamConsumerTest method consumerShouldKeepConsumingAfterDisruption.
@ParameterizedTest
@MethodSource
@TestUtils.DisabledIfRabbitMqCtlNotSet
void consumerShouldKeepConsumingAfterDisruption(java.util.function.Consumer<Object> disruption, TestInfo info) throws Exception {
String s = streamName(info);
environment.streamCreator().stream(s).create();
try {
int messageCount = 10_000;
CountDownLatch publishLatch = new CountDownLatch(messageCount);
Producer producer = environment.producerBuilder().stream(s).build();
IntStream.range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().addData("".getBytes()).build(), confirmationStatus -> publishLatch.countDown()));
assertThat(publishLatch.await(10, TimeUnit.SECONDS)).isTrue();
producer.close();
AtomicInteger receivedMessageCount = new AtomicInteger(0);
CountDownLatch consumeLatch = new CountDownLatch(messageCount);
CountDownLatch consumeLatchSecondWave = new CountDownLatch(messageCount * 2);
StreamConsumer consumer = (StreamConsumer) environment.consumerBuilder().stream(s).offset(OffsetSpecification.first()).messageHandler((offset, message) -> {
receivedMessageCount.incrementAndGet();
consumeLatch.countDown();
consumeLatchSecondWave.countDown();
}).build();
assertThat(consumeLatch.await(10, TimeUnit.SECONDS)).isTrue();
assertThat(consumer.isOpen()).isTrue();
disruption.accept(s);
Client client = cf.get();
TestUtils.waitAtMost(10, () -> {
Client.StreamMetadata metadata = client.metadata(s).get(s);
return metadata.getLeader() != null || !metadata.getReplicas().isEmpty();
});
CountDownLatch publishLatchSecondWave = new CountDownLatch(messageCount);
Producer producerSecondWave = environment.producerBuilder().stream(s).build();
IntStream.range(0, messageCount).forEach(i -> producerSecondWave.send(producerSecondWave.messageBuilder().addData("".getBytes()).build(), confirmationStatus -> publishLatchSecondWave.countDown()));
assertThat(publishLatchSecondWave.await(10, TimeUnit.SECONDS)).isTrue();
producerSecondWave.close();
assertThat(consumeLatchSecondWave.await(10, TimeUnit.SECONDS)).isTrue();
assertThat(receivedMessageCount.get()).isBetween(messageCount * 2, // there can be a duplicate
messageCount * 2 + 1);
assertThat(consumer.isOpen()).isTrue();
consumer.close();
} finally {
environment.deleteStream(s);
}
}
Aggregations