Skip to content

Commit d74b125

Browse files
committed
Use default ThreadFactory for NIO in test suite
The ExecutorService wasn't closed properly in the base test class, there were thus many threads in park state when running the test suite. This could lead to resource exhaustion, especially on MacOS. Fixes #11
1 parent 22bf675 commit d74b125

File tree

4 files changed

+14
-31
lines changed

4 files changed

+14
-31
lines changed

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

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.rabbitmq.client.test;
1818

1919
import com.rabbitmq.client.*;
20-
import com.rabbitmq.client.impl.nio.NioParams;
2120
import com.rabbitmq.tools.Host;
2221
import org.junit.After;
2322
import org.junit.Before;
@@ -34,8 +33,6 @@
3433
import java.util.Arrays;
3534
import java.util.Map;
3635
import java.util.UUID;
37-
import java.util.concurrent.ExecutorService;
38-
import java.util.concurrent.Executors;
3936
import java.util.concurrent.TimeoutException;
4037

4138
import static org.junit.Assert.*;
@@ -58,17 +55,12 @@ protected void finished(Description description) {
5855

5956
protected ConnectionFactory connectionFactory = newConnectionFactory();
6057

61-
protected ExecutorService nioExecutor = null;
62-
6358
protected ConnectionFactory newConnectionFactory() {
6459
ConnectionFactory connectionFactory = new ConnectionFactory();
6560
if(nio()) {
6661
connectionFactory.useNio();
67-
this.nioExecutor = Executors.newFixedThreadPool(5);
68-
connectionFactory.setNioParams(new NioParams().setNioExecutor(this.nioExecutor));
6962
} else {
7063
connectionFactory.useBlockingIo();
71-
this.nioExecutor = null;
7264
}
7365

7466
return connectionFactory;
@@ -91,24 +83,14 @@ protected boolean nio() {
9183

9284
@After public void tearDown()
9385
throws IOException, TimeoutException {
94-
try {
95-
closeChannel();
96-
closeConnection();
97-
98-
openConnection();
99-
openChannel();
100-
releaseResources();
101-
closeChannel();
102-
closeConnection();
103-
} finally {
104-
if(nio()) {
105-
if(this.nioExecutor != null) {
106-
this.nioExecutor.shutdownNow();
107-
}
108-
}
109-
}
110-
86+
closeChannel();
87+
closeConnection();
11188

89+
openConnection();
90+
openChannel();
91+
releaseResources();
92+
closeChannel();
93+
closeConnection();
11294
}
11395

11496
/**

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public void twoConnectionsWithNioExecutor() throws IOException, TimeoutException
5555
ExecutorService nioExecutor = Executors.newFixedThreadPool(5);
5656
ConnectionFactory connectionFactory = new ConnectionFactory();
5757
connectionFactory.useNio();
58-
connectionFactory.setNioParams(new NioParams().setNioExecutor(nioExecutor));
5958
Connection connection1 = null;
6059
Connection connection2 = null;
6160
try {

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package com.rabbitmq.client.test.functional;
1717

1818
import com.rabbitmq.client.*;
19-
import com.rabbitmq.client.impl.nio.NioParams;
2019
import com.rabbitmq.client.impl.recovery.*;
2120
import com.rabbitmq.client.test.BrokerTestCase;
2221
import com.rabbitmq.tools.Host;
@@ -831,7 +830,6 @@ private ConnectionFactory buildConnectionFactoryWithRecoveryEnabled(boolean disa
831830
}
832831
if(nio()) {
833832
cf.useNio();
834-
cf.setNioParams(new NioParams().setNioExecutor(this.nioExecutor));
835833
} else {
836834
cf.useBlockingIo();
837835
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package com.rabbitmq.client.test.functional;
1717

1818
import com.rabbitmq.client.*;
19-
import com.rabbitmq.client.impl.nio.NioParams;
2019
import com.rabbitmq.client.impl.StandardMetricsCollector;
2120
import com.rabbitmq.client.impl.recovery.AutorecoveringConnection;
2221
import com.rabbitmq.client.test.BrokerTestCase;
@@ -268,6 +267,7 @@ private void doMultiThreadedMetrics(ConnectionFactory connectionFactory) throws
268267

269268
// create connections
270269
Connection [] connections = new Connection[nbConnections];
270+
ExecutorService executorService = Executors.newFixedThreadPool(nbTasks);
271271
try {
272272
Channel [] channels = new Channel[nbChannels];
273273
for(int i = 0; i < nbConnections; i++) {
@@ -284,7 +284,7 @@ private void doMultiThreadedMetrics(ConnectionFactory connectionFactory) throws
284284
sendMessage(channels[random.nextInt(nbChannels)]);
285285
}
286286

287-
ExecutorService executorService = Executors.newFixedThreadPool(nbTasks);
287+
288288
List<Callable<Void>> tasks = new ArrayList<Callable<Void>>();
289289
for(int i = 0; i < nbTasks; i++) {
290290
Channel channelForConsuming = channels[random.nextInt(nbChannels)];
@@ -311,6 +311,8 @@ private void doMultiThreadedMetrics(ConnectionFactory connectionFactory) throws
311311
sendMessage(channels[random.nextInt(nbChannels)]);
312312
}
313313

314+
executorService.shutdownNow();
315+
314316
executorService = Executors.newFixedThreadPool(nbTasks);
315317
tasks = new ArrayList<Callable<Void>>();
316318
for(int i = 0; i < nbTasks; i++) {
@@ -338,6 +340,8 @@ private void doMultiThreadedMetrics(ConnectionFactory connectionFactory) throws
338340
sendMessage(channels[random.nextInt(nbChannels)]);
339341
}
340342

343+
executorService.shutdownNow();
344+
341345
executorService = Executors.newFixedThreadPool(nbTasks);
342346
tasks = new ArrayList<Callable<Void>>();
343347
for(int i = 0; i < nbTasks; i++) {
@@ -356,6 +360,7 @@ private void doMultiThreadedMetrics(ConnectionFactory connectionFactory) throws
356360
for (Connection connection : connections) {
357361
safeClose(connection);
358362
}
363+
executorService.shutdownNow();
359364
}
360365

361366
}
@@ -427,7 +432,6 @@ private ConnectionFactory createConnectionFactory() {
427432
ConnectionFactory connectionFactory = new ConnectionFactory();
428433
if(nio()) {
429434
connectionFactory.useNio();
430-
connectionFactory.setNioParams(new NioParams().setNioExecutor(this.nioExecutor));
431435
} else {
432436
connectionFactory.useBlockingIo();
433437
}

0 commit comments

Comments
 (0)