Skip to content

Commit d340a9f

Browse files
committed
Minor improvements to CCM tests
1 parent 3980cd0 commit d340a9f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+259
-412
lines changed

driver-core/src/test/java/com/datastax/driver/core/AsyncQueryTest.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.datastax.driver.core.utils.CassandraVersion;
2525
import com.google.common.base.Function;
2626
import com.google.common.base.Throwables;
27-
import com.google.common.collect.Lists;
2827
import com.google.common.util.concurrent.AsyncFunction;
2928
import com.google.common.util.concurrent.ListenableFuture;
3029
import com.google.common.util.concurrent.Uninterruptibles;
@@ -83,13 +82,7 @@ public void cancelled_query_should_release_the_connection() throws InterruptedEx
8382
public void should_init_cluster_and_session_if_needed() throws Exception {
8483
// For this test we need an uninitialized cluster, so we can't reuse the one provided by the
8584
// parent class. Rebuild a new one with the same (unique) host.
86-
Host host = cluster().getMetadata().allHosts().iterator().next();
87-
88-
Cluster cluster2 =
89-
register(
90-
Cluster.builder()
91-
.addContactPointsWithPorts(Lists.newArrayList(host.getEndPoint().resolve()))
92-
.build());
85+
Cluster cluster2 = register(createClusterBuilder().build());
9386
try {
9487
Session session2 = cluster2.newSession();
9588

driver-core/src/test/java/com/datastax/driver/core/AuthenticationTest.java

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,7 @@ public void sleepIf12() {
5353
@Test(groups = "short")
5454
public void should_connect_with_credentials() {
5555
PlainTextAuthProvider authProvider = spy(new PlainTextAuthProvider("cassandra", "cassandra"));
56-
Cluster cluster =
57-
Cluster.builder()
58-
.addContactPoints(getContactPoints())
59-
.withPort(ccm().getBinaryPort())
60-
.withAuthProvider(authProvider)
61-
.build();
56+
Cluster cluster = createClusterBuilder().withAuthProvider(authProvider).build();
6257
cluster.connect();
6358
verify(authProvider, atLeastOnce())
6459
.newAuthenticator(
@@ -78,13 +73,7 @@ public void should_connect_with_credentials() {
7873
*/
7974
@Test(groups = "short")
8075
public void should_fail_to_connect_with_wrong_credentials() {
81-
Cluster cluster =
82-
register(
83-
Cluster.builder()
84-
.addContactPoints(getContactPoints())
85-
.withPort(ccm().getBinaryPort())
86-
.withCredentials("bogus", "bogus")
87-
.build());
76+
Cluster cluster = register(createClusterBuilder().withCredentials("bogus", "bogus").build());
8877

8978
try {
9079
cluster.connect();
@@ -106,12 +95,7 @@ public void should_fail_to_connect_with_wrong_credentials() {
10695

10796
@Test(groups = "short", expectedExceptions = AuthenticationException.class)
10897
public void should_fail_to_connect_without_credentials() {
109-
Cluster cluster =
110-
register(
111-
Cluster.builder()
112-
.addContactPoints(getContactPoints())
113-
.withPort(ccm().getBinaryPort())
114-
.build());
98+
Cluster cluster = register(createClusterBuilder().build());
11599
cluster.connect();
116100
}
117101

@@ -124,9 +108,7 @@ public void should_fail_to_connect_without_credentials() {
124108
@CCMConfig(dirtiesContext = true)
125109
public void should_connect_with_slow_server() {
126110
Cluster cluster =
127-
Cluster.builder()
128-
.addContactPoints(getContactPoints())
129-
.withPort(ccm().getBinaryPort())
111+
createClusterBuilder()
130112
.withAuthProvider(new SlowAuthProvider())
131113
.withPoolingOptions(new PoolingOptions().setHeartbeatIntervalSeconds(1))
132114
.build();
@@ -169,13 +151,7 @@ public void run() {
169151
@Test(groups = "short")
170152
public void should_not_create_pool_with_wrong_credentials() {
171153
PlainTextAuthProvider authProvider = new PlainTextAuthProvider("cassandra", "cassandra");
172-
Cluster cluster =
173-
register(
174-
Cluster.builder()
175-
.addContactPoints(getContactPoints())
176-
.withPort(ccm().getBinaryPort())
177-
.withAuthProvider(authProvider)
178-
.build());
154+
Cluster cluster = register(createClusterBuilder().withAuthProvider(authProvider).build());
179155
cluster.init();
180156
authProvider.setPassword("wrong");
181157
Level previous = TestUtils.setLogLevel(Session.class, Level.WARN);

driver-core/src/test/java/com/datastax/driver/core/CCMAccess.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
import java.io.Closeable;
1919
import java.io.File;
20+
import java.net.InetAddress;
2021
import java.net.InetSocketAddress;
22+
import java.util.List;
2123
import java.util.Map;
2224

2325
public interface CCMAccess extends Closeable {
@@ -41,17 +43,13 @@ enum Workload {
4143
* is assumed that this value is only used for representing the compatible Cassandra version for
4244
* that DSE version.
4345
*
44-
* <p>
45-
*
4646
* @return The version of this CCM cluster.
4747
*/
4848
VersionNumber getCassandraVersion();
4949

5050
/**
5151
* Returns the DSE version of this CCM cluster if this is a DSE cluster, otherwise null.
5252
*
53-
* <p>
54-
*
5553
* @return The version of this CCM cluster.
5654
*/
5755
VersionNumber getDSEVersion();
@@ -87,13 +85,29 @@ enum Workload {
8785
void setKeepLogs(boolean keepLogs);
8886

8987
/**
90-
* @return the node count for each datacenter, mapped in the corresponding cell of the returned
91-
* int array. This is the count that was passed at initialization (that is, the argument to
92-
* {@link CCMBridge.Builder#withNodes(int...)} or {@link CCMConfig#numberOfNodes()}). Note
93-
* that it will <b>NOT</b> be updated dynamically if nodes are added or removed at runtime.
88+
* Returns the node count for each datacenter, mapped in the corresponding cell of the returned
89+
* int array.
90+
*
91+
* <p>This is the count that was passed at initialization (that is, the argument to {@link
92+
* CCMBridge.Builder#withNodes(int...)} or {@link CCMConfig#numberOfNodes()}). Note that it will
93+
* <b>NOT</b> be updated dynamically if nodes are added or removed at runtime.
94+
*
95+
* @return the node count for each datacenter.
9496
*/
9597
int[] getNodeCount();
9698

99+
/**
100+
* Returns the contact points to use to contact the CCM cluster.
101+
*
102+
* <p>This reflects the initial number of nodes in the cluster, as configured at initialization
103+
* (that is, the argument to {@link CCMBridge.Builder#withNodes(int...)} or {@link
104+
* CCMConfig#numberOfNodes()}). Note that it will <b>NOT</b> be updated dynamically if nodes are
105+
* added or removed at runtime.
106+
*
107+
* @return the contact points to use to contact the CCM cluster.
108+
*/
109+
List<InetAddress> getContactPoints();
110+
97111
/**
98112
* Returns the address of the {@code nth} host in the CCM cluster (counting from 1, i.e., {@code
99113
* addressOfNode(1)} returns the address of the first node.

driver-core/src/test/java/com/datastax/driver/core/CCMBridge.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@
3535
import java.io.OutputStream;
3636
import java.io.PrintWriter;
3737
import java.io.StringWriter;
38+
import java.net.InetAddress;
3839
import java.net.InetSocketAddress;
40+
import java.net.UnknownHostException;
41+
import java.util.ArrayList;
3942
import java.util.Arrays;
4043
import java.util.Collections;
4144
import java.util.HashMap;
4245
import java.util.LinkedHashSet;
46+
import java.util.List;
4347
import java.util.Map;
4448
import java.util.Set;
4549
import java.util.concurrent.TimeUnit;
@@ -367,6 +371,24 @@ public int[] getNodeCount() {
367371
return Arrays.copyOf(nodes, nodes.length);
368372
}
369373

374+
@Override
375+
public List<InetAddress> getContactPoints() {
376+
List<InetAddress> contactPoints = new ArrayList<InetAddress>();
377+
int n = 1;
378+
for (int dc = 1; dc <= nodes.length; dc++) {
379+
int nodesInDc = nodes[dc - 1];
380+
for (int i = 0; i < nodesInDc; i++) {
381+
try {
382+
contactPoints.add(InetAddress.getByName(ipOfNode(n)));
383+
} catch (UnknownHostException e) {
384+
Throwables.propagate(e);
385+
}
386+
n++;
387+
}
388+
}
389+
return contactPoints;
390+
}
391+
370392
protected String ipOfNode(int n) {
371393
return ipPrefix + n;
372394
}

driver-core/src/test/java/com/datastax/driver/core/CCMCache.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
import com.google.common.cache.RemovalNotification;
2424
import com.google.common.cache.Weigher;
2525
import java.io.File;
26+
import java.net.InetAddress;
2627
import java.net.InetSocketAddress;
2728
import java.util.Iterator;
29+
import java.util.List;
2830
import java.util.Map;
2931
import java.util.concurrent.ExecutionException;
3032
import java.util.concurrent.atomic.AtomicInteger;
@@ -107,6 +109,11 @@ public int[] getNodeCount() {
107109
return ccm.getNodeCount();
108110
}
109111

112+
@Override
113+
public List<InetAddress> getContactPoints() {
114+
return ccm.getContactPoints();
115+
}
116+
110117
@Override
111118
public InetSocketAddress addressOfNode(int n) {
112119
return ccm.addressOfNode(n);

driver-core/src/test/java/com/datastax/driver/core/CCMTestsSupport.java

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static org.assertj.core.api.Assertions.fail;
2424

2525
import com.datastax.driver.core.CCMAccess.Workload;
26+
import com.datastax.driver.core.Cluster.Builder;
2627
import com.datastax.driver.core.CreateCCM.TestMode;
2728
import com.datastax.driver.core.exceptions.InvalidQueryException;
2829
import com.google.common.base.Throwables;
@@ -157,6 +158,11 @@ public int[] getNodeCount() {
157158
return delegate.getNodeCount();
158159
}
159160

161+
@Override
162+
public List<InetAddress> getContactPoints() {
163+
return delegate.getContactPoints();
164+
}
165+
160166
@Override
161167
public String checkForErrors() {
162168
return delegate.checkForErrors();
@@ -631,7 +637,7 @@ public void beforeTestClass(Object testInstance) throws Exception {
631637
} catch (Exception e) {
632638
LOGGER.error(e.getMessage(), e);
633639
errorOut();
634-
fail(e.getMessage());
640+
throw e;
635641
}
636642
}
637643
}
@@ -706,18 +712,15 @@ public void afterTestClass() throws Exception {
706712
/**
707713
* Returns the cluster builder to use for this test.
708714
*
709-
* <p>The default implementation returns a vanilla builder.
710-
*
711-
* <p>It's not required to call {@link
712-
* com.datastax.driver.core.Cluster.Builder#addContactPointsWithPorts}, it will be done
713-
* automatically.
715+
* <p>The default implementation returns a vanilla builder with contact points and port that match
716+
* the running CCM cluster. Therefore it's not required to call {@link
717+
* Cluster.Builder#addContactPointsWithPorts}, it will be done automatically.
714718
*
715719
* @return The cluster builder to use for the tests.
716720
*/
717721
public Cluster.Builder createClusterBuilder() {
718-
return Cluster.builder()
719-
// use a different codec registry for each cluster instance
720-
.withCodecRegistry(new CodecRegistry());
722+
Cluster.Builder builder = Cluster.builder();
723+
return configureClusterBuilder(builder);
721724
}
722725

723726
/**
@@ -730,7 +733,18 @@ public Cluster.Builder createClusterBuilder() {
730733
* @return The cluster builder to use for the tests.
731734
*/
732735
public Cluster.Builder createClusterBuilderNoDebouncing() {
733-
return Cluster.builder().withQueryOptions(TestUtils.nonDebouncingQueryOptions());
736+
return createClusterBuilder().withQueryOptions(TestUtils.nonDebouncingQueryOptions());
737+
}
738+
739+
/**
740+
* Configures the builder with contact points and port that match the running CCM cluster.
741+
* Therefore it's not required to call {@link Cluster.Builder#addContactPointsWithPorts}, it will
742+
* be done automatically.
743+
*
744+
* @return The cluster builder (for method chaining).
745+
*/
746+
protected Builder configureClusterBuilder(Builder builder) {
747+
return TestUtils.configureClusterBuilder(builder, ccm());
734748
}
735749

736750
/**
@@ -963,6 +977,12 @@ protected void initTestCluster(Object testInstance) throws Exception {
963977
// add contact points only if the provided builder didn't do so
964978
if (builder.getContactPoints().isEmpty()) builder.addContactPoints(getContactPoints());
965979
builder.withPort(ccm.getBinaryPort());
980+
if (ccm().getCassandraVersion().compareTo(VersionNumber.parse("3.10")) >= 0
981+
&& ccm().getCassandraVersion().compareTo(VersionNumber.parse("4.0-beta5")) < 0) {
982+
// prevent usage of protocol v5 for 3.10 and 3.11 since these versions have the beta
983+
// version of it
984+
builder.withProtocolVersion(ProtocolVersion.V4);
985+
}
966986
cluster = register(builder.build());
967987
cluster.init();
968988
}

driver-core/src/test/java/com/datastax/driver/core/ClusterStressTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,7 @@ private class CreateClusterAndCheckConnections
174174
CreateClusterAndCheckConnections(CountDownLatch startSignal) {
175175
this.startSignal = startSignal;
176176
this.cluster =
177-
Cluster.builder()
178-
.addContactPoints(getContactPoints())
179-
.withPort(ccm().getBinaryPort())
177+
createClusterBuilder()
180178
.withPoolingOptions(
181179
new PoolingOptions().setCoreConnectionsPerHost(HostDistance.LOCAL, 1))
182180
.withNettyOptions(channelMonitor.nettyOptions())

driver-core/src/test/java/com/datastax/driver/core/ControlConnectionTest.java

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static com.datastax.driver.core.CreateCCM.TestMode.PER_METHOD;
2020
import static com.datastax.driver.core.ScassandraCluster.SELECT_PEERS;
2121
import static com.datastax.driver.core.ScassandraCluster.datacenter;
22-
import static com.datastax.driver.core.TestUtils.nonDebouncingQueryOptions;
2322
import static com.datastax.driver.core.TestUtils.nonQuietClusterCloseOptions;
2423
import static com.google.common.collect.Lists.newArrayList;
2524
import static org.scassandra.http.client.PrimingRequest.then;
@@ -79,9 +78,7 @@ public void should_prevent_simultaneous_reconnection_attempts() throws Interrupt
7978
// this host
8079
Cluster cluster =
8180
register(
82-
Cluster.builder()
83-
.addContactPoints(getContactPoints().get(0))
84-
.withPort(ccm().getBinaryPort())
81+
createClusterBuilder()
8582
.withReconnectionPolicy(reconnectionPolicy)
8683
.withLoadBalancingPolicy(loadBalancingPolicy)
8784
.build());
@@ -109,25 +106,15 @@ public void should_prevent_simultaneous_reconnection_attempts() throws Interrupt
109106
@CassandraVersion("2.1.0")
110107
public void should_parse_UDT_definitions_when_using_default_protocol_version() {
111108
// First driver instance: create UDT
112-
Cluster cluster =
113-
register(
114-
Cluster.builder()
115-
.addContactPoints(getContactPoints().get(0))
116-
.withPort(ccm().getBinaryPort())
117-
.build());
109+
Cluster cluster = register(createClusterBuilder().build());
118110
Session session = cluster.connect();
119111
session.execute(
120112
"create keyspace ks WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}");
121113
session.execute("create type ks.foo (i int)");
122114
cluster.close();
123115

124116
// Second driver instance: read UDT definition
125-
Cluster cluster2 =
126-
register(
127-
Cluster.builder()
128-
.addContactPoints(getContactPoints().get(0))
129-
.withPort(ccm().getBinaryPort())
130-
.build());
117+
Cluster cluster2 = register(createClusterBuilder().build());
131118
UserType fooType = cluster2.getMetadata().getKeyspace("ks").getUserType("foo");
132119

133120
assertThat(fooType.getFieldNames()).containsExactly("i");
@@ -146,13 +133,7 @@ public void should_parse_UDT_definitions_when_using_default_protocol_version() {
146133
@CCMConfig(numberOfNodes = 3)
147134
public void should_reestablish_if_control_node_decommissioned() throws InterruptedException {
148135
InetSocketAddress firstHost = ccm().addressOfNode(1);
149-
Cluster cluster =
150-
register(
151-
Cluster.builder()
152-
.addContactPoints(firstHost.getAddress())
153-
.withPort(ccm().getBinaryPort())
154-
.withQueryOptions(nonDebouncingQueryOptions())
155-
.build());
136+
Cluster cluster = register(createClusterBuilderNoDebouncing().build());
156137
cluster.init();
157138

158139
// Ensure the control connection host is that of the first node.

driver-core/src/test/java/com/datastax/driver/core/EventDebouncerIntegrationTest.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,7 @@ public void should_wait_until_load_balancing_policy_is_fully_initialized()
4747
throws InterruptedException {
4848
TestLoadBalancingPolicy policy = new TestLoadBalancingPolicy();
4949
final Cluster cluster =
50-
register(
51-
createClusterBuilderNoDebouncing()
52-
.addContactPoints(getContactPoints().get(0))
53-
.withPort(ccm().getBinaryPort())
54-
.withLoadBalancingPolicy(policy)
55-
.build());
50+
register(createClusterBuilderNoDebouncing().withLoadBalancingPolicy(policy).build());
5651
new Thread() {
5752
@Override
5853
public void run() {

driver-core/src/test/java/com/datastax/driver/core/GettableDataIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void onTestContextInitialized() {
5858

5959
@Override
6060
public Cluster.Builder createClusterBuilder() {
61-
return Cluster.builder().withCodecRegistry(registry);
61+
return super.createClusterBuilder().withCodecRegistry(registry);
6262
}
6363

6464
@BeforeClass(groups = "short")

0 commit comments

Comments
 (0)