@Test public void tailIndicesAreReturnedUniformly() { PatternStrategy strategy = new RoundRobinLoadBalancer(3, 3); int expectedFirst = 0; int secondTotal = 0; int thirdTotal = 0; for (int i = 0; i < 5000; ++i) { Iterator<Integer> indices = strategy.nextIndices().iterator(); int first = indices.next(); assertEquals(expectedFirst % 3, first); int second = indices.next(); int third = indices.next(); assertFalse(indices.hasNext()); assertNotEquals(first, second); assertNotEquals(first, third); assertNotEquals(second, third); secondTotal += second; thirdTotal += third; ++expectedFirst; } double secondMean = (double) secondTotal / 5000; double thirdMean = (double) thirdTotal / 5000; String message = "Concerning distribution of indices returned"; assertTrue(message, 0.15 > Math.abs(secondMean - thirdMean)); }
@Override public Iterable<Integer> nextIndices() { long index = counter.getAndIncrement(); if (index >= flipPoint) { resetCounter(index); } SingleReaderArrayIterable iterable = new SingleReaderArrayIterable(maxAcquireAttempts); Integer[] orderToTry = iterable.getIndices(); for (int i = 0; i < maxAcquireAttempts; ++i) { orderToTry[i] = (int) ((index + i) % size); } shuffleTail(orderToTry); return iterable; }
RoundRobinLoadBalancer implements PatternStrategy { @Override public Iterable<Integer> nextIndices() { long index = counter.getAndIncrement(); if (index >= flipPoint) { resetCounter(index); } SingleReaderArrayIterable iterable = new SingleReaderArrayIterable(maxAcquireAttempts); Integer[] orderToTry = iterable.getIndices(); for (int i = 0; i < maxAcquireAttempts; ++i) { orderToTry[i] = (int) ((index + i) % size); } shuffleTail(orderToTry); return iterable; } }
RoundRobinLoadBalancer implements PatternStrategy { @Override public Iterable<Integer> nextIndices() { long index = counter.getAndIncrement(); if (index >= flipPoint) { resetCounter(index); } SingleReaderArrayIterable iterable = new SingleReaderArrayIterable(maxAcquireAttempts); Integer[] orderToTry = iterable.getIndices(); for (int i = 0; i < maxAcquireAttempts; ++i) { orderToTry[i] = (int) ((index + i) % size); } shuffleTail(orderToTry); return iterable; } RoundRobinLoadBalancer(int size);  RoundRobinLoadBalancer(int size, int maxAcquireAttempts);  RoundRobinLoadBalancer(int size, int maxAcquireAttempts, AtomicLong counter); }
RoundRobinLoadBalancer implements PatternStrategy { @Override public Iterable<Integer> nextIndices() { long index = counter.getAndIncrement(); if (index >= flipPoint) { resetCounter(index); } SingleReaderArrayIterable iterable = new SingleReaderArrayIterable(maxAcquireAttempts); Integer[] orderToTry = iterable.getIndices(); for (int i = 0; i < maxAcquireAttempts; ++i) { orderToTry[i] = (int) ((index + i) % size); } shuffleTail(orderToTry); return iterable; } RoundRobinLoadBalancer(int size);  RoundRobinLoadBalancer(int size, int maxAcquireAttempts);  RoundRobinLoadBalancer(int size, int maxAcquireAttempts, AtomicLong counter); @Override Iterable<Integer> nextIndices(); @Override int acquireCount(); }
RoundRobinLoadBalancer implements PatternStrategy { @Override public Iterable<Integer> nextIndices() { long index = counter.getAndIncrement(); if (index >= flipPoint) { resetCounter(index); } SingleReaderArrayIterable iterable = new SingleReaderArrayIterable(maxAcquireAttempts); Integer[] orderToTry = iterable.getIndices(); for (int i = 0; i < maxAcquireAttempts; ++i) { orderToTry[i] = (int) ((index + i) % size); } shuffleTail(orderToTry); return iterable; } RoundRobinLoadBalancer(int size);  RoundRobinLoadBalancer(int size, int maxAcquireAttempts);  RoundRobinLoadBalancer(int size, int maxAcquireAttempts, AtomicLong counter); @Override Iterable<Integer> nextIndices(); @Override int acquireCount();  }