kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 0fafb3d87cf72e0734b216773ef945f4556bb97e98f9b4752638dad238737acc |
|---|---|
| Author | Andrew Tolbert <andrew.tolbert@datastax.com> · 2015-09-09 20:43 -05:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-09-29 16:16 -07:00 |
| Parents | f6fcf70d35a8 |
3 files changed
modified driver-core/src/test/java/com/datastax/driver/core/AbstractReplicationStrategyTest.java +26 −25
| @@ -25,12 +25,11 @@ | ||
| 25 | 25 | |
| 26 | 26 | import com.google.common.collect.ImmutableList; |
| 27 | 27 | import com.google.common.collect.ImmutableList.Builder; |
| 28 | -import org.mockito.Mockito; | |
| 29 | 28 | |
| 30 | 29 | import static org.mockito.Mockito.mock; |
| 31 | -import static org.testng.Assert.*; | |
| 32 | - | |
| 33 | -import com.datastax.driver.core.policies.ExponentialReconnectionPolicy; | |
| 30 | +import static org.testng.Assert.assertEquals; | |
| 31 | +import static org.testng.Assert.assertNotNull; | |
| 32 | +import static org.testng.Assert.assertTrue; | |
| 34 | 33 | |
| 35 | 34 | /** |
| 36 | 35 | * Base class for replication strategy tests. Currently only supports testing |
| @@ -40,25 +39,27 @@ | ||
| 40 | 39 | |
| 41 | 40 | private static final Token.Factory partitioner = Token.getFactory("Murmur3Partitioner"); |
| 42 | 41 | |
| 42 | + private static final Cluster.Manager mockManager = mock(Cluster.Manager.class); | |
| 43 | + | |
| 43 | 44 | protected static class HostMock extends Host { |
| 44 | - private final String address; | |
| 45 | + private final InetSocketAddress address; | |
| 45 | 46 | |
| 46 | - private HostMock(String address, Cluster.Manager manager) throws UnknownHostException { | |
| 47 | - super(new InetSocketAddress(InetAddress.getByName(address), 9042), new ConvictionPolicy.DefaultConvictionPolicy.Factory(), manager); | |
| 47 | + private HostMock(InetSocketAddress address, Cluster.Manager manager) { | |
| 48 | + super(address, new ConvictionPolicy.DefaultConvictionPolicy.Factory(), manager); | |
| 48 | 49 | this.address = address; |
| 49 | 50 | } |
| 50 | 51 | |
| 51 | - private HostMock(String address, String dc, String rack, Cluster.Manager manager) throws UnknownHostException { | |
| 52 | + private HostMock(InetSocketAddress address, String dc, String rack, Cluster.Manager manager) { | |
| 52 | 53 | this(address, manager); |
| 53 | 54 | this.setLocationInfo(dc, rack); |
| 54 | 55 | } |
| 55 | 56 | |
| 56 | 57 | @Override |
| 57 | 58 | public String toString() { |
| 58 | - return address; | |
| 59 | + return address.getHostName(); | |
| 59 | 60 | } |
| 60 | 61 | |
| 61 | - public String getMockAddress() { | |
| 62 | + public InetSocketAddress getMockAddress() { | |
| 62 | 63 | return address; |
| 63 | 64 | } |
| 64 | 65 | |
| @@ -84,24 +85,16 @@ | ||
| 84 | 85 | * Convenience method to quickly create a mock host by a given address. |
| 85 | 86 | * Specified address must be accessible, otherwise a RuntimeException is thrown |
| 86 | 87 | */ |
| 87 | - protected static HostMock host(String address) { | |
| 88 | - try { | |
| 89 | - return new HostMock(address, mock(Cluster.Manager.class)); | |
| 90 | - } catch (UnknownHostException ex) { | |
| 91 | - throw new RuntimeException(ex); //wrap to avoid declarations | |
| 92 | - } | |
| 88 | + protected static HostMock host(InetSocketAddress address) { | |
| 89 | + return new HostMock(address, mockManager); | |
| 93 | 90 | } |
| 94 | 91 | |
| 95 | 92 | /** |
| 96 | 93 | * Convenience method to quickly create a mock host by the given address |
| 97 | 94 | * located in the given datacenter/rack |
| 98 | 95 | */ |
| 99 | - protected static HostMock host(String address, String dc, String rack) { | |
| 100 | - try { | |
| 101 | - return new HostMock(address, dc, rack, mock(Cluster.Manager.class)); | |
| 102 | - } catch (UnknownHostException ex) { | |
| 103 | - throw new RuntimeException(ex); //wrap to avoid declarations | |
| 104 | - } | |
| 96 | + protected static HostMock host(InetSocketAddress address, String dc, String rack) { | |
| 97 | + return new HostMock(address, dc, rack, mockManager); | |
| 105 | 98 | } |
| 106 | 99 | |
| 107 | 100 | /** |
| @@ -117,7 +110,7 @@ | ||
| 117 | 110 | * if created by the <code>host(...)</code> methods. Returns null if |
| 118 | 111 | * given host is not a mock. |
| 119 | 112 | */ |
| 120 | - protected static String mockAddress(Host host) { | |
| 113 | + protected static InetSocketAddress mockAddress(Host host) { | |
| 121 | 114 | HostMock mock = asMock(host); |
| 122 | 115 | return mock == null ? null : mock.getMockAddress(); |
| 123 | 116 | } |
| @@ -138,7 +131,7 @@ | ||
| 138 | 131 | * Asserts that the replica map for a given token contains the expected list of replica hosts. |
| 139 | 132 | * Hosts are checked in order, replica placement should be an ordered set |
| 140 | 133 | */ |
| 141 | - protected static void assertReplicaPlacement(Map<Token, Set<Host>> replicaMap, Token token, String... expected) { | |
| 134 | + protected static void assertReplicaPlacement(Map<Token, Set<Host>> replicaMap, Token token, InetSocketAddress... expected) { | |
| 142 | 135 | Set<Host> replicaSet = replicaMap.get(token); |
| 143 | 136 | assertNotNull(replicaSet); |
| 144 | 137 | assertReplicasForToken(replicaSet, expected); |
| @@ -147,7 +140,7 @@ | ||
| 147 | 140 | /** |
| 148 | 141 | * Checks if a given ordered set of replicas matches the expected list of replica hosts |
| 149 | 142 | */ |
| 150 | - protected static void assertReplicasForToken(Set<Host> replicaSet, String... expected) { | |
| 143 | + protected static void assertReplicasForToken(Set<Host> replicaSet, InetSocketAddress... expected) { | |
| 151 | 144 | final String message = "Contents of replica set: " + replicaSet + " do not match expected hosts: " + Arrays.toString(expected); |
| 152 | 145 | assertEquals(replicaSet.size(), expected.length, message); |
| 153 | 146 | |
| @@ -161,4 +154,12 @@ | ||
| 161 | 154 | assertTrue(match, message); |
| 162 | 155 | } |
| 163 | 156 | } |
| 157 | + | |
| 158 | + protected static InetSocketAddress socketAddress(String address) { | |
| 159 | + try { | |
| 160 | + return new InetSocketAddress(InetAddress.getByName(address), 9042); | |
| 161 | + } catch(UnknownHostException ex) { | |
| 162 | + throw new RuntimeException(ex); | |
| 163 | + } | |
| 164 | + } | |
| 164 | 165 | } |
modified driver-core/src/test/java/com/datastax/driver/core/NetworkTopologyStrategyTest.java +20 −11
| @@ -15,6 +15,7 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | +import java.net.InetSocketAddress; | |
| 18 | 19 | import java.util.List; |
| 19 | 20 | import java.util.Map; |
| 20 | 21 | import java.util.Set; |
| @@ -93,14 +94,14 @@ | ||
| 93 | 94 | private static final Token TOKEN18 = token("8000000000000000000"); |
| 94 | 95 | private static final Token TOKEN19 = token("9000000000000000000"); |
| 95 | 96 | |
| 96 | - private static final String IP1 = "127.0.0.101"; | |
| 97 | - private static final String IP2 = "127.0.0.102"; | |
| 98 | - private static final String IP3 = "127.0.0.103"; | |
| 99 | - private static final String IP4 = "127.0.0.104"; | |
| 100 | - private static final String IP5 = "127.0.0.105"; | |
| 101 | - private static final String IP6 = "127.0.0.106"; | |
| 102 | - private static final String IP7 = "127.0.0.107"; | |
| 103 | - private static final String IP8 = "127.0.0.108"; | |
| 97 | + private static final InetSocketAddress IP1 = socketAddress("127.0.0.101"); | |
| 98 | + private static final InetSocketAddress IP2 = socketAddress("127.0.0.102"); | |
| 99 | + private static final InetSocketAddress IP3 = socketAddress("127.0.0.103"); | |
| 100 | + private static final InetSocketAddress IP4 = socketAddress("127.0.0.104"); | |
| 101 | + private static final InetSocketAddress IP5 = socketAddress("127.0.0.105"); | |
| 102 | + private static final InetSocketAddress IP6 = socketAddress("127.0.0.106"); | |
| 103 | + private static final InetSocketAddress IP7 = socketAddress("127.0.0.107"); | |
| 104 | + private static final InetSocketAddress IP8 = socketAddress("127.0.0.108"); | |
| 104 | 105 | |
| 105 | 106 | private static final ReplicationStrategy exampleStrategy = networkTopologyStrategy(rf(DC1, 2), rf(DC2, 2)); |
| 106 | 107 | |
| @@ -110,10 +111,11 @@ | ||
| 110 | 111 | private static final Map<Token, Host> largeRingTokenToPrimary = Maps.newHashMap(); |
| 111 | 112 | static { |
| 112 | 113 | for (int i = 0; i < 100; i++) { |
| 114 | + InetSocketAddress address = socketAddress("127.0.0." + i); | |
| 113 | 115 | for (int vnodes = 0; vnodes < 256; vnodes++) { |
| 114 | 116 | Token token = token("" + ((i * 256) + vnodes)); |
| 115 | 117 | largeRing.add(token); |
| 116 | - largeRingTokenToPrimary.put(token, host("127.0.0." + i, DC1, RACK11)); | |
| 118 | + largeRingTokenToPrimary.put(token, host(address, DC1, RACK11)); | |
| 117 | 119 | } |
| 118 | 120 | } |
| 119 | 121 | } |
| @@ -591,13 +593,20 @@ | ||
| 591 | 593 | .computeTokenToReplicaMap(largeRingTokenToPrimary, largeRing); |
| 592 | 594 | assertThat(System.currentTimeMillis() - t1).isLessThan(10000); |
| 593 | 595 | |
| 596 | + InetSocketAddress currNode = null; | |
| 597 | + InetSocketAddress nextNode; | |
| 594 | 598 | for (int node = 0; node < 99; node++) { // 100th wraps so doesn't match this, check after |
| 599 | + if(currNode == null) { | |
| 600 | + currNode = socketAddress("127.0.0." + node); | |
| 601 | + } | |
| 602 | + nextNode = socketAddress("127.0.0." + (node+1)); | |
| 595 | 603 | for (int vnodes = 0; vnodes < 256; vnodes++) { |
| 596 | 604 | Token token = token("" + ((node * 256) + vnodes)); |
| 597 | - assertReplicaPlacement(replicaMap, token, "127.0.0." + node, "127.0.0." + (node + 1)); | |
| 605 | + assertReplicaPlacement(replicaMap, token, currNode, nextNode); | |
| 598 | 606 | } |
| 607 | + currNode = nextNode; | |
| 599 | 608 | } |
| 600 | - assertReplicaPlacement(replicaMap, token(""+ 99 * 256), "127.0.0.99", "127.0.0.0"); | |
| 609 | + assertReplicaPlacement(replicaMap, token(""+ 99 * 256), currNode, socketAddress("127.0.0.0")); | |
| 601 | 610 | } |
| 602 | 611 | |
| 603 | 612 |
modified driver-core/src/test/java/com/datastax/driver/core/SimpleStrategyTest.java +7 −6
| @@ -15,6 +15,7 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | +import java.net.InetSocketAddress; | |
| 18 | 19 | import java.util.List; |
| 19 | 20 | import java.util.Map; |
| 20 | 21 | import java.util.Set; |
| @@ -60,12 +61,12 @@ | ||
| 60 | 61 | private static final Token TOKEN18 = token("8000000000000000000"); |
| 61 | 62 | private static final Token TOKEN19 = token("9000000000000000000"); |
| 62 | 63 | |
| 63 | - private static final String IP1 = "127.0.0.101"; | |
| 64 | - private static final String IP2 = "127.0.0.102"; | |
| 65 | - private static final String IP3 = "127.0.0.103"; | |
| 66 | - private static final String IP4 = "127.0.0.104"; | |
| 67 | - private static final String IP5 = "127.0.0.105"; | |
| 68 | - private static final String IP6 = "127.0.0.106"; | |
| 64 | + private static final InetSocketAddress IP1 = socketAddress("127.0.0.101"); | |
| 65 | + private static final InetSocketAddress IP2 = socketAddress("127.0.0.102"); | |
| 66 | + private static final InetSocketAddress IP3 = socketAddress("127.0.0.103"); | |
| 67 | + private static final InetSocketAddress IP4 = socketAddress("127.0.0.104"); | |
| 68 | + private static final InetSocketAddress IP5 = socketAddress("127.0.0.105"); | |
| 69 | + private static final InetSocketAddress IP6 = socketAddress("127.0.0.106"); | |
| 69 | 70 | |
| 70 | 71 | private static final ReplicationStrategy exampleStrategy = simpleStrategy(3); |
| 71 | 72 |