Search in sources :

Example 1 with ProducerInfo

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

the class StreamProducerTest method send.

@Test
void send() throws Exception {
    int batchSize = 10;
    // don't want a multiple of batch size
    int messageCount = 10 * batchSize + 1;
    CountDownLatch publishLatch = new CountDownLatch(messageCount);
    Producer producer = environment.producerBuilder().stream(stream).batchSize(batchSize).build();
    AtomicLong count = new AtomicLong(0);
    AtomicLong sequence = new AtomicLong(0);
    Set<Long> idsSent = ConcurrentHashMap.newKeySet(messageCount);
    Set<Long> idsConfirmed = ConcurrentHashMap.newKeySet(messageCount);
    IntStream.range(0, messageCount).forEach(i -> {
        long id = sequence.getAndIncrement();
        idsSent.add(id);
        producer.send(producer.messageBuilder().properties().messageId(id).messageBuilder().addData("".getBytes()).build(), confirmationStatus -> {
            idsConfirmed.add(confirmationStatus.getMessage().getProperties().getMessageIdAsLong());
            count.incrementAndGet();
            publishLatch.countDown();
        });
    });
    boolean completed = publishLatch.await(10, TimeUnit.SECONDS);
    assertThat(idsSent).hasSameSizeAs(idsConfirmed);
    idsSent.forEach(idSent -> assertThat(idsConfirmed).contains(idSent));
    assertThat(completed).isTrue();
    ProducerInfo info = MonitoringTestUtils.extract(producer);
    assertThat(info.getId()).isGreaterThanOrEqualTo(0);
    assertThat(info.getStream()).isEqualTo(stream);
    assertThat(info.getPublishingClient()).contains(" -> localhost:5552");
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) ProducerInfo(com.rabbitmq.stream.impl.MonitoringTestUtils.ProducerInfo) Producer(com.rabbitmq.stream.Producer) AtomicLong(java.util.concurrent.atomic.AtomicLong) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Producer (com.rabbitmq.stream.Producer)1 ProducerInfo (com.rabbitmq.stream.impl.MonitoringTestUtils.ProducerInfo)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1