Elffuss BraidHub

kikocis/java-driver public

Refactored PerClassSingleNodeCluster classes who inherit it (JAVA-659)
Changeset253ab5bc1334a4d6244d40a0d27a9c5cc5499b089f7f5aac5aa2ebd56bcbe11a
AuthorKevin Gallardo <kevingallardo@Kevins-MacBook-Pro.local> · 2015-03-05 09:19 UTC
CommitterKevin Gallardo <kevin.gallardo@datastax.com> · 2015-03-19 11:07 UTC
Parentsa521417641f3

Browse the files at this changeset

9 files changed

modified driver-core/src/test/java/com/datastax/driver/core/CCMBridge.java +133 −72
@@ -15,31 +15,34 @@
1515 */
1616 package com.datastax.driver.core;
1717
18-import com.datastax.driver.core.Cluster.Builder;
19-import com.datastax.driver.core.exceptions.AlreadyExistsException;
20-import com.datastax.driver.core.exceptions.DriverException;
21-import com.datastax.driver.core.exceptions.NoHostAvailableException;
22-
23-import com.google.common.base.Joiner;
24-import com.google.common.collect.ImmutableMap;
25-import com.google.common.io.*;
26-import org.slf4j.Logger;
27-import org.slf4j.LoggerFactory;
28-import org.testng.annotations.AfterClass;
29-import org.testng.annotations.BeforeClass;
30-
3118 import java.io.*;
3219 import java.net.InetAddress;
3320 import java.net.InetSocketAddress;
3421 import java.net.Socket;
3522 import java.net.UnknownHostException;
3623 import java.util.Collection;
24+import java.util.Collections;
3725 import java.util.Map;
3826 import java.util.concurrent.TimeUnit;
27+import java.util.concurrent.atomic.AtomicLong;
3928
40-import static org.testng.Assert.fail;
29+import com.google.common.base.Joiner;
30+import com.google.common.collect.ImmutableMap;
31+import com.google.common.io.ByteStreams;
32+import com.google.common.io.Closer;
33+import com.google.common.io.Files;
34+import org.slf4j.Logger;
35+import org.slf4j.LoggerFactory;
36+import org.testng.annotations.AfterClass;
37+import org.testng.annotations.BeforeClass;
4138
4239 import static com.google.common.base.Preconditions.checkArgument;
40+import static org.testng.Assert.fail;
41+
42+import com.datastax.driver.core.Cluster.Builder;
43+import com.datastax.driver.core.exceptions.AlreadyExistsException;
44+import com.datastax.driver.core.exceptions.DriverException;
45+import com.datastax.driver.core.exceptions.NoHostAvailableException;
4346
4447 import static com.datastax.driver.core.TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT;
4548 import static com.datastax.driver.core.TestUtils.SIMPLE_KEYSPACE;
@@ -70,6 +73,7 @@
7073
7174 static final File CASSANDRA_DIR;
7275 static final String CASSANDRA_VERSION;
76+
7377 static {
7478 String version = System.getProperty("cassandra.version");
7579 if (version.matches(CASSANDRA_VERSION_REGEXP)) {
@@ -90,8 +94,7 @@
9094 private final Runtime runtime = Runtime.getRuntime();
9195 private final File ccmDir;
9296
93- private CCMBridge()
94- {
97+ private CCMBridge() {
9598 this.ccmDir = Files.createTempDir();
9699 }
97100
@@ -118,7 +121,7 @@
118121 OutputStream trustStoreOs = new FileOutputStream(f);
119122 closer.register(trustStoreOs);
120123 ByteStreams.copy(trustStoreIs, trustStoreOs);
121- } catch(IOException e) {
124+ } catch (IOException e) {
122125 logger.warn("Failure to write keystore, SSL-enabled servers may fail to start.", e);
123126 } finally {
124127 try {
@@ -141,7 +144,7 @@
141144
142145 public static CCMBridge create(String name, int nbNodes, String... options) {
143146 checkArgument(!"current".equals(name.toLowerCase()),
144- "cluster can't be called \"current\"");
147+ "cluster can't be called \"current\"");
145148 CCMBridge bridge = new CCMBridge();
146149 bridge.execute("ccm create %s -n %d -s -i %s -b %s " + Joiner.on(" ").join(options), name, nbNodes, IP_PREFIX, CASSANDRA_VERSION);
147150 return bridge;
@@ -157,7 +160,7 @@
157160
158161 public static CCMBridge create(String name, int nbNodesDC1, int nbNodesDC2) {
159162 checkArgument(!"current".equals(name.toLowerCase()),
160- "cluster can't be called \"current\"");
163+ "cluster can't be called \"current\"");
161164 CCMBridge bridge = new CCMBridge();
162165 bridge.execute("ccm create %s -n %d:%d -s -i %s -b %s", name, nbNodesDC1, nbNodesDC2, IP_PREFIX, CASSANDRA_VERSION);
163166 return bridge;
@@ -198,6 +201,11 @@
198201 execute("ccm node%d stop", n);
199202 }
200203
204+ public void stop(String clusterName) {
205+ logger.info("Stopping Cluster : "+clusterName);
206+ execute("ccm stop "+clusterName);
207+ }
208+
201209 public void forceStop(int n) {
202210 logger.info("Force stopping: " + IP_PREFIX + n);
203211 execute("ccm node%d stop --not-gently", n);
@@ -208,6 +216,11 @@
208216 execute("ccm remove");
209217 }
210218
219+ public void remove(String clusterName) {
220+ stop(clusterName);
221+ execute("ccm remove " + clusterName);
222+ }
223+
211224 public void remove(int n) {
212225 logger.info("Removing: " + IP_PREFIX + n);
213226 execute("ccm node%d remove", n);
@@ -227,9 +240,19 @@
227240
228241 public void bootstrapNode(int n, String dc) {
229242 if (dc == null)
230- execute("ccm add node%d -i %s%d -j %d -r %d -b -s", n, IP_PREFIX, n, 7000 + 100*n, 8000 + 100*n);
243+ execute("ccm add node%d -i %s%d -j %d -r %d -b -s", n, IP_PREFIX, n, 7000 + 100 * n, 8000 + 100 * n);
231244 else
232- execute("ccm add node%d -i %s%d -j %d -b -d %s -s", n, IP_PREFIX, n, 7000 + 100*n, dc);
245+ execute("ccm add node%d -i %s%d -j %d -b -d %s -s", n, IP_PREFIX, n, 7000 + 100 * n, dc);
246+ execute("ccm node%d start --wait-other-notice --wait-for-binary-proto", n);
247+ }
248+
249+ public void bootstrapNodeWithPorts(int n, int thriftPort, int storagePort, int binaryPort, int jmxPort, int remoteDebugPort) {
250+ String thriftItf = IP_PREFIX + n + ":" + thriftPort;
251+ String storageItf = IP_PREFIX + n + ":" + storagePort;
252+ String binaryItf = IP_PREFIX + n + ":" + binaryPort;
253+ String remoteLogItf = IP_PREFIX + n + ":" + remoteDebugPort;
254+ execute("ccm add node%d -i %s%d -b -t %s -l %s --binary-itf %s -j %d -r %s -s",
255+ n, IP_PREFIX, n, thriftItf, storageItf, binaryItf, jmxPort, remoteLogItf);
233256 execute("ccm node%d start --wait-other-notice --wait-for-binary-proto", n);
234257 }
235258
@@ -243,7 +266,7 @@
243266
244267 public void updateConfig(Map<String, String> configs) {
245268 StringBuilder confStr = new StringBuilder();
246- for(Map.Entry<String,String> entry : configs.entrySet()) {
269+ for (Map.Entry<String, String> entry : configs.entrySet()) {
247270 confStr.append(entry.getKey() + ":" + entry.getValue() + " ");
248271 }
249272 execute("ccm updateconf " + confStr);
@@ -339,13 +362,13 @@
339362 * @param requireClientAuth Whether or not to require Clients used authentication.
340363 */
341364 public void enableSSL(boolean requireClientAuth) {
342- ImmutableMap.Builder<String,String> configs = ImmutableMap.builder();
365+ ImmutableMap.Builder<String, String> configs = ImmutableMap.builder();
343366
344367 configs.put("client_encryption_options.enabled", "true");
345368 configs.put("client_encryption_options.keystore", DEFAULT_SERVER_KEYSTORE_FILE.getAbsolutePath());
346369 configs.put("client_encryption_options.keystore_password", DEFAULT_SERVER_KEYSTORE_PASSWORD);
347370
348- if(requireClientAuth) {
371+ if (requireClientAuth) {
349372 configs.put("client_encryption_options.require_client_auth", "true");
350373 configs.put("client_encryption_options.truststore", DEFAULT_SERVER_TRUSTSTORE_FILE.getAbsolutePath());
351374 configs.put("client_encryption_options.truststore_password", DEFAULT_SERVER_TRUSTSTORE_PASSWORD);
@@ -396,12 +419,37 @@
396419 return IP_PREFIX + Integer.toString(nodeNumber);
397420 }
398421
422+ public static class terminationHook extends Thread {
423+ public void run() {
424+ logger.debug("shut down hook task..");
425+
426+ if (PerClassSingleNodeCluster.cluster != null) {
427+ PerClassSingleNodeCluster.cluster.close();
428+ }
429+ if (PerClassSingleNodeCluster.ccmBridge == null) {
430+ logger.error("No cluster to discard");
431+ } else if (PerClassSingleNodeCluster.erroredOut) {
432+ PerClassSingleNodeCluster.ccmBridge.remove("test-class");
433+ logger.info("Error during tests, kept C* logs in " + PerClassSingleNodeCluster.ccmBridge.ccmDir);
434+ } else {
435+ PerClassSingleNodeCluster.ccmBridge.remove("test-class");
436+ PerClassSingleNodeCluster.ccmBridge.ccmDir.delete();
437+ }
438+
439+ }
440+ }
441+
399442 // One cluster for the whole test class
400443 public static abstract class PerClassSingleNodeCluster {
401444
402- protected static CCMBridge cassandraCluster;
445+ protected static CCMBridge ccmBridge;
403446 private static boolean erroredOut;
404- private static boolean schemaCreated;
447+ private static boolean clusterInitialized=false;
448+ private static AtomicLong ksNumber;
449+ protected String keyspace;
450+
451+ protected static InetSocketAddress hostAddress;
452+ protected static int[] ports;
405453
406454 protected static Cluster cluster;
407455 protected static Session session;
@@ -417,59 +465,57 @@
417465 erroredOut = true;
418466 }
419467
420- public void createCluster() {
421- erroredOut = false;
422- schemaCreated = false;
423- cassandraCluster = CCMBridge.create("test", 1);
424- try {
425- Builder builder = Cluster.builder();
426- builder = configure(builder);
427- cluster = builder.addContactPoints(IP_PREFIX + '1').build();
428- session = cluster.connect();
429- } catch (NoHostAvailableException e) {
430- erroredOut = true;
431- for (Map.Entry<InetSocketAddress, Throwable> entry : e.getErrors().entrySet())
432- logger.info("Error connecting to " + entry.getKey() + ": " + entry.getValue());
433- throw new RuntimeException(e);
434- }
468+ @BeforeClass(groups = { "short", "long" })
469+ public void beforeClass() {
470+ maybeInitCluster();
471+ initKeyspace();
435472 }
436473
437- @AfterClass(groups = {"short", "long"})
438- public static void discardCluster() {
439- if (cluster != null)
440- cluster.close();
474+ @AfterClass(groups = { "short", "long" })
475+ public void afterClass() {
476+ clearSimpleKeyspace();
477+ }
441478
442- if (cassandraCluster == null) {
443- logger.error("No cluster to discard");
444- } else if (erroredOut) {
445- cassandraCluster.stop();
446- logger.info("Error during tests, kept C* logs in " + cassandraCluster.ccmDir);
447- } else {
448- cassandraCluster.remove();
449- cassandraCluster.ccmDir.delete();
479+ private void maybeInitCluster(){
480+ if (!clusterInitialized){
481+ try {
482+ //launch ccm cluster
483+ ccmBridge = CCMBridge.create("test-class");
484+
485+ ports = new int[5];
486+ for (int i = 0; i < 5; i++) {
487+ ports[i] = TestUtils.findAvailablePort(10000 + i);
488+ }
489+
490+ ccmBridge.bootstrapNodeWithPorts(1, ports[0], ports[1], ports[2], ports[3], ports[4]);
491+ ksNumber = new AtomicLong(0);
492+ erroredOut = false;
493+ hostAddress = new InetSocketAddress(InetAddress.getByName(IP_PREFIX + 1), ports[2]);
494+
495+ Runtime r = Runtime.getRuntime();
496+ r.addShutdownHook(new terminationHook());
497+ clusterInitialized = true;
498+
499+ } catch (UnknownHostException e) {
500+ throw new RuntimeException(e);
501+ }
450502 }
451- }
452503
453- @BeforeClass(groups = {"short", "long"})
454- public void beforeClass() {
455- createCluster();
456- maybeCreateSchema();
457504 }
458505
459- public void maybeCreateSchema() {
460506
507+ private void initKeyspace() {
461508 try {
462- if (schemaCreated)
463- return;
509+ Builder builder = Cluster.builder();
464510
465- try {
466- session.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, SIMPLE_KEYSPACE, 1));
467- } catch (AlreadyExistsException e) {
468- // It's ok, ignore
469- }
511+ builder = configure(builder);
470512
471- session.execute("USE " + SIMPLE_KEYSPACE);
513+ cluster = builder.addContactPointsWithPorts(Collections.singletonList(hostAddress)).build();
514+ session = cluster.connect();
515+ keyspace = SIMPLE_KEYSPACE + "_" + ksNumber.incrementAndGet();
516+ session.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, 1));
472517
518+ session.execute("USE " + keyspace);
473519 for (String tableDef : getTableDefinitions()) {
474520 try {
475521 session.execute(tableDef);
@@ -477,13 +523,26 @@
477523 // It's ok, ignore
478524 }
479525 }
480-
481- schemaCreated = true;
526+ } catch (AlreadyExistsException e) {
527+ // It's ok, ignore (not supposed to go there)
528+ } catch (NoHostAvailableException e) {
529+ erroredOut = true;
530+ for (Map.Entry<InetSocketAddress, Throwable> entry : e.getErrors().entrySet())
531+ logger.info("Error connecting to " + entry.getKey() + ": " + entry.getValue());
532+ throw new RuntimeException(e);
482533 } catch (DriverException e) {
483534 erroredOut = true;
484535 throw e;
485536 }
486537 }
538+
539+ private void clearSimpleKeyspace() {
540+ session.execute("DROP KEYSPACE " + keyspace);
541+ if (cluster != null) {
542+ cluster.close();
543+ }
544+ }
545+
487546 }
488547
489548 public static class CCMCluster {
@@ -518,10 +577,12 @@
518577 try {
519578 String[] contactPoints = new String[totalNodes];
520579 for (int i = 0; i < totalNodes; i++)
521- contactPoints[i] = IP_PREFIX + (i+1);
522-
523- try { Thread.sleep(1000); } catch (Exception e) {}
580+ contactPoints[i] = IP_PREFIX + (i + 1);
524581
582+ try {
583+ Thread.sleep(1000);
584+ } catch (Exception e) {
585+ }
525586 this.cluster = builder.addContactPoints(contactPoints).build();
526587 this.session = cluster.connect();
527588 } catch (NoHostAvailableException e) {
modified driver-core/src/test/java/com/datastax/driver/core/PreparedStatementTest.java +5 −4
@@ -25,6 +25,7 @@
2525
2626 import com.datastax.driver.core.exceptions.NoHostAvailableException;
2727 import com.datastax.driver.core.exceptions.UnsupportedFeatureException;
28+
2829 import static com.datastax.driver.core.TestUtils.*;
2930
3031 /**
@@ -301,10 +302,10 @@
301302
302303 assertEquals(session.execute(ps.bind("123")).one().getInt("i"), 17);
303304
304- cassandraCluster.stop();
305+ ccmBridge.stop();
305306 waitForDown(CCMBridge.IP_PREFIX + '1', cluster);
306307
307- cassandraCluster.start();
308+ ccmBridge.start();
308309 waitFor(CCMBridge.IP_PREFIX + '1', cluster, 120);
309310
310311 try
@@ -413,9 +414,9 @@
413414
414415 @Test(groups="short")
415416 public void should_set_routing_key_on_case_insensitive_keyspace_and_table() {
416- session.execute("CREATE TABLE ks.foo (i int PRIMARY KEY)");
417+ session.execute(String.format("CREATE TABLE %s.foo (i int PRIMARY KEY)",keyspace));
417418
418- PreparedStatement ps = session.prepare("INSERT INTO ks.foo (i) VALUES (?)");
419+ PreparedStatement ps = session.prepare(String.format("INSERT INTO %s.foo (i) VALUES (?)",keyspace));
419420 BoundStatement bs = ps.bind(1);
420421 assertThat(bs.getRoutingKey()).isNotNull();
421422 }
modified driver-core/src/test/java/com/datastax/driver/core/RecommissionedNodeTest.java +2 −0
@@ -7,12 +7,14 @@
77 import org.testng.annotations.AfterMethod;
88 import org.testng.annotations.Test;
99
10+
1011 import com.datastax.driver.core.utils.CassandraVersion;
1112
1213 import static com.datastax.driver.core.Assertions.*;
1314 import static com.datastax.driver.core.Host.State.DOWN;
1415 import static com.datastax.driver.core.Host.State.UP;
1516
17+
1618 /**
1719 * Due to C* gossip bugs, system.peers may report nodes that are gone from the cluster.
1820 *
modified driver-core/src/test/java/com/datastax/driver/core/SchemaTest.java +16 −16
@@ -33,52 +33,52 @@
3333 @Override
3434 protected Collection<String> getTableDefinitions() {
3535
36- String sparse = "CREATE TABLE ks.sparse (\n"
36+ String sparse = String.format("CREATE TABLE %s.sparse (\n"
3737 + " k text,\n"
3838 + " c1 int,\n"
3939 + " c2 float,\n"
4040 + " l list<text>,\n"
4141 + " v int,\n"
4242 + " PRIMARY KEY (k, c1, c2)\n"
43- + ");";
43+ + ");", keyspace);
4444
45- String st = "CREATE TABLE ks.static (\n"
45+ String st = String.format("CREATE TABLE %s.static (\n"
4646 + " k text,\n"
4747 + " i int,\n"
4848 + " m map<text, timeuuid>,\n"
4949 + " v int,\n"
5050 + " PRIMARY KEY (k)\n"
51- + ");";
51+ + ");", keyspace);
5252
53- String counters = "CREATE TABLE ks.counters (\n"
53+ String counters = String.format("CREATE TABLE %s.counters (\n"
5454 + " k text,\n"
5555 + " c counter,\n"
5656 + " PRIMARY KEY (k)\n"
57- + ");";
57+ + ");", keyspace);
5858
59- String compactStatic = "CREATE TABLE ks.compact_static (\n"
59+ String compactStatic = String.format("CREATE TABLE %s.compact_static (\n"
6060 + " k text,\n"
6161 + " i int,\n"
6262 + " t timeuuid,\n"
6363 + " v int,\n"
6464 + " PRIMARY KEY (k)\n"
65- + ") WITH COMPACT STORAGE;";
65+ + ") WITH COMPACT STORAGE;", keyspace);
6666
67- String compactDynamic = "CREATE TABLE ks.compact_dynamic (\n"
67+ String compactDynamic = String.format("CREATE TABLE %s.compact_dynamic (\n"
6868 + " k text,\n"
6969 + " c int,\n"
7070 + " v timeuuid,\n"
7171 + " PRIMARY KEY (k, c)\n"
72- + ") WITH COMPACT STORAGE;";
72+ + ") WITH COMPACT STORAGE;", keyspace);
7373
74- String compactComposite = "CREATE TABLE ks.compact_composite (\n"
74+ String compactComposite = String.format("CREATE TABLE %s.compact_composite (\n"
7575 + " k text,\n"
7676 + " c1 int,\n"
7777 + " c2 float,\n"
7878 + " c3 double,\n"
7979 + " v timeuuid,\n"
8080 + " PRIMARY KEY (k, c1, c2, c3)\n"
81- + ") WITH COMPACT STORAGE;";
81+ + ") WITH COMPACT STORAGE;", keyspace);
8282
8383 cql3.put("sparse", sparse);
8484 cql3.put("static", st);
@@ -87,7 +87,7 @@
8787 compact.put("compact_dynamic", compactDynamic);
8888 compact.put("compact_composite", compactComposite);
8989
90- withOptions = "CREATE TABLE ks.with_options (\n"
90+ withOptions = String.format("CREATE TABLE %s.with_options (\n"
9191 + " k text,\n"
9292 + " v1 int,\n"
9393 + " v2 int,\n"
@@ -102,7 +102,7 @@
102102 + " AND caching = 'ALL'\n"
103103 + " AND comment = 'My awesome table'\n"
104104 + " AND compaction = { 'class' : 'org.apache.cassandra.db.compaction.LeveledCompactionStrategy', 'sstable_size_in_mb' : 15 }\n"
105- + " AND compression = { 'sstable_compression' : 'org.apache.cassandra.io.compress.SnappyCompressor', 'chunk_length_kb' : 128 };";
105+ + " AND compression = { 'sstable_compression' : 'org.apache.cassandra.io.compress.SnappyCompressor', 'chunk_length_kb' : 128 };", keyspace);
106106
107107 List<String> allDefs = new ArrayList<String>();
108108 allDefs.addAll(cql3.values());
@@ -126,7 +126,7 @@
126126 @Test(groups = "short")
127127 public void schemaExportTest() {
128128
129- KeyspaceMetadata metadata = cluster.getMetadata().getKeyspace(TestUtils.SIMPLE_KEYSPACE);
129+ KeyspaceMetadata metadata = cluster.getMetadata().getKeyspace(keyspace);
130130
131131 for (Map.Entry<String, String> tableEntry : cql3.entrySet()) {
132132 String table = tableEntry.getKey();
@@ -144,7 +144,7 @@
144144 // Same remark as the preceding test
145145 @Test(groups = "short")
146146 public void schemaExportOptionsTest() {
147- TableMetadata metadata = cluster.getMetadata().getKeyspace(TestUtils.SIMPLE_KEYSPACE).getTable("with_options");
147+ TableMetadata metadata = cluster.getMetadata().getKeyspace(keyspace).getTable("with_options");
148148
149149 String withOpts = withOptions;
150150 // With C* 2.0 we'll have a few additional options
added driver-core/src/test/java/com/datastax/driver/core/SessionLeakTest.java +85 −0
@@ -0,0 +1,85 @@
1+package com.datastax.driver.core;
2+
3+import org.testng.annotations.Test;
4+
5+import java.util.Collections;
6+
7+import static org.testng.Assert.assertEquals;
8+
9+
10+public class SessionLeakTest {
11+
12+
13+ @Test(groups = "short")
14+ public void connectionLeakTest() throws Exception {
15+ // Checking for JAVA-342
16+ CCMBridge ccmBridge;
17+ ccmBridge = CCMBridge.create("test", 1);
18+
19+ // give the driver time to close other sessions in this class
20+ //Thread.sleep(10);
21+
22+ // create a new cluster object and ensure 0 sessions and connections
23+ Cluster cluster = Cluster.builder().addContactPoints(CCMBridge.IP_PREFIX + '1').build();
24+
25+ int corePoolSize = cluster.getConfiguration()
26+ .getPoolingOptions()
27+ .getCoreConnectionsPerHost(HostDistance.LOCAL);
28+
29+ assertEquals(cluster.manager.sessions.size(), 0);
30+ assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 0);
31+
32+ // ensure sessions.size() returns with 1 control connection + core pool size.
33+ Session session = cluster.connect();
34+ assertEquals(cluster.manager.sessions.size(), 1);
35+ assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1 + corePoolSize);
36+
37+ // ensure sessions.size() returns to 0 with only 1 active connection (the control connection)
38+ session.close();
39+ assertEquals(cluster.manager.sessions.size(), 0);
40+ assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1);
41+
42+ try {
43+ Session thisSession;
44+
45+ // ensure bootstrapping a node does not create additional connections
46+
47+
48+ ccmBridge.bootstrapNode(2);
49+ assertEquals(cluster.manager.sessions.size(), 0);
50+ assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1);
51+
52+ // ensure a new session gets registered and core connections are established
53+ // there should be corePoolSize more connections to accommodate for the new host.
54+ thisSession = cluster.connect();
55+ assertEquals(cluster.manager.sessions.size(), 1);
56+
57+ assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1 + (corePoolSize * 2));
58+
59+ // ensure bootstrapping a node does not create additional connections that won't get cleaned up
60+ thisSession.close();
61+
62+ assertEquals(cluster.manager.sessions.size(), 0);
63+
64+ assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1);
65+
66+
67+ } finally {
68+ // ensure we decommission node2 for the rest of the tests
69+
70+ assertEquals(cluster.manager.sessions.size(), 0);
71+ assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1);
72+
73+ if(cluster != null){
74+ cluster.close();
75+
76+ }
77+
78+ if (ccmBridge != null) {
79+ ccmBridge.remove();
80+ }
81+
82+
83+ }
84+ }
85+}
modified driver-core/src/test/java/com/datastax/driver/core/SessionTest.java +10 −64
@@ -15,17 +15,20 @@
1515 */
1616 package com.datastax.driver.core;
1717
18-import java.util.Arrays;
19-import java.util.Collection;
20-import java.util.List;
21-import java.util.Locale;
22-import java.util.concurrent.*;
18+import java.util.*;
19+import java.util.concurrent.CountDownLatch;
20+import java.util.concurrent.ExecutorService;
21+import java.util.concurrent.Executors;
22+import java.util.concurrent.TimeUnit;
2323
2424 import org.testng.annotations.Test;
25+
2526 import static org.testng.Assert.assertEquals;
2627 import static org.testng.Assert.assertTrue;
2728 import static org.testng.Assert.fail;
2829
30+
31+
2932 /**
3033 * Simple test of the Sessions methods against a one node cluster.
3134 */
@@ -112,7 +115,7 @@
112115
113116 try {
114117
115- Session compressedSession = cluster.connect(TestUtils.SIMPLE_KEYSPACE);
118+ Session compressedSession = cluster.connect(keyspace);
116119
117120 // Simple calls to all versions of the execute/executeAsync methods
118121 String key = "execute_compressed_test";
@@ -196,63 +199,6 @@
196199 }
197200 }
198201
199- @Test(groups = "short")
200- public void connectionLeakTest() throws Exception {
201- // Checking for JAVA-342
202-
203- // give the driver time to close other sessions in this class
204- Thread.sleep(10);
205-
206- // create a new cluster object and ensure 0 sessions and connections
207- Cluster cluster = Cluster.builder().addContactPoints(CCMBridge.IP_PREFIX + '1').build();
208-
209- int corePoolSize = cluster.getConfiguration()
210- .getPoolingOptions()
211- .getCoreConnectionsPerHost(HostDistance.LOCAL);
212-
213- assertEquals(cluster.manager.sessions.size(), 0);
214- assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 0);
215-
216- // ensure sessions.size() returns with 1 control connection + core pool size.
217- Session session = cluster.connect();
218- assertEquals(cluster.manager.sessions.size(), 1);
219- assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1 + corePoolSize);
220-
221- // ensure sessions.size() returns to 0 with only 1 active connection (the control connection)
222- session.close();
223- assertEquals(cluster.manager.sessions.size(), 0);
224- assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1);
225-
226- try {
227- Session thisSession;
228-
229- // ensure bootstrapping a node does not create additional connections
230- cassandraCluster.bootstrapNode(2);
231- assertEquals(cluster.manager.sessions.size(), 0);
232- assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1);
233-
234- // ensure a new session gets registered and core connections are established
235- // there should be corePoolSize more connections to accommodate for the new host.
236- thisSession = cluster.connect();
237- assertEquals(cluster.manager.sessions.size(), 1);
238- assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1 + (corePoolSize * 2));
239-
240- // ensure bootstrapping a node does not create additional connections that won't get cleaned up
241- thisSession.close();
242- assertEquals(cluster.manager.sessions.size(), 0);
243- assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1);
244-
245- } finally {
246- // ensure we decommission node2 for the rest of the tests
247- cassandraCluster.decommissionNode(2);
248-
249- assertEquals(cluster.manager.sessions.size(), 0);
250- assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1);
251-
252- cluster.close();
253- }
254- }
255-
256202 /**
257203 * Checks for deadlocks when a session shutdown races with the initialization of the cluster (JAVA-418).
258204 */
@@ -262,7 +208,7 @@
262208
263209 // Use our own cluster and session (not the ones provided by the parent class) because we want an uninitialized cluster
264210 // (note the use of newSession below)
265- final Cluster cluster = Cluster.builder().addContactPoint(CCMBridge.IP_PREFIX + "1").build();
211+ final Cluster cluster = Cluster.builder().addContactPointsWithPorts(Collections.singletonList(hostAddress)).build();
266212 final Session session = cluster.newSession();
267213
268214 // Spawn two threads to simulate the race
modified driver-core/src/test/java/com/datastax/driver/core/TableMetadataTest.java +1 −1
@@ -19,7 +19,7 @@
1919
2020 @Test(groups = "short")
2121 public void should_escape_single_quote_table_comment() {
22- TableMetadata table = cluster.getMetadata().getKeyspace("ks").getTable("single_quote");
22+ TableMetadata table = cluster.getMetadata().getKeyspace(keyspace).getTable("single_quote");
2323 assertThat(table.asCQLQuery()).contains("comment with single quote '' should work");
2424 }
2525 }
No newline at the end of the file
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderITest.java +9 −9
@@ -15,12 +15,12 @@
1515 */
1616 package com.datastax.driver.core.querybuilder;
1717
18-
1918 import java.util.Arrays;
2019 import java.util.Collection;
2120 import java.util.TreeSet;
2221
2322 import org.testng.annotations.Test;
23+
2424 import static org.testng.Assert.*;
2525
2626 import com.datastax.driver.core.*;
@@ -44,10 +44,10 @@
4444 public void remainingDeleteTests() throws Exception {
4545
4646 Statement query;
47- TableMetadata table = cluster.getMetadata().getKeyspace(TestUtils.SIMPLE_KEYSPACE).getTable(TABLE_TEXT);
47+ TableMetadata table = cluster.getMetadata().getKeyspace(keyspace).getTable(TABLE_TEXT);
4848 assertNotNull(table);
4949
50- String expected = "DELETE k FROM ks.test_text;";
50+ String expected = String.format("DELETE k FROM %s.test_text;", keyspace);
5151 query = delete("k").from(table);
5252 assertEquals(query.toString(), expected);
5353 try {
@@ -210,25 +210,25 @@
210210 @Test(groups = "short")
211211 @CassandraVersion(major=2.0, minor=7, description="DELETE..IF EXISTS only supported in 2.0.7+ (CASSANDRA-5708)")
212212 public void conditionalDeletesTest() throws Exception {
213- session.execute("INSERT INTO ks.test_int (k, a, b) VALUES (1, 1, 1)");
213+ session.execute(String.format("INSERT INTO %s.test_int (k, a, b) VALUES (1, 1, 1)",keyspace));
214214
215215 Statement delete;
216216 Row row;
217- delete = delete().from(TestUtils.SIMPLE_KEYSPACE, TABLE_INT).where(eq("k", 2)).ifExists();
217+ delete = delete().from(keyspace, TABLE_INT).where(eq("k", 2)).ifExists();
218218 row = session.execute(delete).one();
219219 assertFalse(row.getBool("[applied]"));
220220
221- delete = delete().from(TestUtils.SIMPLE_KEYSPACE, TABLE_INT).where(eq("k", 1)).ifExists();
221+ delete = delete().from(keyspace, TABLE_INT).where(eq("k", 1)).ifExists();
222222 row = session.execute(delete).one();
223223 assertTrue(row.getBool("[applied]"));
224224
225- session.execute("INSERT INTO ks.test_int (k, a, b) VALUES (1, 1, 1)");
225+ session.execute(String.format("INSERT INTO %s.test_int (k, a, b) VALUES (1, 1, 1)", keyspace));
226226
227- delete = delete().from(TestUtils.SIMPLE_KEYSPACE, TABLE_INT).where(eq("k", 1)).onlyIf(eq("a", 1)).and(eq("b", 2));
227+ delete = delete().from(keyspace, TABLE_INT).where(eq("k", 1)).onlyIf(eq("a", 1)).and(eq("b", 2));
228228 row = session.execute(delete).one();
229229 assertFalse(row.getBool("[applied]"));
230230
231- delete = delete().from(TestUtils.SIMPLE_KEYSPACE, TABLE_INT).where(eq("k", 1)).onlyIf(eq("a", 1)).and(eq("b", 1));
231+ delete = delete().from(keyspace, TABLE_INT).where(eq("k", 1)).onlyIf(eq("a", 1)).and(eq("b", 1));
232232 row = session.execute(delete).one();
233233 assertTrue(row.getBool("[applied]"));
234234 }
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderRoutingKeyTest.java +8 −6
@@ -20,10 +20,12 @@
2020 import java.util.Collection;
2121
2222 import org.testng.annotations.Test;
23+
2324 import static org.testng.Assert.assertEquals;
2425 import static org.testng.Assert.assertNotNull;
2526
2627 import com.datastax.driver.core.*;
28+
2729 import static com.datastax.driver.core.querybuilder.QueryBuilder.*;
2830
2931 public class QueryBuilderRoutingKeyTest extends CCMBridge.PerClassSingleNodeCluster {
@@ -41,7 +43,7 @@
4143 public void textRoutingKeyTest() throws Exception {
4244
4345 Statement query;
44- TableMetadata table = cluster.getMetadata().getKeyspace(TestUtils.SIMPLE_KEYSPACE).getTable(TABLE_TEXT);
46+ TableMetadata table = cluster.getMetadata().getKeyspace(keyspace).getTable(TABLE_TEXT);
4547 assertNotNull(table);
4648
4749 String txt = "If she weighs the same as a duck... she's made of wood.";
@@ -61,7 +63,7 @@
6163 public void intRoutingKeyTest() throws Exception {
6264
6365 Statement query;
64- TableMetadata table = cluster.getMetadata().getKeyspace(TestUtils.SIMPLE_KEYSPACE).getTable(TABLE_INT);
66+ TableMetadata table = cluster.getMetadata().getKeyspace(keyspace).getTable(TABLE_INT);
6567 assertNotNull(table);
6668
6769 query = insertInto(table).values(new String[]{"k", "a", "b"}, new Object[]{42, 1, 2});
@@ -82,7 +84,7 @@
8284 public void intRoutingBatchKeyTest() throws Exception {
8385
8486 RegularStatement query;
85- TableMetadata table = cluster.getMetadata().getKeyspace(TestUtils.SIMPLE_KEYSPACE).getTable(TABLE_INT);
87+ TableMetadata table = cluster.getMetadata().getKeyspace(keyspace).getTable(TABLE_INT);
8688 assertNotNull(table);
8789
8890 ByteBuffer bb = ByteBuffer.allocate(4);
@@ -95,8 +97,8 @@
9597 query = select().from(table).where(eq("k", 42));
9698
9799 batch_query = "BEGIN BATCH ";
98- batch_query += "INSERT INTO ks.test_int(k,a) VALUES (42,1);";
99- batch_query += "UPDATE ks.test_int USING TTL 400;";
100+ batch_query += String.format("INSERT INTO %s.test_int(k,a) VALUES (42,1);", keyspace);
101+ batch_query += String.format("UPDATE %s.test_int USING TTL 400;", keyspace);
100102 batch_query += "APPLY BATCH;";
101103 batch = batch()
102104 .add(insertInto(table).values(new String[]{"k", "a"}, new Object[]{42, 1}))
@@ -106,7 +108,7 @@
106108 // TODO: rs = session.execute(batch); // Not guaranteed to be valid CQL
107109
108110 batch_query = "BEGIN BATCH ";
109- batch_query += "SELECT * FROM ks.test_int WHERE k=42;";
111+ batch_query += String.format("SELECT * FROM %s.test_int WHERE k=42;", keyspace);
110112 batch_query += "APPLY BATCH;";
111113 batch = batch(query);
112114 assertEquals(batch.getRoutingKey(), bb);