Skip to content

Commit 3291568

Browse files
committed
Track connections left open in tests
1 parent 1bd2ea4 commit 3291568

16 files changed

+90
-53
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ public class AMQConnectionTest {
5151
private ConnectionFactory factory;
5252
private MyExceptionHandler exceptionHandler;
5353

54-
@Before public void setUp() throws Exception {
54+
@Before public void setUp() {
5555
_mockFrameHandler = new MockFrameHandler();
5656
factory = TestUtils.connectionFactory();
5757
exceptionHandler = new MyExceptionHandler();
5858
factory.setExceptionHandler(exceptionHandler);
5959
}
6060

61-
@After public void tearDown() throws Exception {
61+
@After public void tearDown() {
6262
factory = null;
6363
_mockFrameHandler = null;
6464
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ public class BrokenFramesTest {
4242
private MyFrameHandler myFrameHandler;
4343
private ConnectionFactory factory;
4444

45-
@Before public void setUp() throws Exception {
45+
@Before public void setUp() {
4646
myFrameHandler = new MyFrameHandler();
4747
factory = TestUtils.connectionFactory();
4848
}
4949

50-
@After public void tearDown() throws Exception {
50+
@After public void tearDown() {
5151
factory = null;
5252
myFrameHandler = null;
5353
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.rabbitmq.client.impl.nio.NioParams;
2121
import com.rabbitmq.tools.Host;
2222
import org.junit.After;
23-
import org.junit.Assume;
2423
import org.junit.Before;
2524
import org.junit.Rule;
2625
import org.junit.rules.TestRule;
@@ -32,7 +31,6 @@
3231
import javax.net.ssl.SSLContext;
3332
import java.io.IOException;
3433
import java.security.NoSuchAlgorithmException;
35-
import java.util.Arrays;
3634
import java.util.Map;
3735
import java.util.UUID;
3836
import java.util.concurrent.TimeoutException;
@@ -184,7 +182,6 @@ public void checkShutdownSignal(int expectedCode, ShutdownSignalException sse) {
184182
Method method = sse.getReason();
185183
channel = null;
186184
if (sse.isHardError()) {
187-
connection = null;
188185
AMQP.Connection.Close closeMethod = (AMQP.Connection.Close) method;
189186
assertEquals(expectedCode, closeMethod.getReplyCode());
190187
} else {

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@
2525

2626
import java.io.IOException;
2727
import java.util.UUID;
28-
import java.util.concurrent.CountDownLatch;
29-
import java.util.concurrent.ExecutorService;
30-
import java.util.concurrent.Executors;
31-
import java.util.concurrent.TimeUnit;
28+
import java.util.concurrent.*;
3229

3330
import static org.junit.Assert.assertTrue;
3431

@@ -39,13 +36,17 @@ public class ChannelAsyncCompletableFutureTest extends BrokerTestCase {
3936
String queue;
4037
String exchange;
4138

42-
@Before public void init() {
39+
@Override
40+
protected void createResources() throws IOException, TimeoutException {
41+
super.createResources();
4342
executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
4443
queue = UUID.randomUUID().toString();
4544
exchange = UUID.randomUUID().toString();
4645
}
4746

48-
@After public void tearDown() throws IOException {
47+
@Override
48+
protected void releaseResources() throws IOException {
49+
super.releaseResources();
4950
executor.shutdownNow();
5051
channel.queueDelete(queue);
5152
channel.exchangeDelete(exchange);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ public class ChannelRpcTimeoutIntegrationTest {
3737
ConnectionFactory factory;
3838

3939
@Before
40-
public void setUp() throws Exception {
40+
public void setUp() {
4141
factory = TestUtils.connectionFactory();
4242
}
4343

4444
@After
45-
public void tearDown() throws Exception {
45+
public void tearDown() {
4646
factory = null;
4747
}
4848

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.rabbitmq.client.impl.CredentialsProvider;
2828
import com.rabbitmq.client.impl.FrameHandler;
2929
import com.rabbitmq.client.impl.FrameHandlerFactory;
30+
import org.junit.AfterClass;
3031
import org.junit.Test;
3132

3233
import java.io.IOException;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
import java.io.IOException;
3030
import java.util.Arrays;
31-
import java.util.List;
3231
import java.util.Queue;
3332
import java.util.concurrent.ArrayBlockingQueue;
3433
import java.util.concurrent.TimeoutException;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
import com.rabbitmq.client.*;
1919
import com.rabbitmq.client.impl.AMQImpl;
20-
import org.junit.After;
21-
import org.junit.Before;
2220
import org.junit.Test;
2321
import org.junit.runner.RunWith;
2422
import org.junit.runners.Parameterized;
@@ -57,8 +55,9 @@ protected ConnectionFactory newConnectionFactory() {
5755
return connectionFactory;
5856
}
5957

60-
@Before
61-
public void init() {
58+
@Override
59+
protected void createResources() throws IOException, TimeoutException {
60+
super.createResources();
6261
queue = UUID.randomUUID().toString();
6362
exchange = UUID.randomUUID().toString();
6463
routingKey = UUID.randomUUID().toString();
@@ -67,8 +66,9 @@ public void init() {
6766
routingKey2 = "e2e-" + UUID.randomUUID().toString();
6867
}
6968

70-
@After
71-
public void tearDown() throws IOException {
69+
@Override
70+
protected void releaseResources() throws IOException {
71+
super.releaseResources();
7272
channel.exchangeDelete(exchange);
7373
channel.exchangeDelete(exchange2);
7474
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ public static void close(Connection connection) {
6767
}
6868
}
6969

70+
public static void abort(Connection connection) {
71+
if (connection != null) {
72+
connection.abort();
73+
}
74+
}
75+
7076
public static SSLContext getSSLContext() throws NoSuchAlgorithmException {
7177
SSLContext c = null;
7278

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@
4343
*/
4444
public class ConnectionOpen {
4545
@Test public void correctProtocolHeader() throws IOException {
46-
ConnectionFactory factory = TestUtils.connectionFactory();
4746
SocketFrameHandler fh = new SocketFrameHandler(SocketFactory.getDefault().createSocket("localhost", AMQP.PROTOCOL.PORT));
4847
fh.sendHeader();
4948
AMQCommand command = new AMQCommand();
5049
while (!command.handleFrame(fh.readFrame())) { }
5150
Method m = command.getMethod();
52-
// System.out.println(m.getClass());
51+
5352
assertTrue("First command must be Connection.start",
5453
m instanceof AMQP.Connection.Start);
5554
AMQP.Connection.Start start = (AMQP.Connection.Start) m;

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.rabbitmq.client.test.functional;
1818

19+
import com.rabbitmq.client.ConnectionFactory;
1920
import com.rabbitmq.client.impl.recovery.AutorecoveringConnection;
2021
import com.rabbitmq.client.test.BrokerTestCase;
2122
import org.junit.Test;
@@ -26,19 +27,19 @@
2627

2728
public class Heartbeat extends BrokerTestCase {
2829

29-
public Heartbeat()
30-
{
31-
super();
32-
connectionFactory.setRequestedHeartbeat(1);
30+
@Override
31+
protected ConnectionFactory newConnectionFactory() {
32+
ConnectionFactory cf = super.newConnectionFactory();
33+
cf.setRequestedHeartbeat(1);
34+
return cf;
3335
}
3436

35-
@Test public void heartbeat()
36-
throws IOException, InterruptedException
37-
{
37+
@Test
38+
public void heartbeat() throws InterruptedException {
3839
assertEquals(1, connection.getHeartbeat());
3940
Thread.sleep(3100);
4041
assertTrue(connection.isOpen());
41-
((AutorecoveringConnection)connection).getDelegate().setHeartbeat(0);
42+
((AutorecoveringConnection) connection).getDelegate().setHeartbeat(0);
4243
assertEquals(0, connection.getHeartbeat());
4344
Thread.sleep(3100);
4445
assertFalse(connection.isOpen());

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import com.rabbitmq.client.AMQP;
1919
import com.rabbitmq.client.ConnectionFactory;
20-
import com.rabbitmq.client.DefaultSocketConfigurator;
2120
import com.rabbitmq.client.SocketConfigurators;
2221
import com.rabbitmq.client.impl.*;
2322
import com.rabbitmq.client.impl.recovery.AutorecoveringConnection;
@@ -35,7 +34,7 @@
3534
public class UnexpectedFrames extends BrokerTestCase {
3635

3736
private interface Confuser {
38-
public Frame confuse(Frame frame) throws IOException;
37+
Frame confuse(Frame frame) throws IOException;
3938
}
4039

4140
private static class ConfusedFrameHandler extends SocketFrameHandler {

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.io.IOException;
2121
import java.util.concurrent.TimeoutException;
2222

23+
import com.rabbitmq.client.Channel;
24+
import com.rabbitmq.client.Connection;
2325
import org.junit.Test;
2426

2527
import com.rabbitmq.client.AMQP;
@@ -48,17 +50,18 @@ public class UserIDHeader extends BrokerTestCase {
4850

4951
@Test public void impersonatedUserId() throws IOException, TimeoutException {
5052
Host.rabbitmqctl("set_user_tags guest administrator impersonator");
51-
connection = null;
52-
channel = null;
53-
setUp();
54-
try {
55-
publish(BAD);
53+
try (Connection c = connectionFactory.newConnection()){
54+
publish(BAD, c.createChannel());
5655
} finally {
5756
Host.rabbitmqctl("set_user_tags guest administrator");
5857
}
5958
}
6059

6160
private void publish(AMQP.BasicProperties properties) throws IOException {
61+
publish(properties, this.channel);
62+
}
63+
64+
private void publish(AMQP.BasicProperties properties, Channel channel) throws IOException {
6265
channel.basicPublish("amq.fanout", "", properties, "".getBytes());
6366
channel.queueDeclare(); // To flush the channel
6467
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ protected void releaseResources() throws IOException {
5050
block();
5151
publish(connection);
5252

53-
assertTrue(latch.await(10, TimeUnit.SECONDS));
53+
try {
54+
assertTrue(latch.await(10, TimeUnit.SECONDS));
55+
} finally {
56+
TestUtils.abort(connection);
57+
}
58+
5459
}
5560

5661
// this test first triggers an alarm, then opens a
@@ -62,7 +67,11 @@ protected void releaseResources() throws IOException {
6267
Connection connection = connection(latch);
6368
publish(connection);
6469

65-
assertTrue(latch.await(10, TimeUnit.SECONDS));
70+
try {
71+
assertTrue(latch.await(10, TimeUnit.SECONDS));
72+
} finally {
73+
TestUtils.abort(connection);
74+
}
6675
}
6776

6877
private Connection connection(final CountDownLatch latch) throws IOException, TimeoutException {

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import com.rabbitmq.tools.Host;
4343

4444
public class ChannelLimitNegotiation extends BrokerTestCase {
45+
4546
class SpecialConnection extends AMQConnection {
4647
private final int channelMax;
4748

@@ -68,8 +69,9 @@ protected int negotiateChannelMax(int requestedChannelMax, int serverMax) {
6869
ConnectionFactory cf = TestUtils.connectionFactory();
6970
cf.setRequestedChannelMax(n);
7071

71-
Connection conn = cf.newConnection();
72-
assertEquals(n, conn.getChannelMax());
72+
try (Connection conn = cf.newConnection()) {
73+
assertEquals(n, conn.getChannelMax());
74+
}
7375
}
7476

7577
@Test public void channelMaxGreaterThanServerValue() throws Exception {
@@ -91,10 +93,11 @@ protected int negotiateChannelMax(int requestedChannelMax, int serverMax) {
9193
@Test public void openingTooManyChannels() throws Exception {
9294
int n = 48;
9395

96+
Connection conn = null;
9497
try {
9598
Host.rabbitmqctl("eval 'application:set_env(rabbit, channel_max, " + n + ").'");
9699
ConnectionFactory cf = TestUtils.connectionFactory();
97-
Connection conn = cf.newConnection();
100+
conn = cf.newConnection();
98101
assertEquals(n, conn.getChannelMax());
99102

100103
for (int i = 1; i <= n; i++) {
@@ -118,6 +121,7 @@ public void shutdownCompleted(ShutdownSignalException cause) {
118121
} catch (IOException e) {
119122
checkShutdownSignal(530, e);
120123
} finally {
124+
TestUtils.abort(conn);
121125
Host.rabbitmqctl("eval 'application:set_env(rabbit, channel_max, 0).'");
122126
}
123127
}

0 commit comments

Comments
 (0)