use of com.rabbitmq.stream.MessageHandler.Context in project rabbitmq-stream-java-client by rabbitmq.
the class OffsetTrackingCoordinatorTest method autoShouldNotStoreIfOffsetAlreadyStored.
@Test
void autoShouldNotStoreIfOffsetAlreadyStored() throws Exception {
Duration checkInterval = Duration.ofMillis(100);
OffsetTrackingCoordinator coordinator = new OffsetTrackingCoordinator(env, checkInterval);
long storedOffset = 10;
when(consumer.lastStoredOffset()).thenReturn(storedOffset);
Duration autoFlushInterval = Duration.ofMillis(checkInterval.toMillis() * 2);
Consumer<Context> postProcessedMessageCallback = coordinator.registerTrackingConsumer(consumer, new TrackingConfiguration(true, true, 1, autoFlushInterval, Duration.ZERO)).postMessageProcessingCallback();
postProcessedMessageCallback.accept(context(10, () -> {
}));
Thread.sleep(autoFlushInterval.multipliedBy(4).toMillis());
verify(consumer, never()).store(anyLong());
}
use of com.rabbitmq.stream.MessageHandler.Context in project rabbitmq-stream-java-client by rabbitmq.
the class OffsetTrackingCoordinatorTest method autoShouldStoreAfterSomeInactivity.
@Test
void autoShouldStoreAfterSomeInactivity() {
Duration checkInterval = Duration.ofMillis(100);
OffsetTrackingCoordinator coordinator = new OffsetTrackingCoordinator(env, checkInterval);
CountDownLatch flushLatch = new CountDownLatch(1);
doAnswer(answer(inv -> flushLatch.countDown())).when(consumer).store(anyLong());
Consumer<Context> postProcessedMessageCallback = coordinator.registerTrackingConsumer(consumer, new TrackingConfiguration(true, true, 1, Duration.ofMillis(200), Duration.ZERO)).postMessageProcessingCallback();
postProcessedMessageCallback.accept(context(1, () -> {
}));
assertThat(latchAssert(flushLatch)).completes(5);
}
use of com.rabbitmq.stream.MessageHandler.Context in project rabbitmq-stream-java-client by rabbitmq.
the class OffsetTrackingCoordinatorTest method autoShouldStoreFixedMessageCountAndAutoTrackingAfterInactivity.
@Test
void autoShouldStoreFixedMessageCountAndAutoTrackingAfterInactivity() {
Duration checkInterval = Duration.ofMillis(100);
coordinator = new OffsetTrackingCoordinator(env, checkInterval);
CountDownLatch flushLatch = new CountDownLatch(1);
doAnswer(answer(inv -> flushLatch.countDown())).when(consumer).store(anyLong());
int messageInterval = 100;
int messageCount = 5 * messageInterval + messageInterval / 5;
Consumer<Context> postProcessedMessageCallback = coordinator.registerTrackingConsumer(consumer, new TrackingConfiguration(true, true, messageInterval, Duration.ofMillis(200), Duration.ZERO)).postMessageProcessingCallback();
AtomicInteger storedCountAfterProcessing = new AtomicInteger(0);
IntStream.range(0, messageCount).forEach(i -> {
postProcessedMessageCallback.accept(context(i, () -> storedCountAfterProcessing.incrementAndGet()));
});
assertThat(latchAssert(flushLatch)).completes(5);
assertThat(storedCountAfterProcessing.get()).isEqualTo(messageCount / messageInterval);
}
use of com.rabbitmq.stream.MessageHandler.Context in project rabbitmq-stream-java-client by rabbitmq.
the class OffsetTrackingCoordinatorTest method autoShouldStoreLastProcessedAfterInactivity.
@Test
void autoShouldStoreLastProcessedAfterInactivity() {
Duration checkInterval = Duration.ofMillis(100);
OffsetTrackingCoordinator coordinator = new OffsetTrackingCoordinator(env, checkInterval);
int storeEvery = 10;
int extraMessages = 3;
long expectedLastStoredOffset = storeEvery + extraMessages - 1;
when(consumer.lastStoredOffset()).thenReturn((long) (storeEvery - 1));
ArgumentCaptor<Long> lastStoredOffsetCaptor = ArgumentCaptor.forClass(Long.class);
CountDownLatch flushLatch = new CountDownLatch(1);
doAnswer(answer(inv -> flushLatch.countDown())).when(consumer).store(lastStoredOffsetCaptor.capture());
Duration autoFlushInterval = Duration.ofMillis(checkInterval.toMillis() * 2);
Consumer<Context> postProcessedMessageCallback = coordinator.registerTrackingConsumer(consumer, new TrackingConfiguration(true, true, storeEvery, autoFlushInterval, Duration.ZERO)).postMessageProcessingCallback();
IntStream.range(0, storeEvery + extraMessages).forEach(offset -> {
postProcessedMessageCallback.accept(context(offset, () -> {
}));
});
assertThat(latchAssert(flushLatch)).completes(5);
verify(consumer, times(1)).store(anyLong());
assertThat(lastStoredOffsetCaptor.getValue()).isEqualTo(expectedLastStoredOffset);
}
use of com.rabbitmq.stream.MessageHandler.Context in project rabbitmq-stream-java-client by rabbitmq.
the class OffsetTrackingCoordinatorTest method autoShouldNotFlushAfterInactivityIfLastStoreIsOnModulo.
@Test
void autoShouldNotFlushAfterInactivityIfLastStoreIsOnModulo() throws Exception {
Duration checkInterval = Duration.ofMillis(100);
OffsetTrackingCoordinator coordinator = new OffsetTrackingCoordinator(env, checkInterval);
int storeEvery = 10;
when(consumer.lastStoredOffset()).thenReturn((long) storeEvery - 1);
Duration autoFlushInterval = Duration.ofMillis(checkInterval.toMillis() * 2);
Consumer<Context> postProcessedMessageCallback = coordinator.registerTrackingConsumer(consumer, new TrackingConfiguration(true, true, storeEvery, autoFlushInterval, Duration.ZERO)).postMessageProcessingCallback();
IntStream.range(0, storeEvery).forEach(offset -> {
postProcessedMessageCallback.accept(context(offset, () -> {
}));
});
Thread.sleep(autoFlushInterval.multipliedBy(4).toMillis());
verify(consumer, never()).store(anyLong());
}
Aggregations