Search in sources :

Example 1 with TrackingConfiguration

use of com.rabbitmq.stream.impl.StreamConsumerBuilder.TrackingConfiguration 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());
}
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 2 with TrackingConfiguration

use of com.rabbitmq.stream.impl.StreamConsumerBuilder.TrackingConfiguration 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);
}
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 3 with TrackingConfiguration

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

the class OffsetTrackingCoordinatorTest method manualShouldNotStoreIfAlreadyUpToDate.

@Test
void manualShouldNotStoreIfAlreadyUpToDate() throws Exception {
    Duration checkInterval = Duration.ofMillis(100);
    OffsetTrackingCoordinator coordinator = new OffsetTrackingCoordinator(env, checkInterval);
    long lastStoredOffset = 50;
    when(consumer.lastStoredOffset()).thenReturn(lastStoredOffset);
    LongConsumer storeCallback = coordinator.registerTrackingConsumer(consumer, new TrackingConfiguration(true, false, -1, Duration.ZERO, checkInterval.multipliedBy(2))).trackingCallback();
    storeCallback.accept(lastStoredOffset);
    Thread.sleep(3 * checkInterval.toMillis());
    verify(consumer, never()).store(anyLong());
}
Also used : LongConsumer(java.util.function.LongConsumer) Duration(java.time.Duration) TrackingConfiguration(com.rabbitmq.stream.impl.StreamConsumerBuilder.TrackingConfiguration) Test(org.junit.jupiter.api.Test)

Example 4 with TrackingConfiguration

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

the class OffsetTrackingCoordinatorTest method manualShouldStoreIfRequestedStoredOffsetIsBehind.

@Test
void manualShouldStoreIfRequestedStoredOffsetIsBehind() {
    Duration checkInterval = Duration.ofMillis(100);
    OffsetTrackingCoordinator coordinator = new OffsetTrackingCoordinator(env, checkInterval);
    long lastRequestedOffset = 50;
    long lastStoredOffset = 40;
    when(consumer.lastStoredOffset()).thenReturn(lastStoredOffset);
    ArgumentCaptor<Long> lastStoredOffsetCaptor = ArgumentCaptor.forClass(Long.class);
    CountDownLatch storeLatch = new CountDownLatch(1);
    doAnswer(answer(inv -> storeLatch.countDown())).when(consumer).store(lastStoredOffsetCaptor.capture());
    LongConsumer storeCallback = coordinator.registerTrackingConsumer(consumer, new TrackingConfiguration(true, false, -1, Duration.ZERO, checkInterval.multipliedBy(2))).trackingCallback();
    storeCallback.accept(lastRequestedOffset);
    assertThat(latchAssert(storeLatch)).completes(5);
    verify(consumer, times(1)).store(anyLong());
}
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) LongConsumer(java.util.function.LongConsumer) Duration(java.time.Duration) TrackingConfiguration(com.rabbitmq.stream.impl.StreamConsumerBuilder.TrackingConfiguration) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 5 with TrackingConfiguration

use of com.rabbitmq.stream.impl.StreamConsumerBuilder.TrackingConfiguration 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);
}
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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) TrackingConfiguration(com.rabbitmq.stream.impl.StreamConsumerBuilder.TrackingConfiguration) CountDownLatch(java.util.concurrent.CountDownLatch) 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