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();
}
}
Aggregations