Elffuss BraidHub

kikocis/java-driver public

Update tests that close many Cluster instances to close them quickly.

By default NettyOptions#onClusterClose will shutdown an event loop group
gracefully with a default 2 second quiet period.  Updated these tests to
use a custom NettyOptions that shuts down event loop group with a 0
second quiet period.
Changesetae7b4cb11d32b93e8ecbb1134b433fd3aad4b9d798cc488b9d3e526f034623bd
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-10-30 02:37 -05:00
Committerolim7t <omichallat+github@gmail.com> · 2015-10-30 16:31 +01:00
Parents97b6be87ffc6

Browse the files at this changeset

4 files changed

modified driver-core/src/test/java/com/datastax/driver/core/ControlConnectionTest.java +2 −0
@@ -30,6 +30,7 @@
3030
3131 import static com.datastax.driver.core.Assertions.assertThat;
3232 import static com.datastax.driver.core.TestUtils.nonDebouncingQueryOptions;
33+import static com.datastax.driver.core.TestUtils.nonQuietClusterCloseOptions;
3334
3435 import com.datastax.driver.core.policies.DelegatingLoadBalancingPolicy;
3536 import com.datastax.driver.core.policies.LoadBalancingPolicy;
@@ -158,6 +159,7 @@
158159 for(int i = 0; i < iterations; i++) {
159160 Cluster cluster = Cluster.builder()
160161 .addContactPoints(scassandras.addresses())
162+ .withNettyOptions(nonQuietClusterCloseOptions)
161163 .build();
162164
163165 try {
modified driver-core/src/test/java/com/datastax/driver/core/SessionTest.java +6 −3
@@ -33,7 +33,7 @@
3333 import static org.testng.Assert.assertTrue;
3434 import static org.testng.Assert.fail;
3535
36-
36+import static com.datastax.driver.core.TestUtils.nonQuietClusterCloseOptions;
3737
3838 /**
3939 * Simple test of the Sessions methods against a one node cluster.
@@ -206,7 +206,7 @@
206206 // ensure sessions.size() returns to 0 with only 1 active connection
207207 session.close();
208208 assertEquals(cluster.manager.sessions.size(), 0);
209- assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1);
209+ assertEquals((int)cluster.getMetrics().getOpenConnections().getValue(), 1);
210210 assertEquals(channelMonitor.openChannels(contactPoints).size(), 1);
211211
212212 // give the driver time to close sessions
@@ -251,7 +251,10 @@
251251
252252 // Use our own cluster and session (not the ones provided by the parent class) because we want an uninitialized cluster
253253 // (note the use of newSession below)
254- final Cluster cluster = Cluster.builder().addContactPointsWithPorts(Collections.singletonList(hostAddress)).build();
254+ final Cluster cluster = Cluster.builder()
255+ .addContactPointsWithPorts(Collections.singletonList(hostAddress))
256+ .withNettyOptions(nonQuietClusterCloseOptions)
257+ .build();
255258 final Session session = cluster.newSession();
256259
257260 // Spawn two threads to simulate the race
modified driver-core/src/test/java/com/datastax/driver/core/TestUtils.java +14 −0
@@ -23,8 +23,10 @@
2323 import java.net.ServerSocket;
2424 import java.nio.ByteBuffer;
2525 import java.util.*;
26+import java.util.concurrent.TimeUnit;
2627
2728 import com.google.common.util.concurrent.Futures;
29+import io.netty.channel.EventLoopGroup;
2830 import org.scassandra.Scassandra;
2931 import org.scassandra.ScassandraFactory;
3032 import org.slf4j.Logger;
@@ -470,4 +472,16 @@
470472 .setRefreshNodeListIntervalMillis(0)
471473 .setRefreshSchemaIntervalMillis(0);
472474 }
475+
476+ /**
477+ * A custom {@link NettyOptions} that shuts down the {@link EventLoopGroup} after
478+ * no quiet time. This is useful for tests that consistently close clusters as
479+ * otherwise there is a 2 second delay (from JAVA-914).
480+ */
481+ public static NettyOptions nonQuietClusterCloseOptions = new NettyOptions() {
482+ @Override
483+ public void onClusterClose(EventLoopGroup eventLoopGroup) {
484+ eventLoopGroup.shutdownGracefully(0, 15, TimeUnit.SECONDS).syncUninterruptibly();
485+ }
486+ };
473487 }
modified driver-core/src/test/java/com/datastax/driver/core/utils/SocketChannelMonitor.java +7 −0
@@ -22,6 +22,7 @@
2222 import com.google.common.collect.Lists;
2323 import com.google.common.collect.MapMaker;
2424 import com.google.common.util.concurrent.ThreadFactoryBuilder;
25+import io.netty.channel.EventLoopGroup;
2526 import io.netty.channel.socket.SocketChannel;
2627 import org.slf4j.Logger;
2728 import org.slf4j.LoggerFactory;
@@ -53,10 +54,16 @@
5354 private final AtomicLong channelsCreated = new AtomicLong(0);
5455
5556 private final NettyOptions nettyOptions = new NettyOptions() {
57+ @Override
5658 public void afterChannelInitialized(SocketChannel channel) throws Exception {
5759 channels.add(channel);
5860 channelsCreated.incrementAndGet();
5961 }
62+
63+ @Override
64+ public void onClusterClose(EventLoopGroup eventLoopGroup) {
65+ eventLoopGroup.shutdownGracefully(0, 15, TimeUnit.SECONDS).syncUninterruptibly();
66+ }
6067 };
6168
6269 @Override