Search in sources :

Example 1 with TestUtils.waitAtMost

use of com.rabbitmq.stream.impl.TestUtils.waitAtMost in project rabbitmq-stream-java-client by rabbitmq.

the class StreamConsumerTest method consumerShouldBeClosedWhenStreamGetsDeleted.

@Test
void consumerShouldBeClosedWhenStreamGetsDeleted(TestInfo info) throws Exception {
    String s = streamName(info);
    environment.streamCreator().stream(s).create();
    int messageCount = 10_000;
    CountDownLatch publishLatch = new CountDownLatch(messageCount);
    Producer producer = environment.producerBuilder().stream(s).build();
    IntStream.range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().addData("".getBytes()).build(), confirmationStatus -> publishLatch.countDown()));
    assertThat(publishLatch.await(10, TimeUnit.SECONDS)).isTrue();
    CountDownLatch consumeLatch = new CountDownLatch(messageCount);
    StreamConsumer consumer = (StreamConsumer) environment.consumerBuilder().stream(s).offset(OffsetSpecification.first()).messageHandler((offset, message) -> consumeLatch.countDown()).build();
    assertThat(consumeLatch.await(10, TimeUnit.SECONDS)).isTrue();
    assertThat(consumer.isOpen()).isTrue();
    environment.deleteStream(s);
    TestUtils.waitAtMost(10, () -> !consumer.isOpen());
    assertThat(consumer.isOpen()).isFalse();
}
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) Producer(com.rabbitmq.stream.Producer) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with TestUtils.waitAtMost

use of com.rabbitmq.stream.impl.TestUtils.waitAtMost in project rabbitmq-stream-java-client by rabbitmq.

the class StreamConsumerTest method consumerShouldKeepConsumingAfterDisruption.

@ParameterizedTest
@MethodSource
@TestUtils.DisabledIfRabbitMqCtlNotSet
void consumerShouldKeepConsumingAfterDisruption(java.util.function.Consumer<Object> disruption, TestInfo info) throws Exception {
    String s = streamName(info);
    environment.streamCreator().stream(s).create();
    try {
        int messageCount = 10_000;
        CountDownLatch publishLatch = new CountDownLatch(messageCount);
        Producer producer = environment.producerBuilder().stream(s).build();
        IntStream.range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().addData("".getBytes()).build(), confirmationStatus -> publishLatch.countDown()));
        assertThat(publishLatch.await(10, TimeUnit.SECONDS)).isTrue();
        producer.close();
        AtomicInteger receivedMessageCount = new AtomicInteger(0);
        CountDownLatch consumeLatch = new CountDownLatch(messageCount);
        CountDownLatch consumeLatchSecondWave = new CountDownLatch(messageCount * 2);
        StreamConsumer consumer = (StreamConsumer) environment.consumerBuilder().stream(s).offset(OffsetSpecification.first()).messageHandler((offset, message) -> {
            receivedMessageCount.incrementAndGet();
            consumeLatch.countDown();
            consumeLatchSecondWave.countDown();
        }).build();
        assertThat(consumeLatch.await(10, TimeUnit.SECONDS)).isTrue();
        assertThat(consumer.isOpen()).isTrue();
        disruption.accept(s);
        Client client = cf.get();
        TestUtils.waitAtMost(10, () -> {
            Client.StreamMetadata metadata = client.metadata(s).get(s);
            return metadata.getLeader() != null || !metadata.getReplicas().isEmpty();
        });
        CountDownLatch publishLatchSecondWave = new CountDownLatch(messageCount);
        Producer producerSecondWave = environment.producerBuilder().stream(s).build();
        IntStream.range(0, messageCount).forEach(i -> producerSecondWave.send(producerSecondWave.messageBuilder().addData("".getBytes()).build(), confirmationStatus -> publishLatchSecondWave.countDown()));
        assertThat(publishLatchSecondWave.await(10, TimeUnit.SECONDS)).isTrue();
        producerSecondWave.close();
        assertThat(consumeLatchSecondWave.await(10, TimeUnit.SECONDS)).isTrue();
        assertThat(receivedMessageCount.get()).isBetween(messageCount * 2, // there can be a duplicate
        messageCount * 2 + 1);
        assertThat(consumer.isOpen()).isTrue();
        consumer.close();
    } finally {
        environment.deleteStream(s);
    }
}
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) Producer(com.rabbitmq.stream.Producer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) DisabledIfRabbitMqCtlNotSet(com.rabbitmq.stream.impl.TestUtils.DisabledIfRabbitMqCtlNotSet) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

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