Search in sources :

Example 6 with Response

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

the class ProducersCoordinatorTest method init.

@BeforeEach
void init() {
    Client.ClientParameters clientParameters = new Client.ClientParameters() {

        @Override
        public Client.ClientParameters shutdownListener(Client.ShutdownListener shutdownListener) {
            ProducersCoordinatorTest.this.shutdownListener = shutdownListener;
            return super.shutdownListener(shutdownListener);
        }

        @Override
        public Client.ClientParameters metadataListener(Client.MetadataListener metadataListener) {
            ProducersCoordinatorTest.this.metadataListener = metadataListener;
            return super.metadataListener(metadataListener);
        }
    };
    mocks = MockitoAnnotations.openMocks(this);
    when(environment.locator()).thenReturn(locator);
    when(environment.locatorOperation(any())).thenCallRealMethod();
    when(environment.clientParametersCopy()).thenReturn(clientParameters);
    when(environment.addressResolver()).thenReturn(address -> address);
    when(trackingConsumer.stream()).thenReturn("stream");
    when(client.declarePublisher(anyByte(), isNull(), anyString())).thenReturn(new Response(Constants.RESPONSE_CODE_OK));
    coordinator = new ProducersCoordinator(environment, ProducersCoordinator.MAX_PRODUCERS_PER_CLIENT, ProducersCoordinator.MAX_TRACKING_CONSUMERS_PER_CLIENT, type -> "producer-connection", clientFactory);
}
Also used : Response(com.rabbitmq.stream.impl.Client.Response) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestUtils.answer(com.rabbitmq.stream.impl.TestUtils.answer) StreamDoesNotExistException(com.rabbitmq.stream.StreamDoesNotExistException) Response(com.rabbitmq.stream.impl.Client.Response) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) MockitoAnnotations(org.mockito.MockitoAnnotations) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Duration(java.time.Duration) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ArgumentMatchers.isNull(org.mockito.ArgumentMatchers.isNull) ClientFactory(com.rabbitmq.stream.impl.Utils.ClientFactory) Mockito.times(org.mockito.Mockito.times) Mockito.anyByte(org.mockito.Mockito.anyByte) Mockito.when(org.mockito.Mockito.when) Executors(java.util.concurrent.Executors) Mockito.verify(org.mockito.Mockito.verify) BackOffDelayPolicy(com.rabbitmq.stream.BackOffDelayPolicy) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito.never(org.mockito.Mockito.never) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) Constants(com.rabbitmq.stream.Constants) Collections(java.util.Collections) TestUtils.metadata(com.rabbitmq.stream.impl.TestUtils.metadata) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 7 with Response

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

the class PublisherTest method queryPublisherSequence.

@Test
void queryPublisherSequence() throws Exception {
    String publisherReference = UUID.randomUUID().toString();
    int messageCount = 10_000;
    int duplicatedCount = messageCount / 10;
    AtomicReference<CountDownLatch> publishLatch = new AtomicReference<>();
    Client c = cf.get(new ClientParameters().publishConfirmListener((pubId, publishingId) -> publishLatch.get().countDown()));
    Response response = c.declarePublisher(b(1), publisherReference, stream);
    assertThat(response.isOk()).isTrue();
    AtomicLong publishingSequence = new AtomicLong(0);
    ToLongFunction<Object> publishingSequenceFunction = o -> publishingSequence.incrementAndGet();
    assertThat(c.queryPublisherSequence(publisherReference, stream)).isEqualTo(0);
    publishLatch.set(new CountDownLatch(messageCount));
    IntConsumer publishing = i -> c.publish(b(1), Collections.singletonList(c.messageBuilder().addData("".getBytes()).build()), publishingSequenceFunction);
    IntStream.range(0, messageCount).forEach(publishing);
    assertThat(publishLatch.get().await(10, TimeUnit.SECONDS)).isTrue();
    assertThat(c.queryPublisherSequence(publisherReference, stream)).isEqualTo(publishingSequence.get());
    long previousSequenceValue = publishingSequence.get();
    publishLatch.set(new CountDownLatch(duplicatedCount));
    publishingSequence.addAndGet(-duplicatedCount);
    IntStream.range(0, duplicatedCount).forEach(publishing);
    assertThat(publishLatch.get().await(10, TimeUnit.SECONDS)).isTrue();
    assertThat(c.queryPublisherSequence(publisherReference, stream)).isEqualTo(previousSequenceValue);
    publishLatch.set(new CountDownLatch(messageCount));
    IntStream.range(0, messageCount).forEach(publishing);
    assertThat(publishLatch.get().await(10, TimeUnit.SECONDS)).isTrue();
    assertThat(c.queryPublisherSequence(publisherReference, stream)).isEqualTo(publishingSequence.get());
}
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) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IntConsumer(java.util.function.IntConsumer) Response(com.rabbitmq.stream.impl.Client.Response) AtomicLong(java.util.concurrent.atomic.AtomicLong) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with Response

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

