Search in sources :

Example 1 with Environment

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");
    }
}
Also used : Environment(com.rabbitmq.stream.Environment)

Example 2 with Environment

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();
    });
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) SNIHostName(javax.net.ssl.SNIHostName) Message(com.rabbitmq.stream.Message) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) TestUtils.streamName(com.rabbitmq.stream.impl.TestUtils.streamName) AuthenticationFailureException(com.rabbitmq.stream.AuthenticationFailureException) AfterAll(org.junit.jupiter.api.AfterAll) StreamException(com.rabbitmq.stream.StreamException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) ConsumerBuilder(com.rabbitmq.stream.ConsumerBuilder) Duration(java.time.Duration) Map(java.util.Map) Host(com.rabbitmq.stream.Host) TestUtils.waitAtMost(com.rabbitmq.stream.impl.TestUtils.waitAtMost) Collection(java.util.Collection) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) StandardCharsets(java.nio.charset.StandardCharsets) TestInfo(org.junit.jupiter.api.TestInfo) BackOffDelayPolicy(com.rabbitmq.stream.BackOffDelayPolicy) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) StreamCreator(com.rabbitmq.stream.StreamCreator) SslHandler(io.netty.handler.ssl.SslHandler) ProducerBuilder(com.rabbitmq.stream.ProducerBuilder) Constants(com.rabbitmq.stream.Constants) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) IntStream(java.util.stream.IntStream) SSLParameters(javax.net.ssl.SSLParameters) StreamMetadata(com.rabbitmq.stream.impl.Client.StreamMetadata) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) Address(com.rabbitmq.stream.Address) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) EnvironmentInfo(com.rabbitmq.stream.impl.MonitoringTestUtils.EnvironmentInfo) ConnectException(java.net.ConnectException) ChannelCustomizer(com.rabbitmq.stream.ChannelCustomizer) ValueSource(org.junit.jupiter.params.provider.ValueSource) EventLoopGroup(io.netty.channel.EventLoopGroup) Environment(com.rabbitmq.stream.Environment) Consumer(com.rabbitmq.stream.Consumer) Producer(com.rabbitmq.stream.Producer) EnvironmentBuilder(com.rabbitmq.stream.EnvironmentBuilder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) DisabledIfTlsNotEnabled(com.rabbitmq.stream.impl.TestUtils.DisabledIfTlsNotEnabled) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) TestUtils.localhost(com.rabbitmq.stream.impl.TestUtils.localhost) TestUtils.localhostTls(com.rabbitmq.stream.impl.TestUtils.localhostTls) StreamMetadata(com.rabbitmq.stream.impl.Client.StreamMetadata) Consumer(com.rabbitmq.stream.Consumer) Producer(com.rabbitmq.stream.Producer) Environment(com.rabbitmq.stream.Environment) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with Environment

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();
    }
}
Also used : Environment(com.rabbitmq.stream.Environment) StreamException(com.rabbitmq.stream.StreamException) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with Environment

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);
    }
}
Also used : ProducerBuilder(com.rabbitmq.stream.ProducerBuilder) Address(com.rabbitmq.stream.Address) Environment(com.rabbitmq.stream.Environment) ConsumerBuilder(com.rabbitmq.stream.ConsumerBuilder) StreamCreator(com.rabbitmq.stream.StreamCreator) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with Environment

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();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) SNIHostName(javax.net.ssl.SNIHostName) Message(com.rabbitmq.stream.Message) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) TestUtils.streamName(com.rabbitmq.stream.impl.TestUtils.streamName) AuthenticationFailureException(com.rabbitmq.stream.AuthenticationFailureException) AfterAll(org.junit.jupiter.api.AfterAll) StreamException(com.rabbitmq.stream.StreamException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) ConsumerBuilder(com.rabbitmq.stream.ConsumerBuilder) Duration(java.time.Duration) Map(java.util.Map) Host(com.rabbitmq.stream.Host) TestUtils.waitAtMost(com.rabbitmq.stream.impl.TestUtils.waitAtMost) Collection(java.util.Collection) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) StandardCharsets(java.nio.charset.StandardCharsets) TestInfo(org.junit.jupiter.api.TestInfo) BackOffDelayPolicy(com.rabbitmq.stream.BackOffDelayPolicy) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) StreamCreator(com.rabbitmq.stream.StreamCreator) SslHandler(io.netty.handler.ssl.SslHandler) ProducerBuilder(com.rabbitmq.stream.ProducerBuilder) Constants(com.rabbitmq.stream.Constants) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) IntStream(java.util.stream.IntStream) SSLParameters(javax.net.ssl.SSLParameters) StreamMetadata(com.rabbitmq.stream.impl.Client.StreamMetadata) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) Address(com.rabbitmq.stream.Address) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) EnvironmentInfo(com.rabbitmq.stream.impl.MonitoringTestUtils.EnvironmentInfo) ConnectException(java.net.ConnectException) ChannelCustomizer(com.rabbitmq.stream.ChannelCustomizer) ValueSource(org.junit.jupiter.params.provider.ValueSource) EventLoopGroup(io.netty.channel.EventLoopGroup) Environment(com.rabbitmq.stream.Environment) Consumer(com.rabbitmq.stream.Consumer) Producer(com.rabbitmq.stream.Producer) EnvironmentBuilder(com.rabbitmq.stream.EnvironmentBuilder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) DisabledIfTlsNotEnabled(com.rabbitmq.stream.impl.TestUtils.DisabledIfTlsNotEnabled) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) TestUtils.localhost(com.rabbitmq.stream.impl.TestUtils.localhost) TestUtils.localhostTls(com.rabbitmq.stream.impl.TestUtils.localhostTls) Producer(com.rabbitmq.stream.Producer) Consumer(com.rabbitmq.stream.Consumer) EnvironmentInfo(com.rabbitmq.stream.impl.MonitoringTestUtils.EnvironmentInfo) Environment(com.rabbitmq.stream.Environment) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Environment (com.rabbitmq.stream.Environment)35 Producer (com.rabbitmq.stream.Producer)20 Test (org.junit.jupiter.api.Test)13 Consumer (com.rabbitmq.stream.Consumer)12 Message (com.rabbitmq.stream.Message)12 Address (com.rabbitmq.stream.Address)10 Collections (java.util.Collections)10 OffsetSpecification (com.rabbitmq.stream.OffsetSpecification)9 List (java.util.List)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 ConsumerBuilder (com.rabbitmq.stream.ConsumerBuilder)7 StreamException (com.rabbitmq.stream.StreamException)7 Duration (java.time.Duration)7 ChannelCustomizer (com.rabbitmq.stream.ChannelCustomizer)6 ConfirmationHandler (com.rabbitmq.stream.ConfirmationHandler)6 ProducerBuilder (com.rabbitmq.stream.ProducerBuilder)6 StreamCreator (com.rabbitmq.stream.StreamCreator)6 SslHandler (io.netty.handler.ssl.SslHandler)6 StandardCharsets (java.nio.charset.StandardCharsets)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6