Skip to content

Commit 145169f

Browse files
tolbertamolim7t
authored andcommitted
Refactor Load Balancing Policy Tests to use Scassandra.
- Pull tests from LoadBalancingPolicyTest into individual test classes for each policy being tested. - Rename SCassandraCluster -> ScassandraCluster. - Add capability to prime keyspace metadata to ScassandraCluster. - Update DCAwareRoundRobinPolicy tests to use builder based construction and HostFilterPolicy. - Update more tests to use TestUtils#nonQuietClusterCloseOptions.
1 parent 744abd5 commit 145169f

21 files changed

Lines changed: 1882 additions & 879 deletions

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ public static ClusterAssert assertThat(Cluster cluster) {
2323
return new ClusterAssert(cluster);
2424
}
2525

26+
public static HostAssert assertThat(Host host) {
27+
return new HostAssert(host);
28+
}
29+
2630
public static SessionAssert assertThat(Session session) {
2731
return new SessionAssert(session);
2832
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public ClusterAssert hasOpenControlConnection() {
4747
return this;
4848
}
4949

50+
public HostAssert controlHost() {
51+
Host host = TestUtils.findOrWaitForControlConnection(actual, 10, TimeUnit.SECONDS);
52+
return new HostAssert(host, actual);
53+
}
54+
5055
public HostAssert host(int hostNumber) {
5156
// Wait for the node to be added if it's not already known.
5257
// In 2.2+ C* does not send an added event until the node is ready so we wait a long time.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ private void primePeerRows(Scassandra scassandra, List<FakeHost> otherHosts) thr
239239
primingClient.prime(
240240
PrimingRequest.queryBuilder()
241241
.withQuery("SELECT * FROM system.peers")
242-
.withColumnTypes(SCassandraCluster.SELECT_PEERS_COLUMN_TYPES)
242+
.withColumnTypes(ScassandraCluster.SELECT_PEERS)
243243
.withRows(rows)
244244
.build());
245245
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,22 @@
1616
package com.datastax.driver.core;
1717

1818
import java.net.InetAddress;
19+
import java.util.Collection;
1920
import java.util.Iterator;
2021
import java.util.Map;
2122
import java.util.concurrent.TimeUnit;
2223
import java.util.concurrent.atomic.AtomicInteger;
2324

2425
import com.google.common.base.Function;
2526
import com.google.common.collect.HashMultiset;
27+
import com.google.common.collect.Iterables;
2628
import com.google.common.collect.Maps;
2729
import org.slf4j.Logger;
2830
import org.slf4j.LoggerFactory;
2931
import org.testng.annotations.Test;
3032

33+
import static com.google.common.collect.Lists.newArrayList;
34+
3135
import static com.datastax.driver.core.Assertions.assertThat;
3236
import static com.datastax.driver.core.TestUtils.nonDebouncingQueryOptions;
3337
import static com.datastax.driver.core.TestUtils.nonQuietClusterCloseOptions;
@@ -152,13 +156,18 @@ public void should_reestablish_if_control_node_decommissioned() throws Interrupt
152156
public void should_randomize_contact_points_when_determining_control_connection() {
153157
int hostCount = 5;
154158
int iterations = 100;
155-
SCassandraCluster scassandras = new SCassandraCluster(hostCount);
159+
ScassandraCluster scassandras = ScassandraCluster.builder().withNodes(hostCount).build();
160+
scassandras.init();
156161

157162
try {
163+
Collection<String> contactPoints = newArrayList();
164+
for(int i = 1; i <= hostCount; i++) {
165+
contactPoints.add(scassandras.address(i));
166+
}
158167
final HashMultiset<InetAddress> occurrencesByHost = HashMultiset.create(hostCount);
159168
for(int i = 0; i < iterations; i++) {
160169
Cluster cluster = Cluster.builder()
161-
.addContactPoints(scassandras.addresses())
170+
.addContactPoints(contactPoints.toArray(new String[hostCount]))
162171
.withNettyOptions(nonQuietClusterCloseOptions)
163172
.build();
164173

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (C) 2012-2015 DataStax Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datastax.driver.core;
17+
18+
import java.util.Arrays;
19+
import java.util.Iterator;
20+
21+
import com.google.common.base.Predicate;
22+
import com.google.common.collect.Iterables;
23+
import org.testng.annotations.DataProvider;
24+
25+
public class DataProviders {
26+
27+
/**
28+
* @return A DataProvider that provides all non-serial consistency levels
29+
*/
30+
@DataProvider(name = "consistencyLevels")
31+
public static Iterator<Object[]> consistencyLevels() {
32+
final Iterator<ConsistencyLevel> consistencyLevels = Iterables.filter(Arrays.asList(ConsistencyLevel.values()), new Predicate<ConsistencyLevel>() {
33+
@Override
34+
public boolean apply(ConsistencyLevel input) {
35+
// filter out serial CLs.
36+
return !input.equals(ConsistencyLevel.SERIAL) && !input.equals(ConsistencyLevel.LOCAL_SERIAL);
37+
}
38+
}).iterator();
39+
40+
return new Iterator<Object[]> () {
41+
42+
@Override
43+
public boolean hasNext() {
44+
return consistencyLevels.hasNext();
45+
}
46+
47+
@Override
48+
public Object[] next() {
49+
return new Object[] {
50+
consistencyLevels.next()
51+
};
52+
}
53+
54+
@Override
55+
public void remove() {
56+
throw new UnsupportedOperationException("This shouldn't happen..");
57+
}
58+
};
59+
}
60+
61+
/**
62+
* @return A DataProvider that provides all serial consistency levels
63+
*/
64+
@DataProvider(name = "serialConsistencyLevels")
65+
public static Object[][] serialConsistencyLevels() {
66+
return new Object[][] {
67+
{ ConsistencyLevel.SERIAL },
68+
{ ConsistencyLevel.LOCAL_SERIAL }
69+
};
70+
}
71+
72+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public class HostAssert extends AbstractAssert<HostAssert, Host> {
3131

3232
private final Cluster cluster;
3333

34+
protected HostAssert(Host host) {
35+
this(host, null);
36+
}
37+
3438
protected HostAssert(Host host, Cluster cluster) {
3539
super(host, HostAssert.class);
3640
this.cluster = cluster;
@@ -63,6 +67,11 @@ public HostAssert isReconnectingFromDown() {
6367
return this;
6468
}
6569

70+
public HostAssert isInDatacenter(String datacenter) {
71+
assertThat(actual.getDatacenter()).isEqualTo(datacenter);
72+
return this;
73+
}
74+
6675
public HostAssert isNotReconnectingFromDown() {
6776
assertThat(actual.getReconnectionAttemptFuture() != null && !actual.getReconnectionAttemptFuture().isDone())
6877
.isFalse();

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@
3232

3333
public class HostConnectionPoolMultiTest {
3434

35-
private SCassandraCluster scassandra;
35+
private ScassandraCluster scassandra;
3636

3737
private Cluster cluster;
3838

3939
@BeforeMethod(groups={"short", "long"})
4040
private void setUp() {
41-
scassandra = new SCassandraCluster(2);
41+
scassandra = ScassandraCluster.builder().withNodes(2).build();
42+
scassandra.init();
4243
}
4344

4445
@AfterMethod(groups={"short", "long"})
@@ -72,15 +73,15 @@ private void createCluster(int core, int max) {
7273
@Test(groups="short")
7374
public void should_mark_host_down_if_all_connections_fail_on_init() {
7475
// Prevent any connections on node 2.
75-
scassandra.instance(2).currentClient().disableListener();
76+
scassandra.node(2).currentClient().disableListener();
7677
createCluster(8,8);
7778

7879
// Node 2 should be in a down state while node 1 stays up.
7980
assertThat(cluster).host(2).goesDownWithin(10, SECONDS);
8081
assertThat(cluster).host(1).isUp();
8182

8283
// Node 2 should come up as soon as it is able to reconnect.
83-
scassandra.instance(2).currentClient().enableListener();
84+
scassandra.node(2).currentClient().enableListener();
8485
assertThat(cluster).host(2).comesUpWithin(2, SECONDS);
8586
}
8687

@@ -104,7 +105,7 @@ public void should_replace_control_connection_if_it_goes_down_but_host_remains_u
104105
InetSocketAddress controlSocket = (InetSocketAddress)controlConnection.channel.localAddress();
105106

106107
// Close the control connection.
107-
scassandra.instance(1).currentClient()
108+
scassandra.node(1).currentClient()
108109
.closeConnection(CLOSE, controlSocket);
109110

110111
// Sleep reconnect interval * 2 to allow time to reconnect.

0 commit comments

Comments
 (0)