Search in sources :

Example 31 with Environment

use of com.rabbitmq.stream.Environment in project rabbitmq-stream-java-client by rabbitmq.

the class StreamEnvironmentTest method createPublishConsumeDelete.

@ParameterizedTest
@ValueSource(booleans = { false, true })
void createPublishConsumeDelete(boolean lazyInit, TestInfo info) {
    try (Environment env = environmentBuilder.lazyInitialization(lazyInit).build()) {
        String s = streamName(info);
        env.streamCreator().stream(s).create();
        int messageCount = 50_000;
        CountDownLatch confirmLatch = new CountDownLatch(messageCount);
        CountDownLatch consumeLatch = new CountDownLatch(messageCount);
        Producer producer = env.producerBuilder().stream(s).build();
        ConfirmationHandler confirmationHandler = confirmationStatus -> confirmLatch.countDown();
        IntStream.range(0, messageCount).forEach(i -> {
            Message message = producer.messageBuilder().addData("".getBytes(StandardCharsets.UTF_8)).build();
            producer.send(message, confirmationHandler);
        });
        latchAssert(confirmLatch).completes();
        Consumer consumer = env.consumerBuilder().stream(s).offset(OffsetSpecification.first()).messageHandler((context, message) -> consumeLatch.countDown()).build();
        latchAssert(consumeLatch).completes();
        producer.close();
        consumer.close();
        env.deleteStream(s);
    }
}
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) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) Message(com.rabbitmq.stream.Message) Consumer(com.rabbitmq.stream.Consumer) Environment(com.rabbitmq.stream.Environment) CountDownLatch(java.util.concurrent.CountDownLatch) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 32 with Environment

use of com.rabbitmq.stream.Environment in project rabbitmq-stream-java-client by rabbitmq.

the class StreamEnvironmentTest method growShrinkResourcesWhenProducersConsumersAreOpenedAndClosed.

@Test
void growShrinkResourcesWhenProducersConsumersAreOpenedAndClosed(TestInfo info) throws Exception {
    int messageCount = 100;
    int streamCount = 20;
    int producersCount = ProducersCoordinator.MAX_PRODUCERS_PER_CLIENT * 3 + 10;
    int consumersCount = ConsumersCoordinator.MAX_SUBSCRIPTIONS_PER_CLIENT * 2 + 10;
    try (Environment environment = environmentBuilder.build()) {
        List<String> streams = IntStream.range(0, streamCount).mapToObj(i -> streamName(info)).map(s -> {
            environment.streamCreator().stream(s).create();
            return s;
        }).collect(Collectors.toCollection(() -> new CopyOnWriteArrayList<>()));
        CountDownLatch confirmLatch = new CountDownLatch(messageCount * producersCount);
        CountDownLatch consumeLatch = new CountDownLatch(messageCount * producersCount);
        List<Producer> producers = IntStream.range(0, producersCount).mapToObj(i -> {
            String s = streams.get(i % streams.size());
            return environment.producerBuilder().stream(s).build();
        }).collect(Collectors.toList());
        List<Consumer> consumers = IntStream.range(0, consumersCount).mapToObj(i -> {
            String s = streams.get(new Random().nextInt(streams.size()));
            return environment.consumerBuilder().stream(s).messageHandler((offset, message) -> consumeLatch.countDown()).build();
        }).collect(Collectors.toList());
        producers.stream().parallel().forEach(producer -> {
            IntStream.range(0, messageCount).forEach(messageIndex -> {
                producer.send(producer.messageBuilder().addData("".getBytes()).build(), confirmationStatus -> {
                    if (confirmationStatus.isConfirmed()) {
                        confirmLatch.countDown();
                    }
                });
            });
        });
        assertThat(confirmLatch.await(10, SECONDS)).isTrue();
        assertThat(consumeLatch.await(10, SECONDS)).isTrue();
        EnvironmentInfo environmentInfo = MonitoringTestUtils.extract(environment);
        assertThat(environmentInfo.getProducers()).hasSize(1);
        int producerManagerCount = environmentInfo.getProducers().get(0).getClients().size();
        assertThat(producerManagerCount).isPositive();
        assertThat(environmentInfo.getConsumers()).hasSize(1);
        int consumerManagerCount = environmentInfo.getConsumers().get(0).getClients().size();
        assertThat(consumerManagerCount).isPositive();
        java.util.function.Consumer<AutoCloseable> closing = agent -> {
            try {
                agent.close();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        };
        Collections.reverse(producers);
        List<Producer> subProducers = producers.subList(0, ProducersCoordinator.MAX_PRODUCERS_PER_CLIENT);
        subProducers.forEach(closing);
        Collections.reverse(consumers);
        List<Consumer> subConsumers = consumers.subList(0, ConsumersCoordinator.MAX_SUBSCRIPTIONS_PER_CLIENT);
        subConsumers.forEach(closing);
        producers.removeAll(subProducers);
        consumers.removeAll(subConsumers);
        environmentInfo = MonitoringTestUtils.extract(environment);
        assertThat(environmentInfo.getProducers()).hasSize(1);
        assertThat(environmentInfo.getProducers().get(0).getClients()).hasSizeLessThan(producerManagerCount);
        assertThat(environmentInfo.getConsumers()).hasSize(1);
        assertThat(environmentInfo.getConsumers().get(0).getClients()).hasSizeLessThan(consumerManagerCount);
        producers.forEach(closing);
        consumers.forEach(closing);
        streams.stream().forEach(stream -> environment.deleteStream(stream));
    }
}
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) CountDownLatch(java.util.concurrent.CountDownLatch) AuthenticationFailureException(com.rabbitmq.stream.AuthenticationFailureException) StreamException(com.rabbitmq.stream.StreamException) ConnectException(java.net.ConnectException) Producer(com.rabbitmq.stream.Producer) Consumer(com.rabbitmq.stream.Consumer) Random(java.util.Random) EnvironmentInfo(com.rabbitmq.stream.impl.MonitoringTestUtils.EnvironmentInfo) Environment(com.rabbitmq.stream.Environment) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 33 with Environment

