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