ConfigurableWeightLoadBalance extends ActiveWeightLoadBalance<T> { @Override protected Referer<T> doSelect(Request request) { if (holder == emptyHolder) { return null; } RefererListCacheHolder<T> h = this.holder; Referer<T> r = h.next(); if (!r.isAvailable()) { int retryTimes = getReferers().size() - 1; for (int i = 0; i < retryTimes; i++) { r = h.next(); if (r.isAvailable()) { break; } } } if (r.isAvailable()) { return r; } else { noAvailableReferer(); return null; } }  @SuppressWarnings("unchecked") @Override void onRefresh(List<Referer<T>> referers); @Override void setWeightString(String weightString);  }
@Test public void testDoSelect() { int[] groupWeight = new int[] {2, 3, 5}; Map<String, AtomicInteger> counter = generate(3, groupWeight, new int[] {3, 4, 5}); for (int j = 0; j < 100; j++) { int size = 100; for (int i = 0; i < size; i++) { Referer referer = balance.doSelect(new DefaultRequest()); String group = referer.getServiceUrl().getGroup(); counter.get(group).incrementAndGet(); } for (String key : counter.keySet()) { float total = size * (j + 1); float ratio = counter.get(key).get() * 10 / total; int weight = groupWeight[Integer.parseInt(key.substring("group".length()))]; Assert.assertTrue(Math.abs(weight - ratio) < 2); } } }