@Test
public void testWeight() throws Exception {

    WeightConsistentHashLoadBalancer loadBalancer = new WeightConsistentHashLoadBalancer(null);
    SofaRequest request = new SofaRequest();
    request.setInterfaceName(ConsistentHashLoadBalancerTest.class.getName());
    request.setMethod(ConsistentHashLoadBalancerTest.class.getMethod("doSelect"));
    int size = 20;
    int total = 100000;
    List<ProviderInfo> providers = buildDiffWeightProviderList(size);
    Map<Integer, Integer> map = new HashMap(total * 2);
    for (int i = 0; i < total; i++) {
        request.setMethodArgs(new Object[] { "method" + i });
        ProviderInfo provider = loadBalancer.doSelect(request, providers);
        Integer key = provider.getPort();
        if (map.containsKey(key)) {
            int count = map.get(key);
            map.put(key, ++count);
        } else {
            map.put(key, 0);
        }
    }
    Set<Map.Entry<Integer, Integer>> set = map.entrySet();
    Iterator<Map.Entry<Integer, Integer>> iterator = set.iterator();
    while (iterator.hasNext()) {
        Map.Entry<Integer, Integer> entry = iterator.next();
        int port = entry.getKey() - 9000;
        //最大误差不超过10%
        Assert.assertTrue(entry.getValue() > 500 * port * 0.90);
        Assert.assertTrue(entry.getValue() < 500 * port * 1.10);
    }
}