|
13 | 13 | |
14 | 14 | package com.rabbitmq.stream.impl; |
15 | 15 |
|
| 16 | +import static io.vavr.Tuple.*; |
16 | 17 | import static java.lang.String.format; |
| 18 | +import static java.util.Arrays.asList; |
17 | 19 | import static java.util.concurrent.TimeUnit.SECONDS; |
18 | 20 | import static java.util.stream.Collectors.toList; |
19 | 21 | import static org.assertj.core.api.Assertions.assertThat; |
20 | 22 | import static org.junit.jupiter.api.Assertions.fail; |
21 | 23 |
|
22 | 24 | import ch.qos.logback.classic.Level; |
| 25 | +import com.rabbitmq.client.BuiltinExchangeType; |
| 26 | +import com.rabbitmq.client.Channel; |
| 27 | +import com.rabbitmq.client.Connection; |
| 28 | +import com.rabbitmq.client.ConnectionFactory; |
23 | 29 | import com.rabbitmq.stream.Address; |
24 | 30 | import com.rabbitmq.stream.Constants; |
25 | 31 | import com.rabbitmq.stream.Host; |
|
32 | 38 | import com.rabbitmq.stream.impl.Client.StreamMetadata; |
33 | 39 | import io.netty.channel.EventLoopGroup; |
34 | 40 | import io.netty.channel.nio.NioEventLoopGroup; |
| 41 | +import io.vavr.Tuple2; |
35 | 42 | import java.io.BufferedReader; |
36 | 43 | import java.io.IOException; |
37 | 44 | import java.io.InputStream; |
|
46 | 53 | import java.nio.charset.StandardCharsets; |
47 | 54 | import java.time.Duration; |
48 | 55 | import java.util.*; |
49 | | -import java.util.concurrent.ConcurrentHashMap; |
50 | | -import java.util.concurrent.CountDownLatch; |
51 | | -import java.util.concurrent.ExecutorService; |
52 | | -import java.util.concurrent.Executors; |
53 | | -import java.util.concurrent.TimeUnit; |
| 56 | +import java.util.concurrent.*; |
54 | 57 | import java.util.concurrent.atomic.AtomicLong; |
55 | 58 | import java.util.concurrent.atomic.AtomicReference; |
56 | 59 | import java.util.function.*; |
@@ -81,6 +84,8 @@ public final class TestUtils { |
81 | 84 |
|
82 | 85 | private static final Duration DEFAULT_CONDITION_TIMEOUT = Duration.ofSeconds(10); |
83 | 86 |
|
| 87 | + private static final ConnectionFactory AMQP_CF = new ConnectionFactory(); |
| 88 | + |
84 | 89 | private TestUtils() {} |
85 | 90 |
|
86 | 91 | public static Duration waitAtMost(CallableBooleanSupplier condition) throws Exception { |
@@ -281,11 +286,59 @@ static void declareSuperStreamTopology(Client client, String superStream, int pa |
281 | 286 | static void declareSuperStreamTopology(Client client, String superStream, String... rks) { |
282 | 287 | List<String> partitions = |
283 | 288 | Arrays.stream(rks).map(rk -> superStream + "-" + rk).collect(toList()); |
284 | | - client.createSuperStream(superStream, partitions, Arrays.asList(rks), null); |
| 289 | + if (atLeastVersion("3.13.0", client.brokerVersion())) { |
| 290 | + client.createSuperStream(superStream, partitions, asList(rks), null); |
| 291 | + } else { |
| 292 | + try (Connection connection = connection(); |
| 293 | + Channel ch = connection.createChannel()) { |
| 294 | + ch.exchangeDeclare( |
| 295 | + superStream, |
| 296 | + BuiltinExchangeType.DIRECT, |
| 297 | + true, |
| 298 | + false, |
| 299 | + Collections.singletonMap("x-super-stream", true)); |
| 300 | + List<Tuple2<String, Integer>> bindings = new ArrayList<>(rks.length); |
| 301 | + for (int i = 0; i < rks.length; i++) { |
| 302 | + bindings.add(of(rks[i], i)); |
| 303 | + } |
| 304 | + // shuffle the order to make sure we get in the correct order from the server |
| 305 | + Collections.shuffle(bindings); |
| 306 | + |
| 307 | + for (Tuple2<String, Integer> binding : bindings) { |
| 308 | + String routingKey = binding._1(); |
| 309 | + String partitionName = superStream + "-" + routingKey; |
| 310 | + ch.queueDeclare( |
| 311 | + partitionName, |
| 312 | + true, |
| 313 | + false, |
| 314 | + false, |
| 315 | + Collections.singletonMap("x-queue-type", "stream")); |
| 316 | + ch.queueBind( |
| 317 | + partitionName, |
| 318 | + superStream, |
| 319 | + routingKey, |
| 320 | + Collections.singletonMap("x-stream-partition-order", binding._2())); |
| 321 | + } |
| 322 | + } catch (Exception e) { |
| 323 | + throw new RuntimeException(e); |
| 324 | + } |
| 325 | + } |
285 | 326 | } |
286 | 327 |
|
287 | 328 | static void deleteSuperStreamTopology(Client client, String superStream) { |
288 | | - client.deleteSuperStream(superStream); |
| 329 | + if (atLeastVersion("3.13.0", client.brokerVersion())) { |
| 330 | + client.deleteSuperStream(superStream); |
| 331 | + } else { |
| 332 | + try (Connection connection = connection(); |
| 333 | + Channel ch = connection.createChannel()) { |
| 334 | + ch.exchangeDelete(superStream); |
| 335 | + for (String partition : client.partitions(superStream)) { |
| 336 | + ch.queueDelete(partition); |
| 337 | + } |
| 338 | + } catch (Exception e) { |
| 339 | + throw new RuntimeException(e); |
| 340 | + } |
| 341 | + } |
289 | 342 | } |
290 | 343 |
|
291 | 344 | public static String streamName(TestInfo info) { |
@@ -1034,4 +1087,8 @@ static void repeatIfFailure(RunnableWithException test) throws Exception { |
1034 | 1087 | throw (Exception) lastException; |
1035 | 1088 | } |
1036 | 1089 | } |
| 1090 | + |
| 1091 | + private static Connection connection() throws IOException, TimeoutException { |
| 1092 | + return AMQP_CF.newConnection(); |
| 1093 | + } |
1037 | 1094 | } |
0 commit comments