Search in sources :

Example 6 with TrackingConfiguration

use of com.rabbitmq.stream.impl.StreamConsumerBuilder.TrackingConfiguration 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);
}
Also used : TrackingConfiguration(com.rabbitmq.stream.impl.StreamConsumerBuilder.TrackingConfiguration) IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestUtils.answer(com.rabbitmq.stream.impl.TestUtils.answer) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) Executors(java.util.concurrent.Executors) LongConsumer(java.util.function.LongConsumer) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) CountDownLatch(java.util.concurrent.CountDownLatch) MockitoAnnotations(org.mockito.MockitoAnnotations) Context(com.rabbitmq.stream.MessageHandler.Context) AfterEach(org.junit.jupiter.api.AfterEach) ArgumentCaptor(org.mockito.ArgumentCaptor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Registration(com.rabbitmq.stream.impl.OffsetTrackingCoordinator.Registration) Context(com.rabbitmq.stream.MessageHandler.Context) Duration(java.time.Duration) TrackingConfiguration(com.rabbitmq.stream.impl.StreamConsumerBuilder.TrackingConfiguration) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 7 with TrackingConfiguration

use of com.rabbitmq.stream.impl.StreamConsumerBuilder.TrackingConfiguration 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());
}
Also used : Context(com.rabbitmq.stream.MessageHandler.Context) Duration(java.time.Duration) TrackingConfiguration(com.rabbitmq.stream.impl.StreamConsumerBuilder.TrackingConfiguration) Test(org.junit.jupiter.api.Test)

Example 8 with TrackingConfiguration

use of com.rabbitmq.stream.impl.StreamConsumerBuilder.TrackingConfiguration in project rabbitmq-stream-java-client by rabbitmq.

the class OffsetTrackingCoordinatorTest method autoShouldNotStoreLastProcessedOffsetOnClosingIfBehindStoredOffset.

@Test
void autoShouldNotStoreLastProcessedOffsetOnClosingIfBehindStoredOffset() {
    Duration checkInterval = Duration.ofMillis(100);
    OffsetTrackingCoordinator coordinator = new OffsetTrackingCoordinator(env, checkInterval);
    when(consumer.lastStoredOffset()).thenReturn(15L);
    Registration registration = coordinator.registerTrackingConsumer(consumer, new TrackingConfiguration(true, true, 1, Duration.ofHours(1), Duration.ZERO));
    long lastProcessedOffset = 10;
    registration.postMessageProcessingCallback().accept(context(lastProcessedOffset, () -> {
    }));
    registration.closingCallback().run();
    verify(consumer, never()).store(anyLong());
}
Also used : Registration(com.rabbitmq.stream.impl.OffsetTrackingCoordinator.Registration) Duration(java.time.Duration) TrackingConfiguration(com.rabbitmq.stream.impl.StreamConsumerBuilder.TrackingConfiguration) Test(org.junit.jupiter.api.Test)

Example 9 with TrackingConfiguration

use of com.rabbitmq.stream.impl.StreamConsumerBuilder.TrackingConfiguration in project rabbitmq-stream-java-client by rabbitmq.

the class OffsetTrackingCoordinatorTest method autoShouldStoreLastProcessedOffsetOnClosing.

@Test
void autoShouldStoreLastProcessedOffsetOnClosing() {
    Duration checkInterval = Duration.ofMillis(100);
    OffsetTrackingCoordinator coordinator = new OffsetTrackingCoordinator(env, checkInterval);
    when(consumer.lastStoredOffset()).thenReturn(5L);
    Registration registration = coordinator.registerTrackingConsumer(consumer, new TrackingConfiguration(true, true, 1, Duration.ofHours(1), Duration.ZERO));
    long lastProcessedOffset = 10;
    registration.postMessageProcessingCallback().accept(context(lastProcessedOffset, () -> {
    }));
    registration.closingCallback().run();
    verify(consumer, times(1)).store(lastProcessedOffset);
}
Also used : Registration(com.rabbitmq.stream.impl.OffsetTrackingCoordinator.Registration) Duration(java.time.Duration) TrackingConfiguration(com.rabbitmq.stream.impl.StreamConsumerBuilder.TrackingConfiguration) Test(org.junit.jupiter.api.Test)

Example 10 with TrackingConfiguration

use of com.rabbitmq.stream.impl.StreamConsumerBuilder.TrackingConfiguration in project rabbitmq-stream-java-client by rabbitmq.

the class OffsetTrackingCoordinatorTest method autoShouldNotStoreIfNoMessagesArrive.

@Test
void autoShouldNotStoreIfNoMessagesArrive() throws Exception {
    Duration checkInterval = Duration.ofMillis(10);
    OffsetTrackingCoordinator coordinator = new OffsetTrackingCoordinator(env, checkInterval);
    coordinator.registerTrackingConsumer(consumer, new TrackingConfiguration(true, true, 100, Duration.ofMillis(200), Duration.ZERO));
    Thread.sleep(3 * checkInterval.toMillis());
    verify(consumer, never()).store(anyLong());
}
Also used : Duration(java.time.Duration) TrackingConfiguration(com.rabbitmq.stream.impl.StreamConsumerBuilder.TrackingConfiguration) Test(org.junit.jupiter.api.Test)

Aggregations

TrackingConfiguration (com.rabbitmq.stream.impl.StreamConsumerBuilder.TrackingConfiguration)10 Duration (java.time.Duration)10 Test (org.junit.jupiter.api.Test)10 Context (com.rabbitmq.stream.MessageHandler.Context)6 Registration (com.rabbitmq.stream.impl.OffsetTrackingCoordinator.Registration)6 LongConsumer (java.util.function.LongConsumer)5 TestUtils.answer (com.rabbitmq.stream.impl.TestUtils.answer)4 TestUtils.latchAssert (com.rabbitmq.stream.impl.TestUtils.latchAssert)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Executors (java.util.concurrent.Executors)4 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Consumer (java.util.function.Consumer)4 IntStream (java.util.stream.IntStream)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 AfterEach (org.junit.jupiter.api.AfterEach)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 ArgumentCaptor (org.mockito.ArgumentCaptor)4 Mock (org.mockito.Mock)4 Mockito (org.mockito.Mockito)4