@Test public void testAllElementsCanBeSelected() { RandomAlgorithm algorithm = new RandomAlgorithm(); List<ServiceEndPoint> endPoints = Lists.newArrayList( mock(ServiceEndPoint.class), mock(ServiceEndPoint.class), mock(ServiceEndPoint.class), mock(ServiceEndPoint.class), mock(ServiceEndPoint.class) ); Set<ServiceEndPoint> seen = Sets.newIdentityHashSet(); for (int i = 0; i < 1000; i++) { seen.add(algorithm.choose(endPoints, null)); } assertEquals(endPoints.size(), seen.size()); }
@Override public ServiceEndPoint choose(Iterable<ServiceEndPoint> endPoints, ServicePoolStatistics statistics) { Preconditions.checkNotNull(endPoints); Iterator<ServiceEndPoint> iter = endPoints.iterator(); if (!iter.hasNext()) { return null; } List<ServiceEndPoint> list = Lists.newArrayList(iter); if (list.size() == 1) { return list.get(0); } return list.get(_rnd.nextInt(list.size())); }
RandomAlgorithm implements LoadBalanceAlgorithm { @Override public ServiceEndPoint choose(Iterable<ServiceEndPoint> endPoints, ServicePoolStatistics statistics) { Preconditions.checkNotNull(endPoints); Iterator<ServiceEndPoint> iter = endPoints.iterator(); if (!iter.hasNext()) { return null; } List<ServiceEndPoint> list = Lists.newArrayList(iter); if (list.size() == 1) { return list.get(0); } return list.get(_rnd.nextInt(list.size())); } }
RandomAlgorithm implements LoadBalanceAlgorithm { @Override public ServiceEndPoint choose(Iterable<ServiceEndPoint> endPoints, ServicePoolStatistics statistics) { Preconditions.checkNotNull(endPoints); Iterator<ServiceEndPoint> iter = endPoints.iterator(); if (!iter.hasNext()) { return null; } List<ServiceEndPoint> list = Lists.newArrayList(iter); if (list.size() == 1) { return list.get(0); } return list.get(_rnd.nextInt(list.size())); }  }
RandomAlgorithm implements LoadBalanceAlgorithm { @Override public ServiceEndPoint choose(Iterable<ServiceEndPoint> endPoints, ServicePoolStatistics statistics) { Preconditions.checkNotNull(endPoints); Iterator<ServiceEndPoint> iter = endPoints.iterator(); if (!iter.hasNext()) { return null; } List<ServiceEndPoint> list = Lists.newArrayList(iter); if (list.size() == 1) { return list.get(0); } return list.get(_rnd.nextInt(list.size())); }  @Override ServiceEndPoint choose(Iterable<ServiceEndPoint> endPoints, ServicePoolStatistics statistics); }
RandomAlgorithm implements LoadBalanceAlgorithm { @Override public ServiceEndPoint choose(Iterable<ServiceEndPoint> endPoints, ServicePoolStatistics statistics) { Preconditions.checkNotNull(endPoints); Iterator<ServiceEndPoint> iter = endPoints.iterator(); if (!iter.hasNext()) { return null; } List<ServiceEndPoint> list = Lists.newArrayList(iter); if (list.size() == 1) { return list.get(0); } return list.get(_rnd.nextInt(list.size())); }  @Override ServiceEndPoint choose(Iterable<ServiceEndPoint> endPoints, ServicePoolStatistics statistics);  }