Search in sources :

Example 6 with MetricsCollector

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

the class MetricsCollectionTest method filteredSmallerOffsetsInChunksShouldNotBeCounted.

@Test
void filteredSmallerOffsetsInChunksShouldNotBeCounted() throws Exception {
    int messageCount = 50000;
    AtomicLong messageIdSequence = new AtomicLong(0);
    TestUtils.publishAndWaitForConfirms(cf, builder -> builder.properties().messageId(messageIdSequence.incrementAndGet()).messageBuilder().build(), messageCount, stream);
    for (int i = 0; i < 10; i++) {
        Map<Byte, CountDownLatch> latches = new ConcurrentHashMap<>();
        latches.put(b(1), new CountDownLatch(1));
        latches.put(b(2), new CountDownLatch(1));
        Map<Byte, AtomicLong> counts = new ConcurrentHashMap<>();
        counts.put(b(1), new AtomicLong());
        counts.put(b(2), new AtomicLong());
        Map<Byte, AtomicLong> expectedCounts = new ConcurrentHashMap<>();
        expectedCounts.put(b(1), new AtomicLong(messageCount - 50));
        expectedCounts.put(b(2), new AtomicLong(messageCount - 100));
        CountMetricsCollector metricsCollector = new CountMetricsCollector();
        Client client = new Client(new ClientParameters().messageListener((subscriptionId, offset, chunkTimestamp, message) -> {
            counts.get(subscriptionId).incrementAndGet();
            if (message.getProperties().getMessageIdAsLong() == messageCount) {
                latches.get(subscriptionId).countDown();
            }
        }).chunkListener((client1, subscriptionId, offset, msgCount, dataSize) -> client1.credit(subscriptionId, 1)).metricsCollector(metricsCollector).eventLoopGroup(eventLoopGroup));
        client.subscribe(b(1), stream, OffsetSpecification.offset(50), 10);
        client.subscribe(b(2), stream, OffsetSpecification.offset(100), 10);
        assertThat(latches.get(b(1)).await(10, SECONDS)).isTrue();
        assertThat(latches.get(b(2)).await(10, SECONDS)).isTrue();
        waitAtMost(() -> counts.get(b(1)).get() == expectedCounts.get(b(1)).get());
        waitAtMost(() -> counts.get(b(2)).get() == expectedCounts.get(b(2)).get());
        waitAtMost(() -> metricsCollector.consume.get() == expectedCounts.values().stream().mapToLong(v -> v.get()).reduce(0, (a, b) -> a + b));
        client.close();
    }
}
Also used : IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) TestUtils.waitAtMost(com.rabbitmq.stream.impl.TestUtils.waitAtMost) EventLoopGroup(io.netty.channel.EventLoopGroup) MetricsCollector(com.rabbitmq.stream.metrics.MetricsCollector) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Compression(com.rabbitmq.stream.compression.Compression) TestUtils.b(com.rabbitmq.stream.impl.TestUtils.b) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) AtomicLong(java.util.concurrent.atomic.AtomicLong) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.jupiter.api.Test)

Aggregations

OffsetSpecification (com.rabbitmq.stream.OffsetSpecification)6 Compression (com.rabbitmq.stream.compression.Compression)6 MetricsCollector (com.rabbitmq.stream.metrics.MetricsCollector)6 Collections (java.util.Collections)6 Map (java.util.Map)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 IntStream (java.util.stream.IntStream)6 ClientParameters (com.rabbitmq.stream.impl.Client.ClientParameters)5 TestUtils.b (com.rabbitmq.stream.impl.TestUtils.b)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 TimeUnit (java.util.concurrent.TimeUnit)5 AtomicLong (java.util.concurrent.atomic.AtomicLong)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 Test (org.junit.jupiter.api.Test)5 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)5 TestUtils.waitAtMost (com.rabbitmq.stream.impl.TestUtils.waitAtMost)4 EventLoopGroup (io.netty.channel.EventLoopGroup)4 SECONDS (java.util.concurrent.TimeUnit.SECONDS)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 RateLimiter (com.google.common.util.concurrent.RateLimiter)1