Search in sources :

Example 1 with Context

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());
}
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 Context

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);
}
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 Context

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);
}
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)

Example 4 with Context

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);
}
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 5 with Context

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());
}
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)

Aggregations

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