Search in sources :

Example 16 with Response

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();
}
Also used : UnpooledByteBufAllocator(io.netty.buffer.UnpooledByteBufAllocator) IntStream(java.util.stream.IntStream) java.util(java.util) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) Connection(com.rabbitmq.client.Connection) QpidProtonCodec(com.rabbitmq.stream.codec.QpidProtonCodec) SimpleCodec(com.rabbitmq.stream.codec.SimpleCodec) Response(com.rabbitmq.stream.impl.Client.Response) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) TestUtils.b(com.rabbitmq.stream.impl.TestUtils.b) TestUtils.streamName(com.rabbitmq.stream.impl.TestUtils.streamName) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) ByteBuffer(java.nio.ByteBuffer) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) DataOutputStream(java.io.DataOutputStream) Charset(java.nio.charset.Charset) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) SwiftMqCodec(com.rabbitmq.stream.codec.SwiftMqCodec) ValueSource(org.junit.jupiter.params.provider.ValueSource) Properties(com.rabbitmq.stream.Properties) TestUtils.waitAtMost(com.rabbitmq.stream.impl.TestUtils.waitAtMost) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) com.rabbitmq.stream(com.rabbitmq.stream) Semaphore(java.util.concurrent.Semaphore) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) TestInfo(org.junit.jupiter.api.TestInfo) LongConsumer(java.util.function.LongConsumer) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) StreamParametersBuilder(com.rabbitmq.stream.impl.Client.StreamParametersBuilder) Channel(com.rabbitmq.client.Channel) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Response(com.rabbitmq.stream.impl.Client.Response) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 17 with Response

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);
    }
}
Also used : UnpooledByteBufAllocator(io.netty.buffer.UnpooledByteBufAllocator) IntStream(java.util.stream.IntStream) java.util(java.util) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) Connection(com.rabbitmq.client.Connection) QpidProtonCodec(com.rabbitmq.stream.codec.QpidProtonCodec) SimpleCodec(com.rabbitmq.stream.codec.SimpleCodec) Response(com.rabbitmq.stream.impl.Client.Response) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) TestUtils.b(com.rabbitmq.stream.impl.TestUtils.b) TestUtils.streamName(com.rabbitmq.stream.impl.TestUtils.streamName) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) ByteBuffer(java.nio.ByteBuffer) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) DataOutputStream(java.io.DataOutputStream) Charset(java.nio.charset.Charset) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) SwiftMqCodec(com.rabbitmq.stream.codec.SwiftMqCodec) ValueSource(org.junit.jupiter.params.provider.ValueSource) Properties(com.rabbitmq.stream.Properties) TestUtils.waitAtMost(com.rabbitmq.stream.impl.TestUtils.waitAtMost) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) com.rabbitmq.stream(com.rabbitmq.stream) Semaphore(java.util.concurrent.Semaphore) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) TestInfo(org.junit.jupiter.api.TestInfo) LongConsumer(java.util.function.LongConsumer) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) StreamParametersBuilder(com.rabbitmq.stream.impl.Client.StreamParametersBuilder) Channel(com.rabbitmq.client.Channel) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Response(com.rabbitmq.stream.impl.Client.Response) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) CountDownLatch(java.util.concurrent.CountDownLatch) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 18 with Response

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());
    });
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) CommonsCompressCompressionCodecFactory(com.rabbitmq.stream.compression.CommonsCompressCompressionCodecFactory) HashMap(java.util.HashMap) Response(com.rabbitmq.stream.impl.Client.Response) TestUtils.b(com.rabbitmq.stream.impl.TestUtils.b) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) Charset(java.nio.charset.Charset) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) DefaultCompressionCodecFactory(com.rabbitmq.stream.compression.DefaultCompressionCodecFactory) MethodSource(org.junit.jupiter.params.provider.MethodSource) MetricsCollector(com.rabbitmq.stream.metrics.MetricsCollector) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Compression(com.rabbitmq.stream.compression.Compression) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) TestInfo(org.junit.jupiter.api.TestInfo) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) CompressionCodecFactory(com.rabbitmq.stream.compression.CompressionCodecFactory) Stream(java.util.stream.Stream) Collections(java.util.Collections) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) Compression(com.rabbitmq.stream.compression.Compression) CountDownLatch(java.util.concurrent.CountDownLatch) Response(com.rabbitmq.stream.impl.Client.Response) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) CommonsCompressCompressionCodecFactory(com.rabbitmq.stream.compression.CommonsCompressCompressionCodecFactory) DefaultCompressionCodecFactory(com.rabbitmq.stream.compression.DefaultCompressionCodecFactory) CompressionCodecFactory(com.rabbitmq.stream.compression.CompressionCodecFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 19 with Response

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);
}
Also used : IntStream(java.util.stream.IntStream) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IntConsumer(java.util.function.IntConsumer) Response(com.rabbitmq.stream.impl.Client.Response) AtomicReference(java.util.concurrent.atomic.AtomicReference) TestUtils.b(com.rabbitmq.stream.impl.TestUtils.b) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ToLongFunction(java.util.function.ToLongFunction) MethodSource(org.junit.jupiter.params.provider.MethodSource) ValueSource(org.junit.jupiter.params.provider.ValueSource) NullAndEmptySource(org.junit.jupiter.params.provider.NullAndEmptySource) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) UUID(java.util.UUID) Arguments(org.junit.jupiter.params.provider.Arguments) TestInfo(org.junit.jupiter.api.TestInfo) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) Constants(com.rabbitmq.stream.Constants) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) Response(com.rabbitmq.stream.impl.Client.Response) AtomicLong(java.util.concurrent.atomic.AtomicLong) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) IntConsumer(java.util.function.IntConsumer) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 20 with Response

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);
}
Also used : Response(com.rabbitmq.stream.impl.Client.Response) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Response (com.rabbitmq.stream.impl.Client.Response)21 Test (org.junit.jupiter.api.Test)18 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)18 CountDownLatch (java.util.concurrent.CountDownLatch)11 IntStream (java.util.stream.IntStream)11 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)11 ClientParameters (com.rabbitmq.stream.impl.Client.ClientParameters)10 TestUtils.b (com.rabbitmq.stream.impl.TestUtils.b)10 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 TestInfo (org.junit.jupiter.api.TestInfo)10 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 ValueSource (org.junit.jupiter.params.provider.ValueSource)9 AtomicLong (java.util.concurrent.atomic.AtomicLong)8 StreamParametersBuilder (com.rabbitmq.stream.impl.Client.StreamParametersBuilder)7 TestUtils.latchAssert (com.rabbitmq.stream.impl.TestUtils.latchAssert)7 StandardCharsets (java.nio.charset.StandardCharsets)7 SECONDS (java.util.concurrent.TimeUnit.SECONDS)7 Collectors (java.util.stream.Collectors)7