Skip to content

Commit d22241b

Browse files
fruchavelanarius
authored andcommitted
ShardingInfoTest: unittest for shardId calculations
1 parent d3a591e commit d22241b

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright ScyllaDB, 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+
17+
package com.datastax.driver.core;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import java.nio.ByteBuffer;
22+
import java.util.Collections;
23+
import java.util.HashMap;
24+
import java.util.List;
25+
import java.util.Map;
26+
import org.testng.annotations.Test;
27+
28+
public class ShardingInfoTest {
29+
30+
@Test(groups = "unit")
31+
public void shardIdCalculationTest() {
32+
// Verify that our Murmur hash calculates the correct shard for specific keys.
33+
Token.Factory factory = Token.M3PToken.FACTORY;
34+
35+
Map<String, List<String>> params =
36+
new HashMap<String, List<String>>() {
37+
{
38+
put("SCYLLA_SHARD", Collections.singletonList("1"));
39+
put("SCYLLA_NR_SHARDS", Collections.singletonList("12"));
40+
put(
41+
"SCYLLA_PARTITIONER",
42+
Collections.singletonList("org.apache.cassandra.dht.Murmur3Partitioner"));
43+
put("SCYLLA_SHARDING_ALGORITHM", Collections.singletonList("biased-token-round-robin"));
44+
put("SCYLLA_SHARDING_IGNORE_MSB", Collections.singletonList("12"));
45+
}
46+
};
47+
ShardingInfo.ConnectionShardingInfo sharding = ShardingInfo.parseShardingInfo(params);
48+
assertThat(sharding).isNotNull();
49+
assertThat(sharding.shardId).isEqualTo(1);
50+
51+
Token token1 =
52+
factory.hash(
53+
ByteBuffer.wrap(
54+
new byte[] {
55+
'a',
56+
}));
57+
assertThat(sharding.shardingInfo.shardId(token1)).isEqualTo(4);
58+
59+
Token token2 =
60+
factory.hash(
61+
ByteBuffer.wrap(
62+
new byte[] {
63+
'b',
64+
}));
65+
assertThat(sharding.shardingInfo.shardId(token2)).isEqualTo(6);
66+
67+
Token token3 =
68+
factory.hash(
69+
ByteBuffer.wrap(
70+
new byte[] {
71+
'c',
72+
}));
73+
assertThat(sharding.shardingInfo.shardId(token3)).isEqualTo(6);
74+
75+
Token token4 =
76+
factory.hash(
77+
ByteBuffer.wrap(
78+
new byte[] {
79+
'e',
80+
}));
81+
assertThat(sharding.shardingInfo.shardId(token4)).isEqualTo(4);
82+
83+
Token token5 =
84+
factory.hash(
85+
ByteBuffer.wrap(
86+
new byte[] {
87+
'1', '0', '0', '0', '0', '0',
88+
}));
89+
assertThat(sharding.shardingInfo.shardId(token5)).isEqualTo(2);
90+
}
91+
}

0 commit comments

Comments
 (0)