Skip to content

Commit b652b00

Browse files
committed
Add use-nio profile for Maven test suite
The default is to use blocking IO. Fixes #11
1 parent 6d912dd commit b652b00

31 files changed

+152
-143
lines changed

pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,28 @@
410410
</build>
411411
</profile>
412412

413+
<!--
414+
Profile to activate the NIO mode in the test suite:
415+
mvn verify -P use-nio
416+
-->
417+
<profile>
418+
<id>use-nio</id>
419+
<build>
420+
<plugins>
421+
<plugin>
422+
<groupId>org.apache.maven.plugins</groupId>
423+
<artifactId>maven-failsafe-plugin</artifactId>
424+
<version>2.19.1</version>
425+
<configuration>
426+
<systemPropertyVariables>
427+
<use.nio>true</use.nio>
428+
</systemPropertyVariables>
429+
</configuration>
430+
</plugin>
431+
</plugins>
432+
</build>
433+
</profile>
434+
413435
<profile>
414436
<!--
415437
The "release" Maven profile is used to push release artifacts to a

src/main/java/com/rabbitmq/client/ConnectionFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public class ConnectionFactory implements Cloneable {
110110

111111
private MetricsCollector metricsCollector;
112112

113-
private boolean nio = true;
113+
private boolean nio = false;
114114
private FrameHandlerFactory frameHandlerFactory;
115115
private NioParams nioParams = new NioParams();
116116

src/main/java/com/rabbitmq/client/impl/nio/NioHelper.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1+
// Copyright (c) 2007-Present Pivotal Software, Inc. All rights reserved.
2+
//
3+
// This software, the RabbitMQ Java client library, is triple-licensed under the
4+
// Mozilla Public License 1.1 ("MPL"), the GNU General Public License version 2
5+
// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see
6+
// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL,
7+
// please see LICENSE-APACHE2.
8+
//
9+
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
10+
// either express or implied. See the LICENSE file for specific language governing
11+
// rights and limitations of this software.
12+
//
13+
// If you have any questions regarding licensing, please contact us at
14+
15+
116
package com.rabbitmq.client.impl.nio;
217

318
import java.io.IOException;
419
import java.nio.ByteBuffer;
520
import java.nio.channels.ReadableByteChannel;
621
import java.nio.channels.SocketChannel;
722

8-
/**
9-
* Created by acogoluegnes on 04/10/2016.
10-
*/
1123
public class NioHelper {
1224

1325
static int read(ReadableByteChannel channel, ByteBuffer buffer) throws IOException {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class AMQConnectionTest {
5353

5454
@Before public void setUp() throws Exception {
5555
_mockFrameHandler = new MockFrameHandler();
56-
factory = new ConnectionFactory();
56+
factory = TestUtils.connectionFactory();
5757
exceptionHandler = new MyExceptionHandler();
5858
factory.setExceptionHandler(exceptionHandler);
5959
}
@@ -64,7 +64,7 @@ public class AMQConnectionTest {
6464
}
6565

6666
@Test public void negativeTCPConnectionTimeout() {
67-
ConnectionFactory cf = new ConnectionFactory();
67+
ConnectionFactory cf = TestUtils.connectionFactory();
6868
try {
6969
cf.setConnectionTimeout(-10);
7070
fail("expected an exception");
@@ -74,7 +74,7 @@ public class AMQConnectionTest {
7474
}
7575

7676
@Test public void negativeProtocolHandshakeTimeout() {
77-
ConnectionFactory cf = new ConnectionFactory();
77+
ConnectionFactory cf = TestUtils.connectionFactory();
7878
try {
7979
cf.setHandshakeTimeout(-10);
8080
fail("expected an exception");
@@ -84,13 +84,13 @@ public class AMQConnectionTest {
8484
}
8585

8686
@Test public void tcpConnectionTimeoutGreaterThanHandShakeTimeout() {
87-
ConnectionFactory cf = new ConnectionFactory();
87+
ConnectionFactory cf = TestUtils.connectionFactory();
8888
cf.setHandshakeTimeout(3000);
8989
cf.setConnectionTimeout(5000);
9090
}
9191

9292
@Test public void protocolHandshakeTimeoutGreaterThanTCPConnectionTimeout() {
93-
ConnectionFactory cf = new ConnectionFactory();
93+
ConnectionFactory cf = TestUtils.connectionFactory();
9494

9595
cf.setConnectionTimeout(5000);
9696
cf.setHandshakeTimeout(7000);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private void parseSuccess(String uri, String user, String password,
7777
String host, int port, String vhost)
7878
throws URISyntaxException, NoSuchAlgorithmException, KeyManagementException
7979
{
80-
ConnectionFactory cf = new ConnectionFactory();
80+
ConnectionFactory cf = TestUtils.connectionFactory();
8181
cf.setUri(uri);
8282

8383
assertEquals(user, cf.getUsername());
@@ -89,7 +89,7 @@ private void parseSuccess(String uri, String user, String password,
8989

9090
private void parseFail(String uri) {
9191
try {
92-
(new ConnectionFactory()).setUri(uri);
92+
(TestUtils.connectionFactory()).setUri(uri);
9393
fail("URI parse didn't fail: '" + uri + "'");
9494
} catch (Exception e) {
9595
// whoosh!

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class BrokenFramesTest {
4444

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

5050
@After public void tearDown() throws Exception {

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ public class BrokerTestCase {
4444
@Rule
4545
public TestRule watcher = new TestWatcher() {
4646
protected void starting(Description description) {
47-
LOGGER.info("Starting test: {}.{}", description.getTestClass().getSimpleName(), description.getMethodName());
47+
LOGGER.info(
48+
"Starting test: {}.{} (nio? {})",
49+
description.getTestClass().getSimpleName(), description.getMethodName(), TestUtils.USE_NIO
50+
);
4851
}
4952

5053
@Override
@@ -56,20 +59,10 @@ protected void finished(Description description) {
5659
protected ConnectionFactory connectionFactory = newConnectionFactory();
5760

5861
protected ConnectionFactory newConnectionFactory() {
59-
ConnectionFactory connectionFactory = new ConnectionFactory();
60-
if(nio()) {
61-
connectionFactory.useNio();
62-
} else {
63-
connectionFactory.useBlockingIo();
64-
}
65-
62+
ConnectionFactory connectionFactory = TestUtils.connectionFactory();
6663
return connectionFactory;
6764
}
6865

69-
protected boolean nio() {
70-
return true;
71-
}
72-
7366
protected Connection connection;
7467
protected Channel channel;
7568

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public int compare(Channel x, Channel y){
4242
Connection connection;
4343

4444
@Before public void setUp() throws Exception{
45-
connection = new ConnectionFactory().newConnection();
45+
connection = TestUtils.connectionFactory().newConnection();
4646
}
4747

4848
@After public void tearDown() throws Exception{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class CloseInMainLoop extends BrokerTestCase{
4242
private final CountDownLatch closeLatch = new CountDownLatch(1);
4343

4444
private ConnectionFactory specialConnectionFactory() {
45-
ConnectionFactory f = new ConnectionFactory();
45+
ConnectionFactory f = TestUtils.connectionFactory();
4646
f.setExceptionHandler(new DefaultExceptionHandler(){
4747
@Override
4848
public void handleConsumerException(Channel channel,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
public class SharedThreadPoolTest extends BrokerTestCase {
3232
@Test public void willShutDownExecutor() throws IOException, TimeoutException {
33-
ConnectionFactory cf = new ConnectionFactory();
33+
ConnectionFactory cf = TestUtils.connectionFactory();
3434
ExecutorService executor = Executors.newFixedThreadPool(8);
3535
cf.setSharedExecutor(executor);
3636

src/test/java/com/rabbitmq/client/test/ssl/NioTlsBadVerifiedConnection.java renamed to src/test/java/com/rabbitmq/client/test/TestUtils.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,22 @@
1313
// If you have any questions regarding licensing, please contact us at
1414
1515

16-
package com.rabbitmq.client.test.ssl;
16+
package com.rabbitmq.client.test;
1717

18-
/**
19-
*
20-
*/
21-
public class NioTlsBadVerifiedConnection extends BadVerifiedConnection {
18+
import com.rabbitmq.client.ConnectionFactory;
2219

23-
@Override
24-
protected boolean nio() {
25-
return true;
20+
public class TestUtils {
21+
22+
public static final boolean USE_NIO = System.getProperty("use.nio") == null ? false : true;
23+
24+
public static ConnectionFactory connectionFactory() {
25+
ConnectionFactory connectionFactory = new ConnectionFactory();
26+
if(USE_NIO) {
27+
connectionFactory.useNio();
28+
} else {
29+
connectionFactory.useBlockingIo();
30+
}
31+
return connectionFactory;
2632
}
33+
2734
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.net.Socket;
2626
import java.util.concurrent.TimeoutException;
2727

28+
import com.rabbitmq.client.test.TestUtils;
2829
import org.junit.Test;
2930

3031
import com.rabbitmq.client.AMQP;
@@ -40,7 +41,7 @@
4041
*/
4142
public class ConnectionOpen {
4243
@Test public void correctProtocolHeader() throws IOException {
43-
ConnectionFactory factory = new ConnectionFactory();
44+
ConnectionFactory factory = TestUtils.connectionFactory();
4445
SocketFrameHandler fh = new SocketFrameHandler(factory.getSocketFactory().createSocket("localhost", AMQP.PROTOCOL.PORT));
4546
fh.sendHeader();
4647
AMQCommand command = new AMQCommand();
@@ -57,7 +58,7 @@ public class ConnectionOpen {
5758
}
5859

5960
@Test public void crazyProtocolHeader() throws IOException {
60-
ConnectionFactory factory = new ConnectionFactory();
61+
ConnectionFactory factory = TestUtils.connectionFactory();
6162
// keep the frame handler's socket
6263
Socket fhSocket = factory.getSocketFactory().createSocket("localhost", AMQP.PROTOCOL.PORT);
6364
SocketFrameHandler fh = new SocketFrameHandler(fhSocket);
@@ -89,7 +90,7 @@ public class ConnectionOpen {
8990
}
9091

9192
@Test public void frameMaxLessThanFrameMinSize() throws IOException, TimeoutException {
92-
ConnectionFactory factory = new ConnectionFactory();
93+
ConnectionFactory factory = TestUtils.connectionFactory();
9394
factory.setRequestedFrameMax(100);
9495
try {
9596
factory.newConnection();

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.rabbitmq.client.*;
1919
import com.rabbitmq.client.impl.recovery.*;
2020
import com.rabbitmq.client.test.BrokerTestCase;
21+
import com.rabbitmq.client.test.TestUtils;
2122
import com.rabbitmq.tools.Host;
2223
import org.junit.Test;
2324

@@ -822,17 +823,12 @@ private AutorecoveringConnection newRecoveringConnection(String connectionName)
822823
}
823824

824825
private ConnectionFactory buildConnectionFactoryWithRecoveryEnabled(boolean disableTopologyRecovery) {
825-
ConnectionFactory cf = new ConnectionFactory();
826+
ConnectionFactory cf = TestUtils.connectionFactory();
826827
cf.setNetworkRecoveryInterval(RECOVERY_INTERVAL);
827828
cf.setAutomaticRecoveryEnabled(true);
828829
if (disableTopologyRecovery) {
829830
cf.setTopologyRecoveryEnabled(false);
830831
}
831-
if(nio()) {
832-
cf.useNio();
833-
} else {
834-
cf.useBlockingIo();
835-
}
836832
return cf;
837833
}
838834

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.concurrent.TimeUnit;
2424
import java.util.concurrent.TimeoutException;
2525

26+
import com.rabbitmq.client.test.TestUtils;
2627
import org.junit.Test;
2728

2829
import com.rabbitmq.client.AMQP;
@@ -38,7 +39,7 @@
3839

3940
public class ExceptionHandling {
4041
private ConnectionFactory newConnectionFactory(ExceptionHandler eh) {
41-
ConnectionFactory cf = new ConnectionFactory();
42+
ConnectionFactory cf = TestUtils.connectionFactory();
4243
cf.setExceptionHandler(eh);
4344
return cf;
4445
}
@@ -91,7 +92,7 @@ public void handleDelivery(String consumerTag, Envelope envelope,
9192
}
9293

9394
@Test public void nullExceptionHandler() {
94-
ConnectionFactory cf = new ConnectionFactory();
95+
ConnectionFactory cf = TestUtils.connectionFactory();
9596
try {
9697
cf.setExceptionHandler(null);
9798
fail("expected setExceptionHandler to throw");

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Map;
2424
import java.util.concurrent.TimeoutException;
2525

26+
import com.rabbitmq.client.test.TestUtils;
2627
import org.junit.Test;
2728

2829
import com.rabbitmq.client.BuiltinExchangeType;
@@ -87,7 +88,7 @@ public void releaseResources() throws IOException {
8788
}
8889

8990
@Test public void exchangeDeclaredWithEnumerationEquivalentOnRecoverableConnection() throws IOException, TimeoutException, InterruptedException {
90-
ConnectionFactory connectionFactory = new ConnectionFactory();
91+
ConnectionFactory connectionFactory = TestUtils.connectionFactory();
9192
connectionFactory.setAutomaticRecoveryEnabled(true);
9293
connectionFactory.setTopologyRecoveryEnabled(false);
9394
Connection c = connectionFactory.newConnection();

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.concurrent.ExecutorService;
2626
import java.util.concurrent.TimeoutException;
2727

28+
import com.rabbitmq.client.test.TestUtils;
2829
import org.junit.Test;
2930

3031
import com.rabbitmq.client.AMQP;
@@ -81,7 +82,7 @@ public FrameMax() {
8182
* during connection negotiation */
8283
@Test public void rejectLargeFramesDuringConnectionNegotiation()
8384
throws IOException, TimeoutException {
84-
ConnectionFactory cf = new ConnectionFactory();
85+
ConnectionFactory cf = TestUtils.connectionFactory();
8586
cf.getClientProperties().put("too_long", LongStringHelper.asLongString(new byte[AMQP.FRAME_MIN_SIZE]));
8687
try {
8788
cf.newConnection();
@@ -106,6 +107,16 @@ public FrameMax() {
106107
/* ConnectionFactory that uses MyFrameHandler rather than
107108
* SocketFrameHandler. */
108109
private static class MyConnectionFactory extends ConnectionFactory {
110+
111+
public MyConnectionFactory() {
112+
super();
113+
if(TestUtils.USE_NIO) {
114+
useNio();
115+
} else {
116+
useBlockingIo();
117+
}
118+
}
119+
109120
protected FrameHandler createFrameHandler(Socket sock)
110121
throws IOException
111122
{
@@ -154,6 +165,15 @@ public GenerousAMQConnection(ConnectionFactory factory,
154165

155166
private static class GenerousConnectionFactory extends ConnectionFactory {
156167

168+
public GenerousConnectionFactory() {
169+
super();
170+
if(TestUtils.USE_NIO) {
171+
useNio();
172+
} else {
173+
useBlockingIo();
174+
}
175+
}
176+
157177
@Override public Connection newConnection(ExecutorService executor, List<Address> addrs)
158178
throws IOException, TimeoutException {
159179
IOException lastException = null;

0 commit comments

Comments
 (0)