use of com.rabbitmq.stream.impl.Client.Response in project rabbitmq-stream-java-client by rabbitmq.
the class ClientTest method consume.
@Test
void consume() throws Exception {
int publishCount = 100000;
byte correlationId = 42;
TestUtils.publishAndWaitForConfirms(cf, publishCount, stream);
CountDownLatch latch = new CountDownLatch(publishCount);
AtomicInteger receivedCorrelationId = new AtomicInteger();
Client.ChunkListener chunkListener = (client, corr, offset, messageCountInChunk, dataSize) -> {
receivedCorrelationId.set(corr);
client.credit(correlationId, 1);
};
AtomicLong chunkTimestamp = new AtomicLong();
Client.MessageListener messageListener = (corr, offset, chkTimestamp, message) -> {
chunkTimestamp.set(chkTimestamp);
latch.countDown();
};
Client client = cf.get(new Client.ClientParameters().chunkListener(chunkListener).messageListener(messageListener));
Response response = client.subscribe(correlationId, stream, OffsetSpecification.first(), credit);
assertThat(response.getResponseCode()).isEqualTo(Constants.RESPONSE_CODE_OK);
assertThat(response.isOk()).isTrue();
assertThat(latch.await(60, SECONDS)).isTrue();
assertThat(receivedCorrelationId).hasValue(correlationId);
assertThat(chunkTimestamp.get()).isNotZero();
client.close();
}
use of com.rabbitmq.stream.impl.Client.Response in project rabbitmq-stream-java-client by rabbitmq.
the class ClientTest method closingPublisherWhilePublishingShouldNotCloseConnection.
@ParameterizedTest
@ValueSource(strings = { "", "some-publisher-reference" })
void closingPublisherWhilePublishingShouldNotCloseConnection(String publisherReference) {
AtomicReference<CountDownLatch> confirmLatch = new AtomicReference<>(new CountDownLatch(500_000));
CountDownLatch closedLatch = new CountDownLatch(1);
Semaphore semaphore = new Semaphore(10000);
Client client = cf.get(new ClientParameters().shutdownListener(shutdownContext -> closedLatch.countDown()).publishConfirmListener((publisherId, publishingId) -> {
semaphore.release();
confirmLatch.get().countDown();
}));
Response response = client.declarePublisher(TestUtils.b(0), publisherReference, stream);
assertThat(response.isOk()).isTrue();
List<Thread> threads = IntStream.range(0, 10).mapToObj(i -> {
Thread thread = new Thread(() -> {
while (!Thread.currentThread().isInterrupted()) {
List<Message> messages = IntStream.range(0, 100).mapToObj(j -> client.messageBuilder().addData("hello".getBytes(StandardCharsets.UTF_8)).build()).collect(Collectors.toList());
semaphore.acquireUninterruptibly(100);
client.publish(b(0), messages);
}
});
thread.start();
return thread;
}).collect(Collectors.toList());
try {
assertThat(latchAssert(confirmLatch)).completes();
response = client.deletePublisher(b(0));
assertThat(response.isOk()).isTrue();
assertThat(latchAssert(closedLatch)).doesNotComplete(1);
} finally {
threads.forEach(Thread::interrupt);
}
}
use of com.rabbitmq.stream.impl.Client.Response in project rabbitmq-stream-java-client by rabbitmq.
the class SubEntryBatchingTest method subEntriesCompressedWithDifferentCompressionsShouldBeReadCorrectly.
@Test
void subEntriesCompressedWithDifferentCompressionsShouldBeReadCorrectly() {
List<CompressionCodecFactory> compressionCodecFactories = compressionCodecFactories().collect(Collectors.toList());
int batchCount = compressionCodecFactories.size() * Compression.values().length;
int messagesInBatch = 30;
int messageCount = batchCount * messagesInBatch;
AtomicInteger messageIndex = new AtomicInteger(0);
CountDownLatch publishLatch = new CountDownLatch(batchCount);
Set<String> publishedBodies = ConcurrentHashMap.newKeySet(messageCount);
compressionCodecFactories.forEach(compressionCodecFactory -> {
Client publisher = cf.get(new ClientParameters().compressionCodecFactory(compressionCodecFactory).publishConfirmListener((publisherId, publishingId) -> publishLatch.countDown()));
Response response = publisher.declarePublisher(b(0), null, stream);
assertThat(response.isOk()).isTrue();
for (Compression compression : Compression.values()) {
MessageBatch messageBatch = new MessageBatch(compression);
IntStream.range(0, messagesInBatch).forEach(i -> {
String body = "compression " + compression.name() + " message " + messageIndex.getAndIncrement();
messageBatch.add(publisher.messageBuilder().addData(body.getBytes(UTF8)).build());
publishedBodies.add(body);
});
publisher.publishBatches(b(0), Collections.singletonList(messageBatch));
}
});
assertThat(latchAssert(publishLatch)).completes();
compressionCodecFactories.forEach(compressionCodecFactory -> {
CountDownLatch consumeLatch = new CountDownLatch(messageCount);
Set<String> consumedBodies = ConcurrentHashMap.newKeySet(messageCount);
Client consumer = cf.get(new ClientParameters().compressionCodecFactory(compressionCodecFactory).chunkListener((client, subscriptionId, offset, messageCount1, dataSize) -> client.credit(subscriptionId, 1)).messageListener((subscriptionId, offset, chunkTimestamp, message) -> {
consumedBodies.add(new String(message.getBodyAsBinary(), UTF8));
consumeLatch.countDown();
}));
Response response = consumer.subscribe(b(1), stream, OffsetSpecification.first(), 2);
assertThat(response.isOk()).isTrue();
assertThat(latchAssert(consumeLatch)).completes();
assertThat(consumedBodies).hasSize(messageCount).hasSameSizeAs(publishedBodies);
publishedBodies.forEach(publishBody -> assertThat(consumedBodies.contains(publishBody)).isTrue());
});
}
use of com.rabbitmq.stream.impl.Client.Response 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.Response in project rabbitmq-stream-java-client by rabbitmq.
the class PublisherTest method deleteNonExistingPublisherShouldReturnError.
@Test
void deleteNonExistingPublisherShouldReturnError() {
Response response = cf.get().deletePublisher(b(42));
assertThat(response.isOk()).isFalse();
assertThat(response.getResponseCode()).isEqualTo(Constants.RESPONSE_CODE_PUBLISHER_DOES_NOT_EXIST);
}
Aggregations