package org.zstack.utils.test;

import org.junit.Test;
import org.zstack.utils.hash.ApacheHash;
import org.zstack.utils.hash.ConsistentHash;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: frank
 * Time: 10:14 PM
 * To change this template use File | Settings | File Templates.
 */
public class TestConsistentHash {

    private String uuid() {
        return UUID.randomUUID().toString();
    }

    @Test
    public void test() {
        List<String> nodes = new ArrayList<String>();
        Map<String, Integer> m = new HashMap();
        for (int i=0; i<10; i++) {
            String guid = uuid();
            nodes.add(guid);
            m.put(guid, new Integer(0));
        }

        ConsistentHash<String> chash = new ConsistentHash(new ApacheHash(), 1000, nodes);

        for (int i=0; i<100000; i++) {
            String msg = uuid();
            String node = chash.get(msg);
            Integer count = (Integer) m.get(node);
            int c = count + 1;
            m.put(node, c);
        }

        for (Map.Entry<String, Integer> e : m.entrySet()) {
            System.out.println(String.format("node[%s]: %s", e.getKey(), e.getValue()));
        }
    }
}

@@ -439,24 +439,112 @@
 id test() %7B%0A
+        testNodeHash();%0A        testNodeAdd();%0A    %7D%0A%0A    private void testNodeHash() %7B%0A
         List
@@ -1273,11 +1273,1467 @@
  %7D%0A    %7D
+%0A%0A    private void testNodeAdd() %7B%0A        List%3CString%3E nodes = new ArrayList%3CString%3E();%0A        for (int i=0; i%3C5; i++) %7B%0A            String guid = uuid();%0A            nodes.add(guid);%0A        %7D%0A%0A        ConsistentHash%3CString%3E chash = new ConsistentHash(new ApacheHash(), 1000, nodes);%0A        List%3CString%3E uuids = new ArrayList%3C%3E();%0A        for (int i=0; i%3C100; i++) %7B%0A            uuids.add(uuid());%0A        %7D%0A%0A        HashMap%3CString, List%3CString%3E%3E managedResourcesBefore = new HashMap%3C%3E();%0A        for (String uuid : uuids) %7B%0A            String node = chash.get(uuid);%0A            managedResourcesBefore.computeIfAbsent(node, k -%3E new ArrayList%3C%3E()).add(uuid);%0A        %7D%0A        managedResourcesBefore.values().forEach(Collections::sort);%0A%0A        String guid = uuid();%0A        chash.add(guid);%0A%0A        HashMap%3CString, List%3CString%3E%3E managedResourcesAfter = new HashMap%3C%3E();%0A        for (String uuid : uuids) %7B%0A            String node = chash.get(uuid);%0A            managedResourcesAfter.computeIfAbsent(node, k -%3E new ArrayList%3C%3E()).add(uuid);%0A        %7D%0A%0A        managedResourcesAfter.values().forEach(Collections::sort);%0A        managedResourcesBefore.keySet().forEach(it -%3E %7B%0A            System.out.println(it);%0A            System.out.println(managedResourcesBefore.get(it));%0A            System.out.println(managedResourcesAfter.get(it));%0A            assert managedResourcesBefore.get(it).containsAll(managedResourcesAfter.get(it));%0A        %7D);%0A    %7D
 %0A%7D%0A

add hash ring case