Skip to content

Commit 277a8d2

Browse files
committed
Update to run tests against a containerized broker
(cherry picked from commit 4864e83) Conflicts: RUNNING_TESTS.md
1 parent 9d35159 commit 277a8d2

12 files changed

+201
-64
lines changed

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,67 @@ c.close()
6969
/exit
7070
```
7171

72+
## Building from Source
73+
74+
### Getting the Project and its Dependencies
75+
76+
```
77+
git clone [email protected]:rabbitmq/rabbitmq-java-client.git
78+
cd rabbitmq-java-client
79+
make deps
80+
```
81+
82+
### Building the JAR File
83+
84+
```
85+
./mvnw clean package -Dmaven.test.skip -P '!setup-test-cluster'
86+
```
87+
88+
### Launching Tests with the Broker Running In a Docker Container
89+
90+
Run the broker:
91+
92+
```
93+
docker run -it --rm --name rabbitmq -p 5672:5672 rabbitmq:3.8
94+
```
95+
96+
Launch "essential" tests (takes about 10 minutes):
97+
98+
```
99+
./mvnw verify -P '!setup-test-cluster' \
100+
-Drabbitmqctl.bin=DOCKER:rabbitmq \
101+
-Dit.test=ClientTests,FunctionalTests,ServerTests
102+
```
103+
104+
Launch a single test:
105+
106+
```
107+
./mvnw verify -P '!setup-test-cluster' \
108+
-Drabbitmqctl.bin=DOCKER:rabbitmq \
109+
-Dit.test=DeadLetterExchange
110+
```
111+
112+
### Launching Tests with a Local Broker
113+
114+
The tests can run against a local broker as well. The `rabbitmqctl.bin`
115+
system property must point to the `rabbitmqctl` program:
116+
117+
```
118+
./mvnw verify -P '!setup-test-cluster' \
119+
-Dtest-broker.A.nodename=rabbit@$(hostname) \
120+
-Drabbitmqctl.bin=/path/to/rabbitmqctl \
121+
-Dit.test=ClientTests,FunctionalTests,ServerTests
122+
```
123+
124+
To launch a single test:
125+
126+
```
127+
./mvnw verify -P '!setup-test-cluster' \
128+
-Dtest-broker.A.nodename=rabbit@$(hostname) \
129+
-Drabbitmqctl.bin=/path/to/rabbitmqctl \
130+
-Dit.test=DeadLetterExchange
131+
```
132+
72133
## Contributing
73134

74135
See [Contributing](./CONTRIBUTING.md) and [How to Run Tests](./RUNNING_TESTS.md).

RUNNING_TESTS.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ Tests run: 121, Failures: 0, Errors: 0, Skipped: 0
166166

167167
Test reports can be found in `target/failsafe-reports`.
168168

169+
<<<<<<< HEAD
169170

170171
## Running tests against an externally provided broker or cluster
171172

@@ -183,3 +184,22 @@ mvn verify -P '!setup-test-cluster'
183184

184185
Note that by doing so some tests will fail as they require `rabbitmqctl` to
185186
control the running nodes.
187+
188+
## Running Against a Broker in a Docker Container
189+
190+
Run the broker:
191+
192+
```
193+
docker run -it --rm --name rabbitmq -p 5672:5672 rabbitmq:3.8
194+
```
195+
196+
Launch the tests:
197+
198+
```
199+
./mvnw verify -P '!setup-test-cluster' \
200+
-Drabbitmqctl.bin=DOCKER:rabbitmq \
201+
-Dit.test=ClientTests,FunctionalTests,ServerTests
202+
```
203+
204+
Note the `rabbitmqctl.bin` system property uses the syntax
205+
`DOCKER:{containerId}`.

src/test/java/com/rabbitmq/client/test/AbstractRMQTestSuite.java

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,9 @@ public abstract class AbstractRMQTestSuite {
4343
}
4444

4545
public static boolean requiredProperties() {
46-
/* GNU Make. */
47-
String make = Host.makeCommand();
48-
boolean isGNUMake = false;
49-
if (make != null) {
50-
try {
51-
Process makeProc = Host.executeCommandIgnoringErrors(make + " --version");
52-
String makeVersion = Host.capture(makeProc.getInputStream());
53-
isGNUMake = makeVersion.startsWith("GNU Make");
54-
} catch (IOException e) {}
55-
}
56-
if (!isGNUMake) {
57-
System.err.println(
58-
"GNU Make required; please set \"make.bin\" system property" +
59-
" or \"$MAKE\" environment variable");
60-
return false;
61-
}
62-
63-
/* Path to RabbitMQ. */
64-
String rabbitmq = Host.rabbitmqDir();
65-
if (rabbitmq == null || !new File(rabbitmq).isDirectory()) {
66-
System.err.println(
67-
"RabbitMQ required; please set \"rabbitmq.dir\" system" +
68-
" property");
69-
return false;
70-
}
71-
7246
/* Path to rabbitmqctl. */
7347
String rabbitmqctl = Host.rabbitmqctlCommand();
74-
if (rabbitmqctl == null || !new File(rabbitmqctl).isFile()) {
48+
if (rabbitmqctl == null) {
7549
System.err.println(
7650
"rabbitmqctl required; please set \"rabbitmqctl.bin\" system" +
7751
" property");

src/test/java/com/rabbitmq/client/test/BrokerTestCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ protected void bareRestart()
148148
public void openConnection()
149149
throws IOException, TimeoutException {
150150
if (connection == null) {
151-
connection = connectionFactory.newConnection();
151+
connection = connectionFactory.newConnection(UUID.randomUUID().toString());
152152
}
153153
}
154154

src/test/java/com/rabbitmq/client/test/NoAutoRecoveryWhenTcpWindowIsFullTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.rabbitmq.client.impl.recovery.AutorecoveringConnection;
3030
import org.junit.After;
3131
import org.junit.Before;
32+
import org.junit.ClassRule;
3233
import org.junit.Test;
3334

3435
import java.io.IOException;
@@ -41,6 +42,7 @@
4142
import java.util.concurrent.ExecutorService;
4243
import java.util.concurrent.Executors;
4344
import java.util.concurrent.TimeUnit;
45+
import org.junit.rules.TestRule;
4446

4547
import static org.assertj.core.api.Assertions.assertThat;
4648

@@ -66,6 +68,9 @@
6668
*/
6769
public class NoAutoRecoveryWhenTcpWindowIsFullTest {
6870

71+
@ClassRule
72+
public static TestRule brokerOnDockerTestRule = TestUtils.brokerIsNotRunningOnDocker();
73+
6974
private static final int NUM_MESSAGES_TO_PRODUCE = 50000;
7075
private static final int MESSAGE_PROCESSING_TIME_MS = 3000;
7176
private static final byte[] MESSAGE_CONTENT = ("MESSAGE CONTENT " + NUM_MESSAGES_TO_PRODUCE).getBytes();

src/test/java/com/rabbitmq/client/test/RpcTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public void givenConsumerNotRecoveredCanCreateNewClientOnSameChannelAfterConnect
217217
cf.setNetworkRecoveryInterval(1000);
218218
Connection connection = null;
219219
try {
220-
connection = cf.newConnection();
220+
connection = cf.newConnection(UUID.randomUUID().toString());
221221
Channel channel = connection.createChannel();
222222
RpcClient client = new RpcClient(new RpcClientParams()
223223
.channel(channel).exchange("").routingKey(queue).timeout(1000));
@@ -265,7 +265,7 @@ public void givenConsumerIsRecoveredCanNotCreateNewClientOnSameChannelAfterConne
265265
cf.setNetworkRecoveryInterval(1000);
266266
Connection connection = null;
267267
try {
268-
connection = cf.newConnection();
268+
connection = cf.newConnection(UUID.randomUUID().toString());
269269
Channel channel = connection.createChannel();
270270
RpcClient client = new RpcClient(new RpcClientParams()
271271
.channel(channel).exchange("").routingKey(queue).timeout(1000));

src/test/java/com/rabbitmq/client/test/TestUtils.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ public static TestRule atLeast38() {
114114
return new BrokerVersionTestRule("3.8.0");
115115
}
116116

117+
public static TestRule brokerIsNotRunningOnDocker() {
118+
return new BrokerIsNotOnDocker();
119+
}
120+
117121
public static boolean isVersion37orLater(Connection connection) {
118122
return atLeastVersion("3.7.0", connection);
119123
}
@@ -312,6 +316,27 @@ public void evaluate() throws Throwable {
312316
}
313317
}
314318

319+
private static class BrokerIsNotOnDocker implements TestRule {
320+
321+
322+
@Override
323+
public Statement apply(Statement base, Description description) {
324+
return new Statement() {
325+
@Override
326+
public void evaluate() throws Throwable {
327+
try {
328+
if (Host.isOnDocker()) {
329+
throw new AssumptionViolatedException("Broker is running on Docker");
330+
}
331+
} catch (Exception e) {
332+
throw new AssumptionViolatedException("Could not check whether broker is running on Docker or not", e);
333+
}
334+
base.evaluate();
335+
}
336+
};
337+
}
338+
}
339+
315340
@FunctionalInterface
316341
public interface CallableFunction<T, R> {
317342

src/test/java/com/rabbitmq/client/test/functional/ConnectionRecovery.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class ConnectionRecovery extends BrokerTestCase {
5757

5858
@Test public void namedConnectionRecovery()
5959
throws IOException, InterruptedException, TimeoutException {
60-
String connectionName = "custom name";
60+
String connectionName = "custom-name";
6161
RecoverableConnection c = newRecoveringConnection(connectionName);
6262
try {
6363
assertThat(c.isOpen()).isTrue();
@@ -151,7 +151,7 @@ public String getPassword() {
151151
return password;
152152
}
153153
});
154-
RecoverableConnection c = (RecoverableConnection) cf.newConnection();
154+
RecoverableConnection c = (RecoverableConnection) cf.newConnection(UUID.randomUUID().toString());
155155
try {
156156
assertThat(c.isOpen()).isTrue();
157157
assertThat(usernameRequested.get()).isEqualTo(1);
@@ -802,7 +802,7 @@ public void handleDelivery(String consumerTag,
802802
@Test public void recoveryWithExponentialBackoffDelayHandler() throws Exception {
803803
ConnectionFactory connectionFactory = TestUtils.connectionFactory();
804804
connectionFactory.setRecoveryDelayHandler(new RecoveryDelayHandler.ExponentialBackoffDelayHandler());
805-
Connection testConnection = connectionFactory.newConnection();
805+
Connection testConnection = connectionFactory.newConnection(UUID.randomUUID().toString());
806806
try {
807807
assertThat(testConnection.isOpen()).isTrue();
808808
TestUtils.closeAndWaitForRecovery((RecoverableConnection) testConnection);
@@ -820,7 +820,9 @@ public void handleDelivery(String consumerTag,
820820
assertThat(connectionFactory.getTopologyRecoveryExecutor()).isNull();
821821
connectionFactory.setTopologyRecoveryExecutor(executor);
822822
assertThat(connectionFactory.getTopologyRecoveryExecutor()).isEqualTo(executor);
823-
RecoverableConnection testConnection = (RecoverableConnection) connectionFactory.newConnection();
823+
RecoverableConnection testConnection = (RecoverableConnection) connectionFactory.newConnection(
824+
UUID.randomUUID().toString()
825+
);
824826
try {
825827
final List<Channel> channels = new ArrayList<Channel>();
826828
final List<String> exchanges = new ArrayList<String>();
@@ -975,20 +977,20 @@ protected ConnectionFactory newConnectionFactory() {
975977
private static RecoverableConnection newRecoveringConnection(boolean disableTopologyRecovery)
976978
throws IOException, TimeoutException {
977979
ConnectionFactory cf = buildConnectionFactoryWithRecoveryEnabled(disableTopologyRecovery);
978-
return (AutorecoveringConnection) cf.newConnection();
980+
return (AutorecoveringConnection) cf.newConnection(UUID.randomUUID().toString());
979981
}
980982

981983
private static RecoverableConnection newRecoveringConnection(Address[] addresses)
982984
throws IOException, TimeoutException {
983985
ConnectionFactory cf = buildConnectionFactoryWithRecoveryEnabled(false);
984986
// specifically use the Address[] overload
985-
return (AutorecoveringConnection) cf.newConnection(addresses);
987+
return (AutorecoveringConnection) cf.newConnection(addresses, UUID.randomUUID().toString());
986988
}
987989

988990
private static RecoverableConnection newRecoveringConnection(boolean disableTopologyRecovery, List<Address> addresses)
989991
throws IOException, TimeoutException {
990992
ConnectionFactory cf = buildConnectionFactoryWithRecoveryEnabled(disableTopologyRecovery);
991-
return (AutorecoveringConnection) cf.newConnection(addresses);
993+
return (AutorecoveringConnection) cf.newConnection(addresses, UUID.randomUUID().toString());
992994
}
993995

994996
private static RecoverableConnection newRecoveringConnection(List<Address> addresses)

src/test/java/com/rabbitmq/client/test/functional/Metrics.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.rabbitmq.client.test.BrokerTestCase;
3131
import com.rabbitmq.client.test.TestUtils;
3232
import com.rabbitmq.tools.Host;
33+
import java.util.UUID;
3334
import org.junit.Test;
3435
import org.junit.runner.RunWith;
3536
import org.junit.runners.Parameterized;
@@ -414,7 +415,7 @@ protected void releaseResources() throws IOException {
414415

415416
Connection connection = null;
416417
try {
417-
connection = connectionFactory.newConnection();
418+
connection = connectionFactory.newConnection(UUID.randomUUID().toString());
418419

419420
Collection<?> shutdownHooks = getShutdownHooks(connection);
420421
assertThat(shutdownHooks.size()).isEqualTo(0);
@@ -444,7 +445,7 @@ protected void releaseResources() throws IOException {
444445

445446
Connection connection = null;
446447
try {
447-
connection = connectionFactory.newConnection();
448+
connection = connectionFactory.newConnection(UUID.randomUUID().toString());
448449

449450
Channel channel1 = connection.createChannel();
450451
AtomicInteger ackedMessages = new AtomicInteger(0);

src/test/java/com/rabbitmq/client/test/functional/TopologyRecoveryFiltering.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.rabbitmq.client.impl.recovery.TopologyRecoveryFilter;
3030
import com.rabbitmq.client.test.BrokerTestCase;
3131
import com.rabbitmq.client.test.TestUtils;
32+
import java.util.UUID;
3233
import org.junit.Test;
3334

3435
import java.io.IOException;
@@ -66,7 +67,7 @@ protected ConnectionFactory newConnectionFactory() {
6667
@Override
6768
protected void createResources() throws IOException, TimeoutException {
6869
super.createResources();
69-
c = connectionFactory.newConnection();
70+
c = connectionFactory.newConnection(UUID.randomUUID().toString());
7071
deleteExchanges(exchangesToDelete);
7172
deleteQueues(queuesToDelete);
7273
}

src/test/java/com/rabbitmq/client/test/server/LoopbackUsers.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ public class LoopbackUsers {
4646
}
4747

4848
@Test public void loopback() throws IOException, TimeoutException {
49-
String addr = findRealIPAddress().getHostAddress();
50-
assertGuestFail(addr);
51-
Host.rabbitmqctl("eval 'application:set_env(rabbit, loopback_users, []).'");
52-
assertGuestSucceed(addr);
53-
Host.rabbitmqctl("eval 'application:set_env(rabbit, loopback_users, [<<\"guest\">>]).'");
54-
assertGuestFail(addr);
49+
if (!Host.isOnDocker()) {
50+
String addr = findRealIPAddress().getHostAddress();
51+
assertGuestFail(addr);
52+
Host.rabbitmqctl("eval 'application:set_env(rabbit, loopback_users, []).'");
53+
assertGuestSucceed(addr);
54+
Host.rabbitmqctl("eval 'application:set_env(rabbit, loopback_users, [<<\"guest\">>]).'");
55+
assertGuestFail(addr);
56+
}
5557
}
5658

5759
private void assertGuestSucceed(String addr) throws IOException, TimeoutException {

0 commit comments

Comments
 (0)