add hash ring case
@@ -20,6 +20,11 @@ public class TestConsistentHash {
@Test
public void test() {
+ testNodeHash();
+ testNodeAdd();
+ }
+
+ private void testNodeHash() {
List<String> nodes = new ArrayList<String>();
Map<String, Integer> m = new HashMap();
for (int i=0; i<10; i++) {
@@ -42,4 +47,42 @@ public class TestConsistentHash {
System.out.println(String.format("node[%s]: %s", e.getKey(), e.getValue()));
}
}
+
+ private void testNodeAdd() {
+ List<String> nodes = new ArrayList<String>();
+ for (int i=0; i<5; i++) {
+ String guid = uuid();
+ nodes.add(guid);
+ }
+
+ ConsistentHash<String> chash = new ConsistentHash(new ApacheHash(), 1000, nodes);
+ List<String> uuids = new ArrayList<>();
+ for (int i=0; i<100; i++) {
+ uuids.add(uuid());
+ }
+
+ HashMap<String, List<String>> managedResourcesBefore = new HashMap<>();
+ for (String uuid : uuids) {
+ String node = chash.get(uuid);
+ managedResourcesBefore.computeIfAbsent(node, k -> new ArrayList<>()).add(uuid);
+ }
+ managedResourcesBefore.values().forEach(Collections::sort);
+
+ String guid = uuid();
+ chash.add(guid);
+
+ HashMap<String, List<String>> managedResourcesAfter = new HashMap<>();
+ for (String uuid : uuids) {
+ String node = chash.get(uuid);
+ managedResourcesAfter.computeIfAbsent(node, k -> new ArrayList<>()).add(uuid);
+ }
+
+ managedResourcesAfter.values().forEach(Collections::sort);
+ managedResourcesBefore.keySet().forEach(it -> {
+ System.out.println(it);
+ System.out.println(managedResourcesBefore.get(it));
+ System.out.println(managedResourcesAfter.get(it));
+ assert managedResourcesBefore.get(it).containsAll(managedResourcesAfter.get(it));
+ });
+ }
}
