Elffuss BraidHub

kikocis/java-driver public

Don't assume single host + expand tests (JAVA-684).
Changeseta30aaa55e581b3345cdc852cafd22100080e879d6d9c8ea225b032b38b8f4553
Authorolim7t <omichallat+github@gmail.com> · 2015-03-11 09:05 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2015-03-11 09:05 +01:00
Parentsed2d579048cd

Browse the files at this changeset

3 files changed

modified driver-core/src/main/java/com/datastax/driver/core/Metadata.java +5 −3
@@ -513,9 +513,11 @@
513513
514514 Map<Host, Set<TokenRange>> ksRanges;
515515 if (ring.size() == 1) {
516- // The single range is ]minToken,minToken] which does not necessarily match the host's token
517- assert hosts.size() == 1;
518- ksRanges = ImmutableMap.of(hosts.iterator().next(), tokenRanges);
516+ // We forced the single range to ]minToken,minToken], make sure to use that instead of relying on the host's token
517+ ImmutableMap.Builder<Host, Set<TokenRange>> builder = ImmutableMap.builder();
518+ for (Host host : allTokens.keySet())
519+ builder.put(host, tokenRanges);
520+ ksRanges = builder.build();
519521 } else {
520522 ksRanges = computeHostsToRangesMap(tokenRanges, ksTokens, hosts.size());
521523 }
modified driver-core/src/test/java/com/datastax/driver/core/CCMBridge.java +1 −1
@@ -178,7 +178,7 @@
178178
179179 public void bootstrapNode(int n, String dc) {
180180 if (dc == null)
181- execute("ccm add node%d -i %s%d -j %d -b -s", n, IP_PREFIX, n, 7000 + 100*n);
181+ execute("ccm add node%d -i %s%d -j %d -r %d -b -s", n, IP_PREFIX, n, 7000 + 100*n, 8000 + 100*n);
182182 else
183183 execute("ccm add node%d -i %s%d -j %d -b -d %s -s", n, IP_PREFIX, n, 7000 + 100*n, dc);
184184 execute("ccm node%d start --wait-other-notice --wait-for-binary-proto", n);
modified driver-core/src/test/java/com/datastax/driver/core/SingleTokenIntegrationTest.java +16 −5
@@ -15,10 +15,13 @@
1515 */
1616 package com.datastax.driver.core;
1717
18+import java.nio.ByteBuffer;
1819 import java.util.Set;
1920
2021 import org.testng.annotations.Test;
2122
23+import com.datastax.driver.core.utils.Bytes;
24+
2225 import static com.datastax.driver.core.Assertions.assertThat;
2326
2427 public class SingleTokenIntegrationTest {
@@ -32,7 +35,7 @@
3235
3336 try {
3437 ccm = CCMBridge.create("test");
35- // Set an initial token that is not the minimum
38+ // force the initial token to a non-min value to validate that the single range will always be ]minToken, minToken]
3639 ccm.updateConfig("initial_token", "1");
3740 ccm.bootstrapNode(1);
3841 ccm.start();
@@ -42,7 +45,9 @@
4245 Session session = cluster.connect();
4346 session.execute("create keyspace test with replication = {'class': 'SimpleStrategy', 'replication_factor': 1}");
4447
45- Set<TokenRange> tokenRanges = cluster.getMetadata().getTokenRanges();
48+ Metadata metadata = cluster.getMetadata();
49+
50+ Set<TokenRange> tokenRanges = metadata.getTokenRanges();
4651 assertThat(tokenRanges).hasSize(1);
4752 TokenRange tokenRange = tokenRanges.iterator().next();
4853 assertThat(tokenRange)
@@ -51,10 +56,16 @@
5156 .isNotEmpty()
5257 .isNotWrappedAround();
5358
54- Set<Host> rangeHosts = cluster.getMetadata().getReplicas("test", tokenRange);
55- assertThat(rangeHosts)
56- .containsOnly(TestUtils.findHost(cluster, 1));
59+ Set<Host> hostsForRange = metadata.getReplicas("test", tokenRange);
60+ Host host1 = TestUtils.findHost(cluster, 1);
61+ assertThat(hostsForRange).containsOnly(host1);
62+
63+ ByteBuffer randomPartitionKey = Bytes.fromHexString("0xCAFEBABE");
64+ Set<Host> hostsForKey = metadata.getReplicas("test", randomPartitionKey);
65+ assertThat(hostsForKey).containsOnly(host1);
5766
67+ Set<TokenRange> rangesForHost = metadata.getTokenRanges("test", host1);
68+ assertThat(rangesForHost).containsOnly(tokenRange);
5869 } finally {
5970 if (cluster != null)
6071 cluster.close();