use of com.rabbitmq.stream.impl.StreamProducer.Status in project rabbitmq-stream-java-client by rabbitmq.
the class StreamProducerTest method shouldRecoverAfterConnectionIsKilled.
@ParameterizedTest
@ValueSource(ints = { 1, 10 })
@TestUtils.DisabledIfRabbitMqCtlNotSet
void shouldRecoverAfterConnectionIsKilled(int subEntrySize) throws Exception {
Producer producer = environment.producerBuilder().subEntrySize(subEntrySize).stream(stream).build();
AtomicInteger published = new AtomicInteger(0);
AtomicInteger confirmed = new AtomicInteger(0);
AtomicInteger errored = new AtomicInteger(0);
AtomicBoolean canPublish = new AtomicBoolean(true);
Thread publishThread = new Thread(() -> {
ConfirmationHandler confirmationHandler = confirmationStatus -> {
if (confirmationStatus.isConfirmed()) {
confirmed.incrementAndGet();
} else {
errored.incrementAndGet();
}
};
while (true) {
try {
if (canPublish.get()) {
producer.send(producer.messageBuilder().addData("".getBytes(StandardCharsets.UTF_8)).build(), confirmationHandler);
published.incrementAndGet();
} else {
Thread.sleep(500);
}
} catch (InterruptedException | StreamException e) {
// OK
}
}
});
publishThread.start();
Thread.sleep(1000L);
Host.killConnection("rabbitmq-stream-producer-0");
waitAtMost(10, () -> ((StreamProducer) producer).status() == Status.NOT_AVAILABLE);
canPublish.set(false);
assertThat(confirmed.get()).isPositive();
waitAtMost(5, () -> confirmed.get() + errored.get() == published.get(), () -> String.format("confirmed %d / errored %d / published %d, %d + %d = %d != %d, difference %d", confirmed.get(), errored.get(), published.get(), confirmed.get(), errored.get(), (confirmed.get() + errored.get()), published.get(), (published.get() - (confirmed.get() + errored.get()))));
assertThat(confirmed.get() + errored.get()).isEqualTo(published.get());
waitAtMost(10, () -> ((StreamProducer) producer).status() == StreamProducer.Status.RUNNING);
int confirmedAfterUnavailability = confirmed.get();
int errorAfterUnavailability = errored.get();
canPublish.set(true);
waitAtMost(10, () -> confirmed.get() > confirmedAfterUnavailability * 2);
assertThat(errored.get()).isEqualTo(errorAfterUnavailability);
canPublish.set(false);
publishThread.interrupt();
waitAtMost(10, () -> confirmed.get() + errored.get() == published.get());
CountDownLatch consumeLatch = new CountDownLatch(confirmed.get());
environment.consumerBuilder().stream(stream).offset(OffsetSpecification.first()).messageHandler((offset, message) -> {
consumeLatch.countDown();
}).build();
assertThat(consumeLatch.await(10, TimeUnit.SECONDS)).isTrue();
}
Aggregations