Skip to content

Commit ca3a42a

Browse files
author
Ennio Kerber
committed
test(ConnectionFactory): Update several existing tests to use the new fluent API to configure ConnectionFactory instances
1 parent 8ea7254 commit ca3a42a

File tree

3 files changed

+63
-74
lines changed

3 files changed

+63
-74
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import javax.net.SocketFactory;
2727
import java.io.IOException;
28+
import java.util.HashMap;
2829
import java.util.List;
2930
import java.util.Map;
3031
import java.util.Queue;
@@ -203,7 +204,7 @@ public TestConfig(int value, Consumer<Integer> call, boolean expectException) {
203204
@Test
204205
public void shouldBeConfigurableUsingFluentAPI() throws Exception {
205206
/* GIVEN */
206-
Map<String, Object> clientProperties = Map.of();
207+
Map<String, Object> clientProperties = new HashMap<>();
207208
SaslConfig saslConfig = mock(SaslConfig.class);
208209
ConnectionFactory connectionFactory = new ConnectionFactory();
209210
SocketFactory socketFactory = mock(SocketFactory.class);

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

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public class JavaNioTest {
3030

3131
@BeforeEach
3232
public void init() throws Exception {
33-
ConnectionFactory connectionFactory = new ConnectionFactory();
34-
connectionFactory.useNio();
33+
ConnectionFactory connectionFactory = new ConnectionFactory()
34+
.useNio();
3535
testConnection = connectionFactory.newConnection();
3636
}
3737

@@ -46,8 +46,8 @@ public void tearDown() throws Exception {
4646
@Test
4747
public void connection() throws Exception {
4848
CountDownLatch latch = new CountDownLatch(1);
49-
ConnectionFactory connectionFactory = new ConnectionFactory();
50-
connectionFactory.useNio();
49+
ConnectionFactory connectionFactory = new ConnectionFactory()
50+
.useNio();
5151
Connection connection = null;
5252
try {
5353
connection = basicGetBasicConsume(connectionFactory, "nio.queue", latch);
@@ -61,9 +61,9 @@ public void connection() throws Exception {
6161
@Test
6262
public void twoConnections() throws IOException, TimeoutException, InterruptedException {
6363
CountDownLatch latch = new CountDownLatch(2);
64-
ConnectionFactory connectionFactory = new ConnectionFactory();
65-
connectionFactory.useNio();
66-
connectionFactory.setNioParams(new NioParams().setNbIoThreads(4));
64+
ConnectionFactory connectionFactory = new ConnectionFactory()
65+
.useNio()
66+
.setNioParams(new NioParams().setNbIoThreads(4));
6767
Connection connection1 = null;
6868
Connection connection2 = null;
6969
try {
@@ -82,8 +82,8 @@ public void twoConnections() throws IOException, TimeoutException, InterruptedEx
8282
public void twoConnectionsWithNioExecutor() throws IOException, TimeoutException, InterruptedException {
8383
CountDownLatch latch = new CountDownLatch(2);
8484
ExecutorService nioExecutor = Executors.newFixedThreadPool(5);
85-
ConnectionFactory connectionFactory = new ConnectionFactory();
86-
connectionFactory.useNio();
85+
ConnectionFactory connectionFactory = new ConnectionFactory()
86+
.useNio();
8787
Connection connection1 = null;
8888
Connection connection2 = null;
8989
try {
@@ -101,8 +101,8 @@ public void twoConnectionsWithNioExecutor() throws IOException, TimeoutException
101101

102102
@Test
103103
public void shutdownListenerCalled() throws IOException, TimeoutException, InterruptedException {
104-
ConnectionFactory connectionFactory = new ConnectionFactory();
105-
connectionFactory.useNio();
104+
ConnectionFactory connectionFactory = new ConnectionFactory()
105+
.useNio();
106106
Connection connection = connectionFactory.newConnection();
107107
try {
108108
final CountDownLatch latch = new CountDownLatch(1);
@@ -122,8 +122,8 @@ public void shutdownCompleted(ShutdownSignalException cause) {
122122

123123
@Test
124124
public void nioLoopCleaning() throws Exception {
125-
ConnectionFactory connectionFactory = new ConnectionFactory();
126-
connectionFactory.useNio();
125+
ConnectionFactory connectionFactory = new ConnectionFactory()
126+
.useNio();
127127
for (int i = 0; i < 10; i++) {
128128
Connection connection = connectionFactory.newConnection();
129129
connection.abort();
@@ -139,20 +139,20 @@ public void messageSize() throws Exception {
139139

140140
@Test
141141
public void byteBufferFactory() throws Exception {
142-
ConnectionFactory cf = new ConnectionFactory();
143-
cf.useNio();
142+
ConnectionFactory connectionFactory = new ConnectionFactory()
143+
.useNio();
144144
int baseCapacity = 32768;
145145
NioParams nioParams = new NioParams();
146146
nioParams.setReadByteBufferSize(baseCapacity / 2);
147147
nioParams.setWriteByteBufferSize(baseCapacity / 4);
148148
List<ByteBuffer> byteBuffers = new CopyOnWriteArrayList<>();
149-
cf.setNioParams(nioParams.setByteBufferFactory(new DefaultByteBufferFactory(capacity -> {
149+
connectionFactory.setNioParams(nioParams.setByteBufferFactory(new DefaultByteBufferFactory(capacity -> {
150150
ByteBuffer bb = ByteBuffer.allocate(capacity);
151151
byteBuffers.add(bb);
152152
return bb;
153153
})));
154154

155-
try (Connection c = cf.newConnection()) {
155+
try (Connection c = connectionFactory.newConnection()) {
156156
sendAndVerifyMessage(c, 100);
157157
}
158158

@@ -165,27 +165,27 @@ public void byteBufferFactory() throws Exception {
165165

166166
@Test
167167
public void directByteBuffers() throws Exception {
168-
ConnectionFactory cf = new ConnectionFactory();
169-
cf.useNio();
170-
cf.setNioParams(new NioParams().setByteBufferFactory(new DefaultByteBufferFactory(capacity -> ByteBuffer.allocateDirect(capacity))));
171-
try (Connection c = cf.newConnection()) {
168+
ConnectionFactory connectionFactory = new ConnectionFactory()
169+
.useNio()
170+
.setNioParams(new NioParams().setByteBufferFactory(new DefaultByteBufferFactory(capacity -> ByteBuffer.allocateDirect(capacity))));
171+
try (Connection c = connectionFactory.newConnection()) {
172172
sendAndVerifyMessage(c, 100);
173173
}
174174
}
175175

176176
@Test
177177
public void customWriteQueue() throws Exception {
178-
ConnectionFactory cf = new ConnectionFactory();
179-
cf.useNio();
180178
AtomicInteger count = new AtomicInteger(0);
181-
cf.setNioParams(new NioParams().setWriteQueueFactory(ctx -> {
182-
count.incrementAndGet();
183-
return new BlockingQueueNioQueue(
184-
new LinkedBlockingQueue<>(ctx.getNioParams().getWriteQueueCapacity()),
185-
ctx.getNioParams().getWriteEnqueuingTimeoutInMs()
186-
);
187-
}));
188-
try (Connection c = cf.newConnection()) {
179+
ConnectionFactory connectionFactory = new ConnectionFactory()
180+
.useNio()
181+
.setNioParams(new NioParams().setWriteQueueFactory(ctx -> {
182+
count.incrementAndGet();
183+
return new BlockingQueueNioQueue(
184+
new LinkedBlockingQueue<>(ctx.getNioParams().getWriteQueueCapacity()),
185+
ctx.getNioParams().getWriteEnqueuingTimeoutInMs()
186+
);
187+
}));
188+
try (Connection c = connectionFactory.newConnection()) {
189189
sendAndVerifyMessage(c, 100);
190190
}
191191
assertEquals(1, count.get());

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

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,22 @@
4040
public class SslContextFactoryTest {
4141

4242
@Test public void setSslContextFactory() throws Exception {
43-
doTestSetSslContextFactory(() -> {
44-
ConnectionFactory connectionFactory = new ConnectionFactory();
45-
connectionFactory.useBlockingIo();
46-
connectionFactory.setAutomaticRecoveryEnabled(true);
47-
return connectionFactory;
48-
});
49-
doTestSetSslContextFactory(() -> {
50-
ConnectionFactory connectionFactory = new ConnectionFactory();
51-
connectionFactory.useNio();
52-
connectionFactory.setAutomaticRecoveryEnabled(true);
53-
return connectionFactory;
54-
});
55-
doTestSetSslContextFactory(() -> {
56-
ConnectionFactory connectionFactory = new ConnectionFactory();
57-
connectionFactory.useBlockingIo();
58-
connectionFactory.setAutomaticRecoveryEnabled(false);
59-
return connectionFactory;
60-
});
61-
doTestSetSslContextFactory(() -> {
62-
ConnectionFactory connectionFactory = new ConnectionFactory();
63-
connectionFactory.useNio();
64-
connectionFactory.setAutomaticRecoveryEnabled(false);
65-
return connectionFactory;
66-
});
43+
doTestSetSslContextFactory(() -> new ConnectionFactory()
44+
.useBlockingIo()
45+
.setAutomaticRecoveryEnabled(true)
46+
);
47+
doTestSetSslContextFactory(() -> new ConnectionFactory()
48+
.useNio()
49+
.setAutomaticRecoveryEnabled(true)
50+
);
51+
doTestSetSslContextFactory(() -> new ConnectionFactory()
52+
.useBlockingIo()
53+
.setAutomaticRecoveryEnabled(false)
54+
);
55+
doTestSetSslContextFactory(() -> new ConnectionFactory()
56+
.useNio()
57+
.setAutomaticRecoveryEnabled(false)
58+
);
6759
}
6860

6961
private void doTestSetSslContextFactory(Supplier<ConnectionFactory> supplier) throws Exception {
@@ -82,31 +74,27 @@ private void doTestSetSslContextFactory(Supplier<ConnectionFactory> supplier) th
8274
}
8375

8476
@Test public void socketFactoryTakesPrecedenceOverSslContextFactoryWithBlockingIo() throws Exception {
85-
doTestSocketFactoryTakesPrecedenceOverSslContextFactoryWithBlockingIo(() -> {
86-
ConnectionFactory connectionFactory = new ConnectionFactory();
87-
connectionFactory.useBlockingIo();
88-
connectionFactory.setAutomaticRecoveryEnabled(true);
89-
return connectionFactory;
90-
});
91-
doTestSocketFactoryTakesPrecedenceOverSslContextFactoryWithBlockingIo(() -> {
92-
ConnectionFactory connectionFactory = new ConnectionFactory();
93-
connectionFactory.useBlockingIo();
94-
connectionFactory.setAutomaticRecoveryEnabled(false);
95-
return connectionFactory;
96-
});
77+
doTestSocketFactoryTakesPrecedenceOverSslContextFactoryWithBlockingIo(() -> new ConnectionFactory()
78+
.useBlockingIo()
79+
.setAutomaticRecoveryEnabled(true)
80+
);
81+
doTestSocketFactoryTakesPrecedenceOverSslContextFactoryWithBlockingIo(() -> new ConnectionFactory()
82+
.useBlockingIo()
83+
.setAutomaticRecoveryEnabled(false)
84+
);
9785
}
9886

9987
private void doTestSocketFactoryTakesPrecedenceOverSslContextFactoryWithBlockingIo(
10088
Supplier<ConnectionFactory> supplier
10189
) throws Exception {
102-
ConnectionFactory connectionFactory = supplier.get();
103-
connectionFactory.useBlockingIo();
10490
SslContextFactory sslContextFactory = sslContextFactory();
105-
connectionFactory.setSslContextFactory(sslContextFactory);
106-
10791
SSLContext contextAcceptAll = sslContextFactory.create("connection01");
108-
connectionFactory.setSocketFactory(contextAcceptAll.getSocketFactory());
109-
92+
ConnectionFactory connectionFactory = supplier.get();
93+
connectionFactory
94+
.useBlockingIo()
95+
.setSslContextFactory(sslContextFactory)
96+
.setSocketFactory(contextAcceptAll.getSocketFactory());
97+
11098
Connection connection = connectionFactory.newConnection("connection01");
11199
TestUtils.close(connection);
112100
connection = connectionFactory.newConnection("connection02");

0 commit comments

Comments
 (0)