use of com.rabbitmq.stream.Environment in project rabbitmq-stream-java-client by rabbitmq.

the class StreamEnvironmentTest method streamCreationShouldBeIdempotent.

@Test
void streamCreationShouldBeIdempotent(TestInfo info) {
    String s = streamName(info);
    Client client = cf.get();
    try (Environment env = environmentBuilder.build()) {
        Duration retention = Duration.ofDays(4);
        env.streamCreator().stream(s).maxAge(retention).create();
        env.streamCreator().stream(s).maxAge(retention).create();
    } finally {
        assertThat(client.delete(s).isOk()).isTrue();
    }
}
Also used : Environment(com.rabbitmq.stream.Environment) Duration(java.time.Duration) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 34 with Environment

use of com.rabbitmq.stream.Environment in project rabbitmq-stream-java-client by rabbitmq.

the class TlsTest method environmentPublisherConsumer.

@Test
void environmentPublisherConsumer() throws Exception {
    try (Environment env = Environment.builder().uri("rabbitmq-stream+tls://localhost").addressResolver(addr -> new Address("localhost", Client.DEFAULT_TLS_PORT)).tls().sslContext(SslContextBuilder.forClient().trustManager(caCertificate()).build()).environmentBuilder().build()) {
        int messageCount = 10_000;
        CountDownLatch latchConfirm = new CountDownLatch(messageCount);
        Producer producer = env.producerBuilder().stream(this.stream).build();
        ConfirmationHandler confirmationHandler = confirmationStatus -> latchConfirm.countDown();
        IntStream.range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().addData("".getBytes(StandardCharsets.UTF_8)).build(), confirmationHandler));
        assertThat(latchAssert(latchConfirm)).completes();
        CountDownLatch latchConsume = new CountDownLatch(messageCount);
        env.consumerBuilder().stream(this.stream).offset(OffsetSpecification.first()).messageHandler((context, message) -> latchConsume.countDown()).build();
        assertThat(latchAssert(latchConsume)).completes();
    }
}
Also used : X509Certificate(java.security.cert.X509Certificate) IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) SNIHostName(javax.net.ssl.SNIHostName) CertificateFactory(java.security.cert.CertificateFactory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SSLParameters(javax.net.ssl.SSLParameters) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) Address(com.rabbitmq.stream.Address) TestUtils.b(com.rabbitmq.stream.impl.TestUtils.b) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) InetAddress(java.net.InetAddress) TRUST_EVERYTHING_TRUST_MANAGER(com.rabbitmq.stream.impl.Utils.TRUST_EVERYTHING_TRUST_MANAGER) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) StreamException(com.rabbitmq.stream.StreamException) Charset(java.nio.charset.Charset) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ChannelCustomizer(com.rabbitmq.stream.ChannelCustomizer) Host(com.rabbitmq.stream.Host) SslContext(io.netty.handler.ssl.SslContext) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) Files(java.nio.file.Files) Environment(com.rabbitmq.stream.Environment) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Producer(com.rabbitmq.stream.Producer) UnknownHostException(java.net.UnknownHostException) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) KeyFactory(java.security.KeyFactory) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) Base64(java.util.Base64) SSLException(javax.net.ssl.SSLException) AfterEach(org.junit.jupiter.api.AfterEach) DisabledIfTlsNotEnabled(com.rabbitmq.stream.impl.TestUtils.DisabledIfTlsNotEnabled) SslHandler(io.netty.handler.ssl.SslHandler) PrivateKey(java.security.PrivateKey) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) Address(com.rabbitmq.stream.Address) InetAddress(java.net.InetAddress) Producer(com.rabbitmq.stream.Producer) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) Environment(com.rabbitmq.stream.Environment) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 35 with Environment

