kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 251552404d7691cea3d31dc3aa8a830c065131cba999967a6932dc253b0bb3f0 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-10-13 11:09 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-10-13 11:09 +02:00 |
| Parents | 20675bac40ab |
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 @@ | ||
| 30 | 30 | import java.io.File; |
| 31 | 31 | import java.io.IOException; |
| 32 | 32 | import java.io.InputStreamReader; |
| 33 | +import java.net.InetAddress; | |
| 33 | 34 | import java.net.InetSocketAddress; |
| 35 | +import java.net.Socket; | |
| 36 | +import java.net.UnknownHostException; | |
| 34 | 37 | import java.util.Collection; |
| 35 | 38 | import java.util.Map; |
| 39 | +import java.util.concurrent.TimeUnit; | |
| 40 | + | |
| 41 | +import static org.testng.Assert.fail; | |
| 36 | 42 | |
| 37 | 43 | import static com.google.common.base.Preconditions.checkArgument; |
| 38 | 44 | |
| @@ -226,6 +232,68 @@ | ||
| 226 | 232 | } |
| 227 | 233 | } |
| 228 | 234 | |
| 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 | + | |
| 229 | 297 | public static String ipOfNode(int nodeNumber) { |
| 230 | 298 | return IP_PREFIX + Integer.toString(nodeNumber); |
| 231 | 299 | } |
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 @@ | ||
| 1 | 1 | package com.datastax.driver.core; |
| 2 | 2 | |
| 3 | -import java.io.IOException; | |
| 4 | -import java.net.InetAddress; | |
| 5 | -import java.net.Socket; | |
| 6 | 3 | import java.util.concurrent.TimeUnit; |
| 7 | 4 | |
| 8 | 5 | import org.slf4j.Logger; |
| @@ -31,7 +28,7 @@ | ||
| 31 | 28 | mainCcm = CCMBridge.create("main", 3); |
| 32 | 29 | // node1 will be our "recommissionned" node, for now we just stop it so that it stays in the peers table. |
| 33 | 30 | mainCcm.stop(1); |
| 34 | - waitForDown(1); | |
| 31 | + mainCcm.waitForDown(1); | |
| 35 | 32 | |
| 36 | 33 | // Now start the driver that will connect to node2 and node3, and consider node1 down |
| 37 | 34 | Cluster mainCluster = Cluster.builder().addContactPoint(CCMBridge.IP_PREFIX + "2").build(); |
| @@ -41,7 +38,7 @@ | ||
| 41 | 38 | |
| 42 | 39 | // Start another ccm that will reuse node1's address |
| 43 | 40 | otherCcm = CCMBridge.create("other", 1); |
| 44 | - waitForUp(1); | |
| 41 | + otherCcm.waitForUp(1); | |
| 45 | 42 | |
| 46 | 43 | // Give the driver the time to notice the node is back up and try to connect to it. |
| 47 | 44 | TimeUnit.SECONDS.sleep(32); |
| @@ -56,7 +53,7 @@ | ||
| 56 | 53 | public void testControlConnection() throws Exception { |
| 57 | 54 | mainCcm = CCMBridge.create("main", 2); |
| 58 | 55 | mainCcm.stop(1); |
| 59 | - waitForDown(1); | |
| 56 | + mainCcm.waitForDown(1); | |
| 60 | 57 | |
| 61 | 58 | // Start the driver, the control connection will be on node2 |
| 62 | 59 | Cluster mainCluster = Cluster.builder().addContactPoint(CCMBridge.IP_PREFIX + "2").build(); |
| @@ -65,7 +62,7 @@ | ||
| 65 | 62 | |
| 66 | 63 | // Start another ccm that will reuse node1's address |
| 67 | 64 | otherCcm = CCMBridge.create("other", 1); |
| 68 | - waitForUp(1); | |
| 65 | + otherCcm.waitForUp(1); | |
| 69 | 66 | |
| 70 | 67 | // Stop node2, the control connection gets defunct |
| 71 | 68 | mainCcm.stop(2); |
| @@ -113,53 +110,4 @@ | ||
| 113 | 110 | TimeUnit.SECONDS.sleep(interval); |
| 114 | 111 | } |
| 115 | 112 | } |
| 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 | - } | |
| 165 | 113 | } |
| 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 @@ | ||
| 1 | 1 | package com.datastax.driver.core.policies; |
| 2 | 2 | |
| 3 | 3 | import org.testng.annotations.Test; |
| 4 | - | |
| 5 | 4 | import static org.testng.Assert.assertEquals; |
| 6 | -import static org.testng.Assert.fail; | |
| 7 | 5 | |
| 8 | 6 | import com.datastax.driver.core.CCMBridge; |
| 9 | 7 | import com.datastax.driver.core.Cluster; |
| 8 | +import com.datastax.driver.core.ClusterAssert; | |
| 10 | 9 | import com.datastax.driver.core.Host; |
| 11 | 10 | |
| 12 | 11 | public class DCAwareRoundRobinPolicyTest { |
| @@ -30,7 +29,7 @@ | ||
| 30 | 29 | cluster.init(); |
| 31 | 30 | |
| 32 | 31 | // 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); | |
| 34 | 33 | assertEquals(policy.localDc, host1.getDatacenter()); |
| 35 | 34 | |
| 36 | 35 | } finally { |
| @@ -40,13 +39,4 @@ | ||
| 40 | 39 | ccm.remove(); |
| 41 | 40 | } |
| 42 | 41 | } |
| 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 | - } | |
| 52 | 42 | } |