Skip to content

Commit 937be84

Browse files
committed
Use round robin address selector in cluster test
This makes the test more predictable than with the default address selector, which is random-based.
1 parent e18d77a commit 937be84

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/test/java/com/rabbitmq/client/amqp/impl/ClusterTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import static org.assertj.core.api.Assertions.assertThat;
2323

2424
import com.rabbitmq.client.amqp.*;
25+
import java.util.List;
26+
import java.util.concurrent.atomic.AtomicInteger;
2527
import java.util.function.Consumer;
2628
import org.junit.jupiter.api.*;
2729

@@ -39,6 +41,7 @@ void init(TestInfo info) {
3941
environment =
4042
new AmqpEnvironmentBuilder()
4143
.connectionSettings()
44+
.addressSelector(new RoundRobinAddressSelector())
4245
.uris("amqp://localhost:5672", "amqp://localhost:5673", "amqp://localhost:5674")
4346
.environmentBuilder()
4447
.build();
@@ -74,4 +77,20 @@ AmqpConnection connection(Consumer<ConnectionBuilder> operation) {
7477
operation.accept(builder);
7578
return (AmqpConnection) builder.build();
7679
}
80+
81+
private static class RoundRobinAddressSelector implements AddressSelector {
82+
83+
private final AtomicInteger count = new AtomicInteger();
84+
85+
@Override
86+
public Address select(List<Address> addresses) {
87+
if (addresses.isEmpty()) {
88+
throw new IllegalStateException("There should at least one node to connect to");
89+
} else if (addresses.size() == 1) {
90+
return addresses.get(0);
91+
} else {
92+
return addresses.get(count.getAndIncrement() % addresses.size());
93+
}
94+
}
95+
}
7796
}

0 commit comments

Comments
 (0)