use of com.rabbitmq.stream.Environment in project spring-amqp by spring-projects.

the class StreamListenerContainerTests method testAdviceChain.

@Test
void testAdviceChain() throws Exception {
    Environment env = mock(Environment.class);
    ConsumerBuilder builder = mock(ConsumerBuilder.class);
    given(env.consumerBuilder()).willReturn(builder);
    AtomicReference<MessageHandler> handler = new AtomicReference<>();
    willAnswer(inv -> {
        handler.set(inv.getArgument(0));
        return null;
    }).given(builder).messageHandler(any());
    AtomicBoolean advised = new AtomicBoolean();
    MethodInterceptor advice = (inv) -> {
        advised.set(true);
        return inv.proceed();
    };
    StreamListenerContainer container = new StreamListenerContainer(env);
    container.setAdviceChain(advice);
    AtomicBoolean called = new AtomicBoolean();
    MessageListener ml = mock(MessageListener.class);
    willAnswer(inv -> {
        called.set(true);
        return null;
    }).given(ml).onMessage(any());
    container.setupMessageListener(ml);
    Message message = mock(Message.class);
    given(message.getBodyAsBinary()).willReturn("foo".getBytes());
    Context context = mock(Context.class);
    handler.get().handle(context, message);
    assertThat(advised.get()).isTrue();
    assertThat(called.get()).isTrue();
    advised.set(false);
    called.set(false);
    ChannelAwareMessageListener cal = mock(ChannelAwareMessageListener.class);
    willAnswer(inv -> {
        called.set(true);
        return null;
    }).given(cal).onMessage(any(), isNull());
    container.setupMessageListener(cal);
    handler.get().handle(context, message);
    assertThat(advised.get()).isTrue();
    assertThat(called.get()).isTrue();
    called.set(false);
    StreamMessageListener sml = mock(StreamMessageListener.class);
    willAnswer(inv -> {
        called.set(true);
        return null;
    }).given(sml).onStreamMessage(message, context);
    container.setupMessageListener(sml);
    handler.get().handle(context, message);
    assertThat(advised.get()).isTrue();
    assertThat(called.get()).isTrue();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.isNull(org.mockito.ArgumentMatchers.isNull) MessageListener(org.springframework.amqp.core.MessageListener) MessageHandler(com.rabbitmq.stream.MessageHandler) Message(com.rabbitmq.stream.Message) Environment(com.rabbitmq.stream.Environment) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) BDDMockito.willAnswer(org.mockito.BDDMockito.willAnswer) Test(org.junit.jupiter.api.Test) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Context(com.rabbitmq.stream.MessageHandler.Context) BDDMockito.given(org.mockito.BDDMockito.given) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) ConsumerBuilder(com.rabbitmq.stream.ConsumerBuilder) Mockito.mock(org.mockito.Mockito.mock) Context(com.rabbitmq.stream.MessageHandler.Context) MessageHandler(com.rabbitmq.stream.MessageHandler) Message(com.rabbitmq.stream.Message) MessageListener(org.springframework.amqp.core.MessageListener) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Environment(com.rabbitmq.stream.Environment) ConsumerBuilder(com.rabbitmq.stream.ConsumerBuilder) Test(org.junit.jupiter.api.Test)

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