use of com.rabbitmq.stream.impl.Client.ClientParameters in project rabbitmq-stream-java-client by rabbitmq.
the class OffsetTrackingTest method storeOffsetAndThenAttachByTimestampShouldWork.
@Test
void storeOffsetAndThenAttachByTimestampShouldWork() throws Exception {
// this test performs a timestamp-based index search within a segment with
// a lot of non-user entries (chunks that contain tracking info, not messages)
int messageCount = 50_000;
AtomicReference<CountDownLatch> confirmLatch = new AtomicReference<>(new CountDownLatch(messageCount));
AtomicInteger consumed = new AtomicInteger();
Client client = cf.get(new ClientParameters().publishConfirmListener((publisherId, publishingId) -> confirmLatch.get().countDown()).chunkListener((client1, subscriptionId, offset, messageCount1, dataSize) -> client1.credit(subscriptionId, 1)).messageListener((subscriptionId, offset, chunkTimestamp, message) -> consumed.incrementAndGet()));
assertThat(client.declarePublisher((byte) 0, null, stream).isOk()).isTrue();
Runnable publishAction = () -> {
IntStream.range(0, messageCount).forEach(i -> client.publish((byte) 0, Collections.singletonList(client.codec().messageBuilder().addData("hello world".getBytes(StandardCharsets.UTF_8)).build())));
};
publishAction.run();
assertThat(latchAssert(confirmLatch)).completes();
IntStream.range(0, messageCount).forEach(i -> client.storeOffset("some reference", stream, i));
waitAtMost(() -> client.queryOffset("some reference", stream).getOffset() == messageCount - 1);
confirmLatch.set(new CountDownLatch(messageCount));
long betweenTwoWaves = System.currentTimeMillis();
publishAction.run();
assertThat(latchAssert(confirmLatch)).completes();
assertThat(consumed.get()).isZero();
client.subscribe((byte) 0, stream, OffsetSpecification.timestamp(betweenTwoWaves), 10);
waitAtMost(() -> consumed.get() == messageCount);
}
use of com.rabbitmq.stream.impl.Client.ClientParameters in project rabbitmq-stream-java-client by rabbitmq.
the class PublisherTest method deduplication.
@ParameterizedTest
@MethodSource
void deduplication(String publisherReference, int messageCount, int duplicatedCount, int expectedConsumed) throws Exception {
CountDownLatch publishLatch = new CountDownLatch(messageCount + duplicatedCount);
CountDownLatch consumeLatch = new CountDownLatch(messageCount);
AtomicInteger consumeCount = new AtomicInteger();
Client c = cf.get(new ClientParameters().publishConfirmListener((pubId, publishingId) -> publishLatch.countDown()).chunkListener((client, subscriptionId, offset, messageCount1, dataSize) -> client.credit(subscriptionId, 1)).messageListener((subscriptionId, offset, chunkTimestamp, message) -> {
consumeCount.incrementAndGet();
consumeLatch.countDown();
}));
Response response = c.declarePublisher(b(1), publisherReference, stream);
assertThat(response.isOk()).isTrue();
AtomicLong publishingSequence = new AtomicLong(0);
ToLongFunction<Object> publishingSequenceFunction = o -> publishingSequence.incrementAndGet();
c.declarePublisher(b(1), null, stream);
IntConsumer publishing = i -> c.publish(b(1), Collections.singletonList(c.messageBuilder().addData("".getBytes()).build()), publishingSequenceFunction);
IntStream.range(0, messageCount).forEach(publishing);
publishingSequence.addAndGet(-duplicatedCount);
IntStream.range(0, duplicatedCount).forEach(publishing);
assertThat(publishLatch.await(10, TimeUnit.SECONDS)).isTrue();
response = c.deletePublisher(b(1));
assertThat(response.isOk()).isTrue();
response = c.subscribe(b(1), stream, OffsetSpecification.first(), 10);
assertThat(response.isOk()).isTrue();
assertThat(consumeLatch.await(10, TimeUnit.SECONDS)).isTrue();
Thread.sleep(1000L);
assertThat(consumeCount.get()).isEqualTo(expectedConsumed);
}
use of com.rabbitmq.stream.impl.Client.ClientParameters 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();
}
}
use of com.rabbitmq.stream.impl.Client.ClientParameters in project rabbitmq-stream-java-client by rabbitmq.
the class UtilsTest method exactNodeRetryClientFactoryShouldReturnImmediatelyIfConditionOk.
@Test
void exactNodeRetryClientFactoryShouldReturnImmediatelyIfConditionOk() {
Client client = mock(Client.class);
ClientFactory cf = mock(ClientFactory.class);
when(cf.client(any())).thenReturn(client);
Predicate<Client> condition = c -> true;
Client result = new ExactNodeRetryClientFactory(cf, condition, Duration.ofMillis(1)).client(ClientFactoryContext.fromParameters(new ClientParameters()));
assertThat(result).isEqualTo(client);
verify(cf, times(1)).client(any());
verify(client, never()).close();
}
Aggregations