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);
}
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());
}
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();
}
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);
}
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);
}
Aggregations