use of com.rabbitmq.stream.Environment in project spring-amqp by spring-projects.
the class RabbitListenerTests method deleteQueues.
// @AfterAll - causes test to throw errors - need to investigate
static void deleteQueues() {
try (Environment environment = Config.environment()) {
environment.deleteStream("test.stream.queue1");
environment.deleteStream("test.stream.queue2");
environment.deleteStream("stream.created.over.amqp");
}
}
use of com.rabbitmq.stream.Environment in project rabbitmq-stream-java-client by rabbitmq.
the class StreamEnvironmentTest method environmentPublishersConsumersShouldCloseSuccessfullyWhenBrokerIsDown.
@Test
@TestUtils.DisabledIfRabbitMqCtlNotSet
void environmentPublishersConsumersShouldCloseSuccessfullyWhenBrokerIsDown() throws Exception {
Environment environment = environmentBuilder.recoveryBackOffDelayPolicy(BackOffDelayPolicy.fixed(Duration.ofSeconds(10))).build();
CountDownLatch consumeLatch = new CountDownLatch(2);
Consumer consumer = environment.consumerBuilder().stream(stream).messageHandler((context, message) -> consumeLatch.countDown()).build();
// will be closed by the environment
environment.consumerBuilder().stream(stream).messageHandler((context, message) -> consumeLatch.countDown()).build();
Producer producer = environment.producerBuilder().stream(stream).build();
// will be closed by the environment
environment.producerBuilder().stream(stream).build();
producer.send(producer.messageBuilder().addData("".getBytes(StandardCharsets.UTF_8)).build(), confirmationStatus -> {
});
latchAssert(consumeLatch).completes();
try {
Host.rabbitmqctl("stop_app");
producer.close();
consumer.close();
environment.close();
} finally {
Host.rabbitmqctl("start_app");
}
waitAtMost(30, () -> {
Client client = cf.get();
Map<String, StreamMetadata> metadata = client.metadata(stream);
return metadata.containsKey(stream) && metadata.get(stream).isResponseOk();
});
}
use of com.rabbitmq.stream.Environment in project rabbitmq-stream-java-client by rabbitmq.
the class StreamEnvironmentTest method createStreamWithDifferentParametersShouldThrowException.
@Test
void createStreamWithDifferentParametersShouldThrowException(TestInfo info) {
String s = streamName(info);
Client client = cf.get();
try (Environment env = environmentBuilder.build()) {
env.streamCreator().stream(s).maxAge(Duration.ofDays(1)).create();
assertThatThrownBy(() -> env.streamCreator().stream(s).maxAge(Duration.ofDays(4)).create()).isInstanceOf(StreamException.class).has(TestUtils.responseCode(Constants.RESPONSE_CODE_PRECONDITION_FAILED));
} finally {
assertThat(client.delete(s).isOk()).isTrue();
}
}
use of com.rabbitmq.stream.Environment in project rabbitmq-stream-java-client by rabbitmq.
the class StreamEnvironmentTest method instanciationShouldSucceedWhenLazyInitIsEnabledAndHostIsNotKnown.
@Test
void instanciationShouldSucceedWhenLazyInitIsEnabledAndHostIsNotKnown() {
String dummyHost = UUID.randomUUID().toString();
Address dummyAddress = new Address(dummyHost, Client.DEFAULT_PORT);
try (Environment env = environmentBuilder.host(dummyHost).addressResolver(a -> dummyAddress).lazyInitialization(true).build()) {
StreamCreator streamCreator = env.streamCreator().stream("should not have been created");
assertThatThrownBy(() -> streamCreator.create()).isInstanceOf(StreamException.class);
assertThatThrownBy(() -> env.deleteStream("should not exist")).isInstanceOf(StreamException.class);
ProducerBuilder producerBuilder = env.producerBuilder().stream(this.stream);
assertThatThrownBy(() -> producerBuilder.build()).isInstanceOf(StreamException.class);
ConsumerBuilder consumerBuilder = env.consumerBuilder().stream(this.stream).messageHandler((context, message) -> {
});
assertThatThrownBy(() -> consumerBuilder.build()).isInstanceOf(StreamException.class);
}
}
use of com.rabbitmq.stream.Environment in project rabbitmq-stream-java-client by rabbitmq.
the class StreamEnvironmentTest method producersAndConsumersShouldBeClosedWhenEnvironmentIsClosed.
@ParameterizedTest
@ValueSource(booleans = { false, true })
void producersAndConsumersShouldBeClosedWhenEnvironmentIsClosed(boolean lazyInit) {
Environment environment = environmentBuilder.lazyInitialization(lazyInit).build();
Collection<Producer> producers = IntStream.range(0, 2).mapToObj(i -> environment.producerBuilder().stream(stream).build()).collect(Collectors.toList());
Collection<Consumer> consumers = IntStream.range(0, 2).mapToObj(i -> environment.consumerBuilder().stream(stream).name(UUID.randomUUID().toString()).messageHandler((offset, message) -> {
}).build()).collect(Collectors.toList());
producers.forEach(producer -> assertThat(((StreamProducer) producer).isOpen()).isTrue());
consumers.forEach(consumer -> assertThat(((StreamConsumer) consumer).isOpen()).isTrue());
EnvironmentInfo environmentInfo = MonitoringTestUtils.extract(environment);
assertThat(environmentInfo.getLocator()).isNotNull();
assertThat(environmentInfo.getProducers()).hasSize(1).element(0).extracting(pool -> pool.getClients()).asList().hasSize(1);
assertThat(environmentInfo.getProducers().get(0).getClients().get(0).getProducerCount()).isEqualTo(2);
assertThat(environmentInfo.getProducers().get(0).getClients().get(0).getTrackingConsumerCount()).isEqualTo(2);
assertThat(environmentInfo.getConsumers()).hasSize(1).element(0).extracting(pool -> pool.getClients()).asList().hasSize(1);
assertThat(environmentInfo.getConsumers().get(0).getClients().get(0).getConsumerCount()).isEqualTo(2);
environment.close();
producers.forEach(producer -> assertThat(((StreamProducer) producer).isOpen()).isFalse());
consumers.forEach(consumer -> assertThat(((StreamConsumer) consumer).isOpen()).isFalse());
environmentInfo = MonitoringTestUtils.extract(environment);
assertThat(environmentInfo.getLocator()).isNull();
assertThat(environmentInfo.getProducers()).isEmpty();
assertThat(environmentInfo.getConsumers()).isEmpty();
}
Aggregations