Search in sources :

Example 11 with Response

use of com.rabbitmq.stream.impl.Client.Response in project rabbitmq-stream-java-client by rabbitmq.

the class ClientTest method publishThenDeleteStreamShouldTriggerPublishErrorListenerWhenPublisherAgain.

@Test
void publishThenDeleteStreamShouldTriggerPublishErrorListenerWhenPublisherAgain(TestInfo info) throws Exception {
    String s = streamName(info);
    Client configurer = cf.get();
    Client.Response response = configurer.create(s);
    assertThat(response.isOk()).isTrue();
    int messageCount = 10;
    AtomicInteger confirms = new AtomicInteger(0);
    Set<Short> responseCodes = ConcurrentHashMap.newKeySet(1);
    Set<Long> publishingIdErrors = ConcurrentHashMap.newKeySet(messageCount);
    CountDownLatch confirmLatch = new CountDownLatch(messageCount);
    CountDownLatch publishErrorLatch = new CountDownLatch(messageCount);
    Client publisher = cf.get(new Client.ClientParameters().publishConfirmListener((publisherId, publishingId) -> {
        confirms.incrementAndGet();
        confirmLatch.countDown();
    }).publishErrorListener((publisherId, publishingId, responseCode) -> {
        publishingIdErrors.add(publishingId);
        responseCodes.add(responseCode);
        publishErrorLatch.countDown();
    }));
    publisher.declarePublisher(b(1), null, s);
    IntStream.range(0, messageCount).forEach(i -> publisher.publish(b(1), Collections.singletonList(publisher.messageBuilder().addData(("first wave" + i).getBytes()).build())));
    assertThat(confirmLatch.await(10, SECONDS)).isTrue();
    assertThat(confirms.get()).isEqualTo(messageCount);
    response = configurer.delete(s);
    assertThat(response.isOk()).isTrue();
    // let the event some time to propagate
    Thread.sleep(1000);
    Set<Long> publishingIds = ConcurrentHashMap.newKeySet(messageCount);
    IntStream.range(0, messageCount).forEach(i -> publishingIds.addAll(publisher.publish(b(1), Collections.singletonList(publisher.messageBuilder().addData(("second wave" + i).getBytes()).build()))));
    assertThat(publishErrorLatch.await(10, SECONDS)).isTrue();
    assertThat(confirms.get()).isEqualTo(messageCount);
    assertThat(responseCodes).hasSize(1).contains(Constants.RESPONSE_CODE_PUBLISHER_DOES_NOT_EXIST);
    assertThat(publishingIdErrors).hasSameSizeAs(publishingIds).hasSameElementsAs(publishingIdErrors);
}
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) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicLong(java.util.concurrent.atomic.AtomicLong) CountDownLatch(java.util.concurrent.CountDownLatch) Response(com.rabbitmq.stream.impl.Client.Response) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 12 with Response

use of com.rabbitmq.stream.impl.Client.Response in project rabbitmq-stream-java-client by rabbitmq.

the class ClientTest method deleteNonExistingStreamShouldReturnError.

@Test
void deleteNonExistingStreamShouldReturnError() {
    String nonExistingStream = UUID.randomUUID().toString();
    Client.Response response = cf.get().delete(nonExistingStream);
    assertThat(response.isOk()).isFalse();
    assertThat(response.getResponseCode()).isEqualTo(Constants.RESPONSE_CODE_STREAM_DOES_NOT_EXIST);
}
Also used : Response(com.rabbitmq.stream.impl.Client.Response) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 13 with Response

use of com.rabbitmq.stream.impl.Client.Response in project rabbitmq-stream-java-client by rabbitmq.

the class ClientTest method createStreamWithDifferentParametersShouldThrowException.

@Test
void createStreamWithDifferentParametersShouldThrowException(TestInfo info) {
    String s = streamName(info);
    Client client = cf.get();
    try {
        StreamParametersBuilder streamParametersBuilder = new StreamParametersBuilder().maxAge(Duration.ofDays(1));
        Response response = client.create(s, streamParametersBuilder.build());
        assertThat(response.isOk()).isTrue();
        response = client.create(s, streamParametersBuilder.maxAge(Duration.ofDays(4)).build());
        assertThat(response.isOk()).isFalse();
        assertThat(response.getResponseCode()).isEqualTo(Constants.RESPONSE_CODE_PRECONDITION_FAILED);
    } finally {
        assertThat(client.delete(s).isOk()).isTrue();
    }
}
Also used : StreamParametersBuilder(com.rabbitmq.stream.impl.Client.StreamParametersBuilder) Response(com.rabbitmq.stream.impl.Client.Response) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 14 with Response

use of com.rabbitmq.stream.impl.Client.Response in project rabbitmq-stream-java-client by rabbitmq.

the class ClientTest method declarePublisherToNonStreamQueueTriggersError.

@Test
void declarePublisherToNonStreamQueueTriggersError() throws Exception {
    String nonStreamQueue = UUID.randomUUID().toString();
    ConnectionFactory connectionFactory = new ConnectionFactory();
    try (Connection amqpConnection = connectionFactory.newConnection()) {
        Channel c = amqpConnection.createChannel();
        c.queueDeclare(nonStreamQueue, false, true, false, null);
        Client client = cf.get();
        Response response = client.declarePublisher(b(1), null, nonStreamQueue);
        assertThat(response.isOk()).isFalse();
        assertThat(response.getResponseCode()).isEqualTo(Constants.RESPONSE_CODE_STREAM_DOES_NOT_EXIST);
    }
}
Also used : Response(com.rabbitmq.stream.impl.Client.Response) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 15 with Response

use of com.rabbitmq.stream.impl.Client.Response in project rabbitmq-stream-java-client by rabbitmq.

the class StreamProducer method close.

@Override
public void close() {
    if (this.closed.compareAndSet(false, true)) {
        if (this.status == Status.RUNNING && this.client != null) {
            LOGGER.debug("Deleting producer {}", this.publisherId);
            Response response = this.client.deletePublisher(this.publisherId);
            if (!response.isOk()) {
                LOGGER.info("Could not delete publisher {} on producer closing: {}", this.publisherId, formatConstant(response.getResponseCode()));
            }
        } else {
            LOGGER.debug("No need to delete producer {}, it is currently unavailable", this.publisherId);
        }
        this.environment.removeProducer(this);
        closeFromEnvironment();
    }
}
Also used : Response(com.rabbitmq.stream.impl.Client.Response)

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