Elffuss BraidHub

kikocis/java-driver public

Refactor test classes.

- introduce custom AssertJ assertion for Cluster
- move waitFor methods to CCMBridge
Changeset251552404d7691cea3d31dc3aa8a830c065131cba999967a6932dc253b0bb3f0
Authorolim7t <omichallat+github@gmail.com> · 2014-10-13 11:09 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2014-10-13 11:09 +02:00
Parents20675bac40ab

Browse the files at this changeset

5 files changed

added driver-core/src/test/java/com/datastax/driver/core/Assertions.java +10 −0
@@ -0,0 +1,10 @@
1+package com.datastax.driver.core;
2+
3+/**
4+ * Augment AssertJ with custom assertions for the Java driver.
5+ */
6+public class Assertions extends org.assertj.core.api.Assertions{
7+ public static ClusterAssert assertThat(Cluster cluster) {
8+ return new ClusterAssert(cluster);
9+ }
10+}
modified driver-core/src/test/java/com/datastax/driver/core/CCMBridge.java +68 −0
@@ -30,9 +30,15 @@
3030 import java.io.File;
3131 import java.io.IOException;
3232 import java.io.InputStreamReader;
33+import java.net.InetAddress;
3334 import java.net.InetSocketAddress;
35+import java.net.Socket;
36+import java.net.UnknownHostException;
3437 import java.util.Collection;
3538 import java.util.Map;
39+import java.util.concurrent.TimeUnit;
40+
41+import static org.testng.Assert.fail;
3642
3743 import static com.google.common.base.Preconditions.checkArgument;
3844
@@ -226,6 +232,68 @@
226232 }
227233 }
228234
235+ /**
236+ * Waits for a host to be up by pinging the TCP socket directly, without using the Java driver's API.
237+ */
238+ public void waitForUp(int node) {
239+ try {
240+ InetAddress address = InetAddress.getByName(ipOfNode(node));
241+ CCMBridge.busyWaitForPort(address, 9042, true);
242+ } catch (UnknownHostException e) {
243+ fail("Unknown host " + ipOfNode(node) + "( node " + node + " of CCMBridge)");
244+ }
245+ }
246+
247+ /**
248+ * Waits for a host to be down by pinging the TCP socket directly, without using the Java driver's API.
249+ */
250+ public void waitForDown(int node) {
251+ try {
252+ InetAddress address = InetAddress.getByName(ipOfNode(node));
253+ CCMBridge.busyWaitForPort(address, 9042, false);
254+ } catch (UnknownHostException e) {
255+ fail("Unknown host " + ipOfNode(node) + "( node " + node + " of CCMBridge)");
256+ }
257+ }
258+
259+ private static void busyWaitForPort(InetAddress address, int port, boolean expectedConnectionState) {
260+ long maxAcceptableWaitTime = TimeUnit.SECONDS.toMillis(10);
261+ long waitQuantum = TimeUnit.MILLISECONDS.toMillis(500);
262+ long waitTimeSoFar = 0;
263+ boolean connectionState = !expectedConnectionState;
264+
265+ while (connectionState != expectedConnectionState && waitTimeSoFar < maxAcceptableWaitTime) {
266+ connectionState = CCMBridge.pingPort(address, port);
267+ try {
268+ Thread.sleep(waitQuantum);
269+ waitTimeSoFar += waitQuantum;
270+ } catch (InterruptedException e) {
271+ throw new RuntimeException("Interrupted while pinging " + address + ":" + port, e);
272+ }
273+ }
274+ }
275+
276+ private static boolean pingPort(InetAddress address, int port) {
277+ logger.debug("Trying {}:{}...", address, port);
278+ boolean connectionSuccessful = false;
279+ Socket socket = null;
280+ try {
281+ socket = new Socket(address, port);
282+ connectionSuccessful = true;
283+ logger.debug("Successfully connected");
284+ } catch (IOException e) {
285+ logger.debug("Connection failed");
286+ } finally {
287+ if (socket != null)
288+ try {
289+ socket.close();
290+ } catch (IOException e) {
291+ logger.warn("Error closing socket to " + address);
292+ }
293+ }
294+ return connectionSuccessful;
295+ }
296+
229297 public static String ipOfNode(int nodeNumber) {
230298 return IP_PREFIX + Integer.toString(nodeNumber);
231299 }
added driver-core/src/test/java/com/datastax/driver/core/ClusterAssert.java +29 −0
@@ -0,0 +1,29 @@
1+package com.datastax.driver.core;
2+
3+import static org.assertj.core.api.Assertions.assertThat;
4+import static org.testng.Assert.fail;
5+import org.assertj.core.api.AbstractAssert;
6+
7+public class ClusterAssert extends AbstractAssert<ClusterAssert, Cluster> {
8+ protected ClusterAssert(Cluster actual) {
9+ super(actual, ClusterAssert.class);
10+ }
11+
12+ public ClusterAssert usesControlHost(int node) {
13+ String expectedAddress = CCMBridge.ipOfNode(node);
14+ Host controlHost = actual.manager.controlConnection.connectedHost();
15+ assertThat(controlHost.getAddress().getHostAddress()).isEqualTo(expectedAddress);
16+ return this;
17+ }
18+
19+ /** Utility method to find the {@code Host} object corresponding to a node in a cluster. */
20+ public static Host findHost(Cluster cluster, int node) {
21+ String address = CCMBridge.ipOfNode(node);
22+ for (Host host : cluster.getMetadata().getAllHosts()) {
23+ if (host.getAddress().getHostAddress().equals(address))
24+ return host;
25+ }
26+ fail(address + " not found in cluster metadata");
27+ return null; // never reached
28+ }
29+}
modified driver-core/src/test/java/com/datastax/driver/core/RecommissionnedNodeTest.java +4 −56
@@ -1,8 +1,5 @@
11 package com.datastax.driver.core;
22
3-import java.io.IOException;
4-import java.net.InetAddress;
5-import java.net.Socket;
63 import java.util.concurrent.TimeUnit;
74
85 import org.slf4j.Logger;
@@ -31,7 +28,7 @@
3128 mainCcm = CCMBridge.create("main", 3);
3229 // node1 will be our "recommissionned" node, for now we just stop it so that it stays in the peers table.
3330 mainCcm.stop(1);
34- waitForDown(1);
31+ mainCcm.waitForDown(1);
3532
3633 // Now start the driver that will connect to node2 and node3, and consider node1 down
3734 Cluster mainCluster = Cluster.builder().addContactPoint(CCMBridge.IP_PREFIX + "2").build();
@@ -41,7 +38,7 @@
4138
4239 // Start another ccm that will reuse node1's address
4340 otherCcm = CCMBridge.create("other", 1);
44- waitForUp(1);
41+ otherCcm.waitForUp(1);
4542
4643 // Give the driver the time to notice the node is back up and try to connect to it.
4744 TimeUnit.SECONDS.sleep(32);
@@ -56,7 +53,7 @@
5653 public void testControlConnection() throws Exception {
5754 mainCcm = CCMBridge.create("main", 2);
5855 mainCcm.stop(1);
59- waitForDown(1);
56+ mainCcm.waitForDown(1);
6057
6158 // Start the driver, the control connection will be on node2
6259 Cluster mainCluster = Cluster.builder().addContactPoint(CCMBridge.IP_PREFIX + "2").build();
@@ -65,7 +62,7 @@
6562
6663 // Start another ccm that will reuse node1's address
6764 otherCcm = CCMBridge.create("other", 1);
68- waitForUp(1);
65+ otherCcm.waitForUp(1);
6966
7067 // Stop node2, the control connection gets defunct
7168 mainCcm.stop(2);
@@ -113,53 +110,4 @@
113110 TimeUnit.SECONDS.sleep(interval);
114111 }
115112 }
116-
117- // Methods to wait for a node's state by pinging the TCP socket directly, instead of using a Cluster instance.
118- // This decouples these checks from the system under test, and avoids CASSANDRA-7510.
119- // Contributed by Pierre Laporte.
120- // TODO: move to a utility class, generalize their use to other tests.
121-
122- private static void waitForUp(int node) throws Exception {
123- InetAddress address = InetAddress.getByName(CCMBridge.IP_PREFIX + Integer.toString(node));
124- busyWaitForPort(address, 9042, true);
125- }
126-
127- private static void waitForDown(int node) throws Exception {
128- InetAddress address = InetAddress.getByName(CCMBridge.IP_PREFIX + Integer.toString(node));
129- busyWaitForPort(address, 9042, false);
130- }
131-
132- private static void busyWaitForPort(InetAddress address, int port, boolean expectedConnectionState) throws Exception {
133- long maxAcceptableWaitTime = TimeUnit.SECONDS.toMillis(10);
134- long waitQuantum = TimeUnit.MILLISECONDS.toMillis(500);
135- long waitTimeSoFar = 0;
136- boolean connectionState = !expectedConnectionState;
137-
138- while (connectionState != expectedConnectionState && waitTimeSoFar < maxAcceptableWaitTime) {
139- connectionState = pingPort(address, port);
140- try {
141- Thread.sleep(waitQuantum);
142- waitTimeSoFar += waitQuantum;
143- } catch (InterruptedException e) {
144- throw new RuntimeException("Interrupted while pinging " + address + ":" + port, e);
145- }
146- }
147- }
148-
149- private static boolean pingPort(InetAddress address, int port) throws Exception {
150- logger.debug("Trying {}:{}...", address, port);
151- boolean connectionSuccessful = false;
152- Socket socket = null;
153- try {
154- socket = new Socket(address, port);
155- connectionSuccessful = true;
156- logger.debug("Successfully connected");
157- } catch (IOException e) {
158- logger.debug("Connection failed");
159- } finally {
160- if (socket != null)
161- socket.close();
162- }
163- return connectionSuccessful;
164- }
165113 }
No newline at the end of the file
modified driver-core/src/test/java/com/datastax/driver/core/policies/DCAwareRoundRobinPolicyTest.java +2 −12
@@ -1,12 +1,11 @@
11 package com.datastax.driver.core.policies;
22
33 import org.testng.annotations.Test;
4-
54 import static org.testng.Assert.assertEquals;
6-import static org.testng.Assert.fail;
75
86 import com.datastax.driver.core.CCMBridge;
97 import com.datastax.driver.core.Cluster;
8+import com.datastax.driver.core.ClusterAssert;
109 import com.datastax.driver.core.Host;
1110
1211 public class DCAwareRoundRobinPolicyTest {
@@ -30,7 +29,7 @@
3029 cluster.init();
3130
3231 // Policy's localDC should be the one from the contact point
33- Host host1 = findHost(cluster, CCMBridge.ipOfNode(1));
32+ Host host1 = ClusterAssert.findHost(cluster, 1);
3433 assertEquals(policy.localDc, host1.getDatacenter());
3534
3635 } finally {
@@ -40,13 +39,4 @@
4039 ccm.remove();
4140 }
4241 }
43-
44- private static Host findHost(Cluster cluster, String ip) {
45- for (Host host : cluster.getMetadata().getAllHosts()) {
46- if (host.getAddress().getHostAddress().equals(ip))
47- return host;
48- }
49- fail(ip + " not found in cluster metadata");
50- return null;
51- }
5242 }