Search in sources :

Example 1 with TestUtils.b

use of com.rabbitmq.stream.impl.TestUtils.b in project rabbitmq-stream-java-client by rabbitmq.

the class ClientTest method closingPublisherWhilePublishingShouldNotCloseConnection.

@ParameterizedTest
@ValueSource(strings = { "", "some-publisher-reference" })
void closingPublisherWhilePublishingShouldNotCloseConnection(String publisherReference) {
    AtomicReference<CountDownLatch> confirmLatch = new AtomicReference<>(new CountDownLatch(500_000));
    CountDownLatch closedLatch = new CountDownLatch(1);
    Semaphore semaphore = new Semaphore(10000);
    Client client = cf.get(new ClientParameters().shutdownListener(shutdownContext -> closedLatch.countDown()).publishConfirmListener((publisherId, publishingId) -> {
        semaphore.release();
        confirmLatch.get().countDown();
    }));
    Response response = client.declarePublisher(TestUtils.b(0), publisherReference, stream);
    assertThat(response.isOk()).isTrue();
    List<Thread> threads = IntStream.range(0, 10).mapToObj(i -> {
        Thread thread = new Thread(() -> {
            while (!Thread.currentThread().isInterrupted()) {
                List<Message> messages = IntStream.range(0, 100).mapToObj(j -> client.messageBuilder().addData("hello".getBytes(StandardCharsets.UTF_8)).build()).collect(Collectors.toList());
                semaphore.acquireUninterruptibly(100);
                client.publish(b(0), messages);
            }
        });
        thread.start();
        return thread;
    }).collect(Collectors.toList());
    try {
        assertThat(latchAssert(confirmLatch)).completes();
        response = client.deletePublisher(b(0));
        assertThat(response.isOk()).isTrue();
        assertThat(latchAssert(closedLatch)).doesNotComplete(1);
    } finally {
        threads.forEach(Thread::interrupt);
    }
}
Also used : UnpooledByteBufAllocator(io.netty.buffer.UnpooledByteBufAllocator) IntStream(java.util.stream.IntStream) java.util(java.util) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) Connection(com.rabbitmq.client.Connection) QpidProtonCodec(com.rabbitmq.stream.codec.QpidProtonCodec) SimpleCodec(com.rabbitmq.stream.codec.SimpleCodec) Response(com.rabbitmq.stream.impl.Client.Response) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) TestUtils.b(com.rabbitmq.stream.impl.TestUtils.b) TestUtils.streamName(com.rabbitmq.stream.impl.TestUtils.streamName) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) ByteBuffer(java.nio.ByteBuffer) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) DataOutputStream(java.io.DataOutputStream) Charset(java.nio.charset.Charset) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) SwiftMqCodec(com.rabbitmq.stream.codec.SwiftMqCodec) ValueSource(org.junit.jupiter.params.provider.ValueSource) Properties(com.rabbitmq.stream.Properties) TestUtils.waitAtMost(com.rabbitmq.stream.impl.TestUtils.waitAtMost) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) com.rabbitmq.stream(com.rabbitmq.stream) Semaphore(java.util.concurrent.Semaphore) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) TestInfo(org.junit.jupiter.api.TestInfo) LongConsumer(java.util.function.LongConsumer) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) StreamParametersBuilder(com.rabbitmq.stream.impl.Client.StreamParametersBuilder) Channel(com.rabbitmq.client.Channel) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Response(com.rabbitmq.stream.impl.Client.Response) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) CountDownLatch(java.util.concurrent.CountDownLatch) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Channel (com.rabbitmq.client.Channel)1 Connection (com.rabbitmq.client.Connection)1 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)1 com.rabbitmq.stream (com.rabbitmq.stream)1 Properties (com.rabbitmq.stream.Properties)1 QpidProtonCodec (com.rabbitmq.stream.codec.QpidProtonCodec)1 SimpleCodec (com.rabbitmq.stream.codec.SimpleCodec)1 SwiftMqCodec (com.rabbitmq.stream.codec.SwiftMqCodec)1 ClientParameters (com.rabbitmq.stream.impl.Client.ClientParameters)1 Response (com.rabbitmq.stream.impl.Client.Response)1 StreamParametersBuilder (com.rabbitmq.stream.impl.Client.StreamParametersBuilder)1 TestUtils.b (com.rabbitmq.stream.impl.TestUtils.b)1 TestUtils.latchAssert (com.rabbitmq.stream.impl.TestUtils.latchAssert)1 TestUtils.streamName (com.rabbitmq.stream.impl.TestUtils.streamName)1 TestUtils.waitAtMost (com.rabbitmq.stream.impl.TestUtils.waitAtMost)1 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)1 UnpooledByteBufAllocator (io.netty.buffer.UnpooledByteBufAllocator)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 UnknownHostException (java.net.UnknownHostException)1