Search in sources :

Example 1 with ConsumerInfo

use of com.rabbitmq.stream.impl.MonitoringTestUtils.ConsumerInfo in project rabbitmq-stream-java-client by rabbitmq.

the class StreamConsumerTest method manualTrackingConsumerShouldRestartWhereItLeftOff.

@Test
void manualTrackingConsumerShouldRestartWhereItLeftOff() throws Exception {
    Producer producer = environment.producerBuilder().stream(stream).build();
    int messageCountFirstWave = 10_000;
    int messageCountSecondWave = 5_000;
    int messageCount = messageCountFirstWave + messageCountSecondWave;
    CountDownLatch latchConfirmFirstWave = new CountDownLatch(messageCountFirstWave);
    CountDownLatch latchConfirmSecondWave = new CountDownLatch(messageCount);
    ConfirmationHandler confirmationHandler = confirmationStatus -> {
        latchConfirmFirstWave.countDown();
        latchConfirmSecondWave.countDown();
    };
    AtomicLong messageIdSequence = new AtomicLong();
    java.util.function.Consumer<Integer> messageSending = messageCountToSend -> {
        IntStream.range(0, messageCountToSend).forEach(i -> producer.send(producer.messageBuilder().addData("".getBytes()).properties().messageId(messageIdSequence.getAndIncrement()).messageBuilder().build(), confirmationHandler));
    };
    messageSending.accept(messageCountFirstWave);
    assertThat(latchAssert(latchConfirmFirstWave)).completes();
    int storeEvery = 100;
    AtomicInteger consumedMessageCount = new AtomicInteger();
    AtomicReference<Consumer> consumerReference = new AtomicReference<>();
    AtomicLong lastStoredOffset = new AtomicLong(0);
    AtomicLong lastProcessedMessage = new AtomicLong(0);
    AtomicInteger storeCount = new AtomicInteger(0);
    Consumer consumer = environment.consumerBuilder().stream(stream).offset(OffsetSpecification.first()).name("application-1").manualTrackingStrategy().checkInterval(Duration.ZERO).builder().messageHandler((context, message) -> {
        consumedMessageCount.incrementAndGet();
        lastProcessedMessage.set(message.getProperties().getMessageIdAsLong());
        if (consumedMessageCount.get() % storeEvery == 0) {
            context.storeOffset();
            lastStoredOffset.set(context.offset());
            storeCount.incrementAndGet();
        }
    }).build();
    ConsumerInfo consumerInfo = MonitoringTestUtils.extract(consumer);
    assertThat(consumerInfo.getId()).isGreaterThanOrEqualTo(0);
    assertThat(consumerInfo.getStream()).isEqualTo(stream);
    assertThat(consumerInfo.getSubscriptionClient()).contains(" -> localhost:5552");
    assertThat(consumerInfo.getTrackingClient()).contains(" -> localhost:5552");
    consumerReference.set(consumer);
    waitAtMost(10, () -> consumedMessageCount.get() == messageCountFirstWave);
    assertThat(lastStoredOffset.get()).isPositive();
    consumer.close();
    messageSending.accept(messageCountSecondWave);
    assertThat(latchAssert(latchConfirmSecondWave)).completes();
    AtomicLong firstOffset = new AtomicLong(0);
    consumer = environment.consumerBuilder().stream(stream).name("application-1").manualTrackingStrategy().checkInterval(Duration.ZERO).builder().messageHandler((context, message) -> {
        firstOffset.compareAndSet(0, context.offset());
        if (message.getProperties().getMessageIdAsLong() > lastProcessedMessage.get()) {
            consumedMessageCount.incrementAndGet();
        }
    }).build();
    waitAtMost(3, () -> consumedMessageCount.get() == messageCount, () -> "Expected " + consumedMessageCount.get() + " to reach " + messageCount);
    // there will be the tracking records after the first wave of messages,
    // messages offset won't be contiguous, so it's not an exact match
    assertThat(firstOffset.get()).isGreaterThanOrEqualTo(lastStoredOffset.get());
    consumer.close();
}
Also used : IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) StreamDoesNotExistException(com.rabbitmq.stream.StreamDoesNotExistException) IntConsumer(java.util.function.IntConsumer) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UnaryOperator(java.util.function.UnaryOperator) AtomicReference(java.util.concurrent.atomic.AtomicReference) TestUtils.b(com.rabbitmq.stream.impl.TestUtils.b) TestUtils.streamName(com.rabbitmq.stream.impl.TestUtils.streamName) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) DisabledIfRabbitMqCtlNotSet(com.rabbitmq.stream.impl.TestUtils.DisabledIfRabbitMqCtlNotSet) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConsumerBuilder(com.rabbitmq.stream.ConsumerBuilder) Duration(java.time.Duration) MethodSource(org.junit.jupiter.params.provider.MethodSource) Host(com.rabbitmq.stream.Host) TestUtils.waitAtMost(com.rabbitmq.stream.impl.TestUtils.waitAtMost) EventLoopGroup(io.netty.channel.EventLoopGroup) Environment(com.rabbitmq.stream.Environment) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) UUID(java.util.UUID) Consumer(com.rabbitmq.stream.Consumer) Producer(com.rabbitmq.stream.Producer) String.format(java.lang.String.format) EnvironmentBuilder(com.rabbitmq.stream.EnvironmentBuilder) ConsumerInfo(com.rabbitmq.stream.impl.MonitoringTestUtils.ConsumerInfo) TestInfo(org.junit.jupiter.api.TestInfo) BackOffDelayPolicy(com.rabbitmq.stream.BackOffDelayPolicy) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) Collections(java.util.Collections) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) TestUtils.localhost(com.rabbitmq.stream.impl.TestUtils.localhost) ConsumerInfo(com.rabbitmq.stream.impl.MonitoringTestUtils.ConsumerInfo) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicLong(java.util.concurrent.atomic.AtomicLong) Producer(com.rabbitmq.stream.Producer) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) IntConsumer(java.util.function.IntConsumer) Consumer(com.rabbitmq.stream.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

BackOffDelayPolicy (com.rabbitmq.stream.BackOffDelayPolicy)1 ConfirmationHandler (com.rabbitmq.stream.ConfirmationHandler)1 Consumer (com.rabbitmq.stream.Consumer)1 ConsumerBuilder (com.rabbitmq.stream.ConsumerBuilder)1 Environment (com.rabbitmq.stream.Environment)1 EnvironmentBuilder (com.rabbitmq.stream.EnvironmentBuilder)1 Host (com.rabbitmq.stream.Host)1 OffsetSpecification (com.rabbitmq.stream.OffsetSpecification)1 Producer (com.rabbitmq.stream.Producer)1 StreamDoesNotExistException (com.rabbitmq.stream.StreamDoesNotExistException)1 ConsumerInfo (com.rabbitmq.stream.impl.MonitoringTestUtils.ConsumerInfo)1 DisabledIfRabbitMqCtlNotSet (com.rabbitmq.stream.impl.TestUtils.DisabledIfRabbitMqCtlNotSet)1 TestUtils.b (com.rabbitmq.stream.impl.TestUtils.b)1 TestUtils.latchAssert (com.rabbitmq.stream.impl.TestUtils.latchAssert)1 TestUtils.localhost (com.rabbitmq.stream.impl.TestUtils.localhost)1 TestUtils.streamName (com.rabbitmq.stream.impl.TestUtils.streamName)1 TestUtils.waitAtMost (com.rabbitmq.stream.impl.TestUtils.waitAtMost)1 EventLoopGroup (io.netty.channel.EventLoopGroup)1 String.format (java.lang.String.format)1 Duration (java.time.Duration)1