|
| 1 | +// Copyright (c) 2021 VMware, Inc. or its affiliates. All rights reserved. |
| 2 | +// |
| 3 | +// This software, the RabbitMQ Stream Java client library, is dual-licensed under the |
| 4 | +// Mozilla Public License 2.0 ("MPL"), and the Apache License version 2 ("ASL"). |
| 5 | +// For the MPL, please see LICENSE-MPL-RabbitMQ. For the ASL, |
| 6 | +// please see LICENSE-APACHE2. |
| 7 | +// |
| 8 | +// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, |
| 9 | +// either express or implied. See the LICENSE file for specific language governing |
| 10 | +// rights and limitations of this software. |
| 11 | +// |
| 12 | +// If you have any questions regarding licensing, please contact us at |
| 13 | + |
| 14 | +package com.rabbitmq.stream.impl; |
| 15 | + |
| 16 | +import static com.rabbitmq.stream.impl.TestUtils.b; |
| 17 | +import static com.rabbitmq.stream.impl.TestUtils.declareSuperStreamTopology; |
| 18 | +import static com.rabbitmq.stream.impl.TestUtils.deleteSuperStreamTopology; |
| 19 | +import static com.rabbitmq.stream.impl.TestUtils.latchAssert; |
| 20 | +import static com.rabbitmq.stream.impl.TestUtils.localhost; |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | + |
| 23 | +import com.rabbitmq.client.Connection; |
| 24 | +import com.rabbitmq.client.ConnectionFactory; |
| 25 | +import com.rabbitmq.stream.Consumer; |
| 26 | +import com.rabbitmq.stream.Environment; |
| 27 | +import com.rabbitmq.stream.EnvironmentBuilder; |
| 28 | +import com.rabbitmq.stream.OffsetSpecification; |
| 29 | +import com.rabbitmq.stream.impl.Client.ClientParameters; |
| 30 | +import com.rabbitmq.stream.impl.TestUtils.BrokerVersionAtLeast; |
| 31 | +import io.netty.channel.EventLoopGroup; |
| 32 | +import java.nio.charset.StandardCharsets; |
| 33 | +import java.util.Collections; |
| 34 | +import java.util.List; |
| 35 | +import java.util.concurrent.ConcurrentHashMap; |
| 36 | +import java.util.concurrent.ConcurrentMap; |
| 37 | +import java.util.concurrent.CountDownLatch; |
| 38 | +import java.util.concurrent.atomic.AtomicInteger; |
| 39 | +import org.junit.jupiter.api.AfterEach; |
| 40 | +import org.junit.jupiter.api.BeforeEach; |
| 41 | +import org.junit.jupiter.api.Test; |
| 42 | +import org.junit.jupiter.api.TestInfo; |
| 43 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 44 | + |
| 45 | +@ExtendWith(TestUtils.StreamTestInfrastructureExtension.class) |
| 46 | +public class SuperStreamConsumerTest { |
| 47 | + |
| 48 | + EventLoopGroup eventLoopGroup; |
| 49 | + |
| 50 | + Environment environment; |
| 51 | + |
| 52 | + Connection connection; |
| 53 | + int partitionCount = 3; |
| 54 | + String superStream; |
| 55 | + String[] routingKeys = null; |
| 56 | + TestUtils.ClientFactory cf; |
| 57 | + |
| 58 | + private static void publishToPartitions( |
| 59 | + TestUtils.ClientFactory cf, List<String> partitions, int messageCount) { |
| 60 | + CountDownLatch publishLatch = new CountDownLatch(messageCount); |
| 61 | + Client client = |
| 62 | + cf.get( |
| 63 | + new ClientParameters() |
| 64 | + .publishConfirmListener((publisherId, publishingId) -> publishLatch.countDown())); |
| 65 | + for (int i = 0; i < partitions.size(); i++) { |
| 66 | + assertThat(client.declarePublisher(b(i), null, partitions.get(i)).isOk()).isTrue(); |
| 67 | + } |
| 68 | + for (int i = 0; i < messageCount; i++) { |
| 69 | + int partitionIndex = i % partitions.size(); |
| 70 | + String partition = partitions.get(partitionIndex); |
| 71 | + client.publish( |
| 72 | + b(partitionIndex), |
| 73 | + Collections.singletonList( |
| 74 | + client.messageBuilder().addData(partition.getBytes(StandardCharsets.UTF_8)).build())); |
| 75 | + } |
| 76 | + latchAssert(publishLatch).completes(); |
| 77 | + } |
| 78 | + |
| 79 | + @BeforeEach |
| 80 | + void init(TestInfo info) throws Exception { |
| 81 | + EnvironmentBuilder environmentBuilder = Environment.builder().eventLoopGroup(eventLoopGroup); |
| 82 | + environmentBuilder.addressResolver(add -> localhost()); |
| 83 | + environment = environmentBuilder.build(); |
| 84 | + superStream = TestUtils.streamName(info); |
| 85 | + connection = new ConnectionFactory().newConnection(); |
| 86 | + } |
| 87 | + |
| 88 | + @AfterEach |
| 89 | + void tearDown() throws Exception { |
| 90 | + environment.close(); |
| 91 | + if (routingKeys == null) { |
| 92 | + deleteSuperStreamTopology(connection, superStream, partitionCount); |
| 93 | + } else { |
| 94 | + deleteSuperStreamTopology(connection, superStream, routingKeys); |
| 95 | + } |
| 96 | + connection.close(); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + @BrokerVersionAtLeast("3.9.6") |
| 101 | + void consumeAllMessagesFromAllPartitions() throws Exception { |
| 102 | + declareSuperStreamTopology(connection, superStream, partitionCount); |
| 103 | + Client client = cf.get(); |
| 104 | + List<String> partitions = client.partitions(superStream); |
| 105 | + int messageCount = 10000 * partitionCount; |
| 106 | + publishToPartitions(cf, partitions, messageCount); |
| 107 | + ConcurrentMap<String, AtomicInteger> messagesReceived = new ConcurrentHashMap<>(partitionCount); |
| 108 | + partitions.forEach(p -> messagesReceived.put(p, new AtomicInteger(0))); |
| 109 | + CountDownLatch consumeLatch = new CountDownLatch(messageCount); |
| 110 | + Consumer consumer = |
| 111 | + environment |
| 112 | + .consumerBuilder() |
| 113 | + .superStream(superStream) |
| 114 | + .offset(OffsetSpecification.first()) |
| 115 | + .messageHandler( |
| 116 | + (context, message) -> { |
| 117 | + String partition = new String(message.getBodyAsBinary()); |
| 118 | + messagesReceived.get(partition).incrementAndGet(); |
| 119 | + consumeLatch.countDown(); |
| 120 | + }) |
| 121 | + .build(); |
| 122 | + latchAssert(consumeLatch).completes(); |
| 123 | + assertThat(messagesReceived).hasSize(partitionCount); |
| 124 | + partitions.forEach( |
| 125 | + p -> { |
| 126 | + assertThat(messagesReceived).containsKey(p); |
| 127 | + assertThat(messagesReceived.get(p).get()).isEqualTo(messageCount / partitionCount); |
| 128 | + }); |
| 129 | + consumer.close(); |
| 130 | + } |
| 131 | +} |
0 commit comments