Search in sources :

Example 51 with Producer

use of com.rabbitmq.stream.Producer in project rabbitmq-stream-java-client by rabbitmq.

the class SuperStreamProducerTest method allMessagesSentToSuperStreamWithHashRoutingShouldBeThenConsumed.

@Test
void allMessagesSentToSuperStreamWithHashRoutingShouldBeThenConsumed() throws Exception {
    int messageCount = 10_000;
    declareSuperStreamTopology(connection, superStream, partitions);
    Producer producer = environment.producerBuilder().stream(superStream).routing(message -> message.getProperties().getMessageIdAsString()).producerBuilder().build();
    CountDownLatch publishLatch = new CountDownLatch(messageCount);
    IntStream.range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().properties().messageId(UUID.randomUUID().toString()).messageBuilder().build(), confirmationStatus -> publishLatch.countDown()));
    assertThat(latchAssert(publishLatch)).completes(5);
    Map<String, AtomicLong> counts = new ConcurrentHashMap<>();
    AtomicLong totalCount = new AtomicLong(0);
    IntStream.range(0, partitions).forEach(i -> {
        String stream = superStream + "-" + i;
        AtomicLong streamCount = new AtomicLong(0);
        counts.put(stream, streamCount);
        environment.consumerBuilder().stream(stream).offset(OffsetSpecification.first()).messageHandler((context, message) -> {
            streamCount.incrementAndGet();
            totalCount.incrementAndGet();
        }).build();
    });
    waitAtMost(10, () -> totalCount.get() == messageCount);
    assertThat(counts.values().stream().map(AtomicLong::get)).hasSize(partitions).doesNotContain(0L);
    assertThat(counts.values().stream().map(AtomicLong::get).reduce(0L, Long::sum)).isEqualTo(messageCount);
}
Also used : IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Connection(com.rabbitmq.client.Connection) TestUtils.deleteSuperStreamTopology(com.rabbitmq.stream.impl.TestUtils.deleteSuperStreamTopology) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) TestUtils.declareSuperStreamTopology(com.rabbitmq.stream.impl.TestUtils.declareSuperStreamTopology) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) TestUtils.waitAtMost(com.rabbitmq.stream.impl.TestUtils.waitAtMost) EventLoopGroup(io.netty.channel.EventLoopGroup) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Environment(com.rabbitmq.stream.Environment) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UUID(java.util.UUID) Producer(com.rabbitmq.stream.Producer) EnvironmentBuilder(com.rabbitmq.stream.EnvironmentBuilder) TestInfo(org.junit.jupiter.api.TestInfo) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) AfterEach(org.junit.jupiter.api.AfterEach) Constants(com.rabbitmq.stream.Constants) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) TestUtils.localhost(com.rabbitmq.stream.impl.TestUtils.localhost) AtomicLong(java.util.concurrent.atomic.AtomicLong) Producer(com.rabbitmq.stream.Producer) AtomicLong(java.util.concurrent.atomic.AtomicLong) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.jupiter.api.Test)

Aggregations

Producer (com.rabbitmq.stream.Producer)51 Environment (com.rabbitmq.stream.Environment)44 CountDownLatch (java.util.concurrent.CountDownLatch)36 Test (org.junit.jupiter.api.Test)36 OffsetSpecification (com.rabbitmq.stream.OffsetSpecification)32 EnvironmentBuilder (com.rabbitmq.stream.EnvironmentBuilder)30 TestUtils.latchAssert (com.rabbitmq.stream.impl.TestUtils.latchAssert)30 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)30 BeforeEach (org.junit.jupiter.api.BeforeEach)30 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)30 TestUtils.localhost (com.rabbitmq.stream.impl.TestUtils.localhost)29 EventLoopGroup (io.netty.channel.EventLoopGroup)29 IntStream (java.util.stream.IntStream)28 UUID (java.util.UUID)26 AfterEach (org.junit.jupiter.api.AfterEach)26 TestInfo (org.junit.jupiter.api.TestInfo)26 ConfirmationHandler (com.rabbitmq.stream.ConfirmationHandler)25 AtomicLong (java.util.concurrent.atomic.AtomicLong)25 TestUtils.waitAtMost (com.rabbitmq.stream.impl.TestUtils.waitAtMost)24 Duration (java.time.Duration)24