the class PublisherTest method declarePublisher.

@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = { "publisher-reference" })
void declarePublisher(String publisherReference) throws Exception {
    int messageCount = 10_000;
    CountDownLatch publishLatch = new CountDownLatch(messageCount);
    CountDownLatch consumerLatch = new CountDownLatch(messageCount);
    Client c = cf.get(new ClientParameters().publishConfirmListener((publisherId, publishingId) -> publishLatch.countDown()).chunkListener((client, subscriptionId, offset, messageCount1, dataSize) -> client.credit(subscriptionId, 1)).messageListener((subscriptionId, offset, chunkTimestamp, message) -> consumerLatch.countDown()));
    Response response = c.declarePublisher(b(1), publisherReference, stream);
    assertThat(response.isOk()).isTrue();
    c.declarePublisher(b(1), null, stream);
    IntStream.range(0, messageCount).forEach(i -> c.publish(b(1), Collections.singletonList(c.messageBuilder().addData("".getBytes()).build())));
    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(consumerLatch.await(10, TimeUnit.SECONDS)).isTrue();
}
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) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) CountDownLatch(java.util.concurrent.CountDownLatch) NullAndEmptySource(org.junit.jupiter.params.provider.NullAndEmptySource) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 9 with Response

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

the class PublisherTest method declarePublisherOnStreamThatDoesNotExistShouldReturnError.

@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = { "ref-1" })
void declarePublisherOnStreamThatDoesNotExistShouldReturnError(String reference) {
    String s = UUID.randomUUID().toString();
    Response response = cf.get().declarePublisher(b(1), reference, s);
    assertThat(response.isOk()).isFalse();
    assertThat(response.getResponseCode()).isEqualTo(Constants.RESPONSE_CODE_STREAM_DOES_NOT_EXIST);
}
Also used : Response(com.rabbitmq.stream.impl.Client.Response) NullAndEmptySource(org.junit.jupiter.params.provider.NullAndEmptySource) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 10 with Response

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

the class ClientTest method creditToUnknownSubscriptionShouldTriggerCreditNotification.

@Test
void creditToUnknownSubscriptionShouldTriggerCreditNotification() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    AtomicInteger creditNotificationCount = new AtomicInteger(0);
    AtomicInteger caughtSubscriptionId = new AtomicInteger(0);
    AtomicReference<Short> caughtResponseCode = new AtomicReference<>();
    Client client = cf.get(new Client.ClientParameters().creditNotification((subscriptionId, responseCode) -> {
        creditNotificationCount.incrementAndGet();
        caughtSubscriptionId.set(subscriptionId);
        caughtResponseCode.set(responseCode);
        latch.countDown();
    }));
    Client.Response response = client.subscribe(b(1), stream, OffsetSpecification.first(), 20);
    assertThat(response.isOk()).isTrue();
    assertThat(response.getResponseCode()).isEqualTo(Constants.RESPONSE_CODE_OK);
    client.credit(b(1), 1);
    client.credit(b(42), credit);
    assertThat(latch.await(10, SECONDS)).isTrue();
    assertThat(creditNotificationCount.get()).isEqualTo(1);
    assertThat(caughtSubscriptionId.get()).isEqualTo(42);
    assertThat(caughtResponseCode.get()).isEqualTo(Constants.RESPONSE_CODE_SUBSCRIPTION_ID_DOES_NOT_EXIST);
}
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) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) 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