|
| 1 | +// Copyright (c) 2023 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; |
| 15 | + |
| 16 | +import static com.rabbitmq.stream.TestUtils.*; |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | + |
| 19 | +import com.rabbitmq.client.AMQP; |
| 20 | +import com.rabbitmq.client.Channel; |
| 21 | +import com.rabbitmq.client.Connection; |
| 22 | +import com.rabbitmq.client.ConnectionFactory; |
| 23 | +import com.rabbitmq.stream.TestUtils.CallableConsumer; |
| 24 | +import io.netty.channel.EventLoopGroup; |
| 25 | +import java.util.*; |
| 26 | +import java.util.concurrent.ConcurrentHashMap; |
| 27 | +import java.util.concurrent.CountDownLatch; |
| 28 | +import java.util.function.BiConsumer; |
| 29 | +import java.util.stream.IntStream; |
| 30 | +import org.junit.jupiter.api.AfterEach; |
| 31 | +import org.junit.jupiter.api.BeforeEach; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | +import org.junit.jupiter.api.TestInfo; |
| 34 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 35 | + |
| 36 | +@ExtendWith(TestUtils.StreamTestInfrastructureExtension.class) |
| 37 | +public class SuperStreamExchangeTest { |
| 38 | + |
| 39 | + EventLoopGroup eventLoopGroup; |
| 40 | + |
| 41 | + Environment environment; |
| 42 | + |
| 43 | + Connection connection; |
| 44 | + int partitions = 3; |
| 45 | + int messageCount = 10_000; |
| 46 | + String superStream; |
| 47 | + |
| 48 | + @BeforeEach |
| 49 | + void init(TestInfo info) throws Exception { |
| 50 | + EnvironmentBuilder environmentBuilder = |
| 51 | + Environment.builder() |
| 52 | + .port(TestUtils.streamPortNode1()) |
| 53 | + .netty() |
| 54 | + .eventLoopGroup(eventLoopGroup) |
| 55 | + .environmentBuilder(); |
| 56 | + environment = environmentBuilder.build(); |
| 57 | + ConnectionFactory cf = new ConnectionFactory(); |
| 58 | + cf.setPort(TestUtils.amqpPortNode1()); |
| 59 | + connection = cf.newConnection(); |
| 60 | + superStream = TestUtils.streamName(info); |
| 61 | + } |
| 62 | + |
| 63 | + @AfterEach |
| 64 | + void tearDown() throws Exception { |
| 65 | + environment.close(); |
| 66 | + deleteSuperStreamTopology(connection, superStream, partitions); |
| 67 | + connection.close(); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void publish() throws Exception { |
| 72 | + declareSuperStreamTopology(connection, superStream, partitions); |
| 73 | + List<String> routingKeys = new ArrayList<>(messageCount); |
| 74 | + IntStream.range(0, messageCount) |
| 75 | + .forEach(ignored -> routingKeys.add(UUID.randomUUID().toString())); |
| 76 | + |
| 77 | + CountDownLatch publishLatch = new CountDownLatch(messageCount); |
| 78 | + try (Producer producer = |
| 79 | + environment |
| 80 | + .producerBuilder() |
| 81 | + .superStream(superStream) |
| 82 | + .routing(msg -> msg.getProperties().getMessageIdAsString()) |
| 83 | + .producerBuilder() |
| 84 | + .build()) { |
| 85 | + ConfirmationHandler confirmationHandler = status -> publishLatch.countDown(); |
| 86 | + routingKeys.forEach( |
| 87 | + rk -> |
| 88 | + producer.send( |
| 89 | + producer.messageBuilder().properties().messageId(rk).messageBuilder().build(), |
| 90 | + confirmationHandler)); |
| 91 | + |
| 92 | + assertThat(publishLatch).is(completed()); |
| 93 | + } |
| 94 | + |
| 95 | + CallableConsumer<Map<String, Set<String>>> consumeMessages = |
| 96 | + receivedMessages -> { |
| 97 | + CountDownLatch consumeLatch = new CountDownLatch(messageCount); |
| 98 | + try (Consumer ignored = |
| 99 | + environment |
| 100 | + .consumerBuilder() |
| 101 | + .superStream(superStream) |
| 102 | + .offset(OffsetSpecification.first()) |
| 103 | + .messageHandler( |
| 104 | + (ctx, msg) -> { |
| 105 | + receivedMessages |
| 106 | + .computeIfAbsent(ctx.stream(), k -> ConcurrentHashMap.newKeySet()) |
| 107 | + .add(msg.getProperties().getMessageIdAsString()); |
| 108 | + consumeLatch.countDown(); |
| 109 | + }) |
| 110 | + .build()) { |
| 111 | + |
| 112 | + assertThat(consumeLatch).is(completed()); |
| 113 | + assertThat(receivedMessages.values().stream().mapToInt(Set::size).sum()) |
| 114 | + .isEqualTo(messageCount); |
| 115 | + } |
| 116 | + }; |
| 117 | + |
| 118 | + Map<String, Set<String>> streamProducerMessages = new ConcurrentHashMap<>(partitions); |
| 119 | + consumeMessages.accept(streamProducerMessages); |
| 120 | + |
| 121 | + deleteSuperStreamTopology(connection, superStream, partitions); |
| 122 | + declareSuperStreamTopology(connection, superStream, partitions); |
| 123 | + |
| 124 | + try (Channel channel = connection.createChannel()) { |
| 125 | + channel.confirmSelect(); |
| 126 | + for (String rk : routingKeys) { |
| 127 | + channel.basicPublish( |
| 128 | + superStream, rk, new AMQP.BasicProperties.Builder().messageId(rk).build(), null); |
| 129 | + } |
| 130 | + channel.waitForConfirmsOrDie(); |
| 131 | + } |
| 132 | + |
| 133 | + Map<String, Set<String>> amqpProducerMessages = new ConcurrentHashMap<>(partitions); |
| 134 | + consumeMessages.accept(amqpProducerMessages); |
| 135 | + assertThat(amqpProducerMessages) |
| 136 | + .hasSameSizeAs(streamProducerMessages) |
| 137 | + .containsKeys(streamProducerMessages.keySet().toArray(new String[] {})); |
| 138 | + |
| 139 | + BiConsumer<Set<String>, Set<String>> compareSets = |
| 140 | + (s1, s2) -> { |
| 141 | + assertThat(s1).hasSameSizeAs(s2); |
| 142 | + s1.forEach(rk -> assertThat(s2).contains(rk)); |
| 143 | + }; |
| 144 | + |
| 145 | + amqpProducerMessages.forEach( |
| 146 | + (key, value) -> compareSets.accept(value, streamProducerMessages.get(key))); |
| 147 | + } |
| 148 | +} |
0 commit comments