### Question:
SlavePriorities { public static SlavePriority roundRobin() { return new SlavePriority() { final AtomicInteger index = new AtomicInteger(); @Override public Iterable<Slave> prioritize( final Iterable<Slave> slaves ) { final List<Slave> slaveList = sortSlaves( slaves, true ); if ( slaveList.isEmpty() ) { return Iterables.empty(); } return () -> new PrefetchingIterator<Slave>() { private int start = index.getAndIncrement() % slaveList.size(); private int count; @Override protected Slave fetchNextOrNull() { int id = count++; return id <= slaveList.size() ? slaveList.get( (start + id) % slaveList.size() ) : null; } }; } }; } private  SlavePriorities(); static SlavePriority givenOrder(); static SlavePriority roundRobin(); static SlavePriority fixedDescending(); static SlavePriority fixedAscending();  }### Answer:
@Test public void roundRobinWithTwoSlavesAndPushFactorTwo() { SlavePriority roundRobin = SlavePriorities.roundRobin(); Iterator<Slave> slaves = roundRobin.prioritize( slaves( 2, 3 ) ).iterator(); assertEquals( 2, slaves.next().getServerId() ); assertEquals( 3, slaves.next().getServerId() ); }
@Test public void roundRobinWithTwoSlavesAndPushFactorOne() { SlavePriority roundRobin = SlavePriorities.roundRobin(); Slave slave1 = roundRobin.prioritize( slaves( 2, 3 ) ).iterator().next(); Slave slave2 = roundRobin.prioritize( slaves( 2, 3 ) ).iterator().next(); assertEquals( 2, slave1.getServerId() ); assertEquals( 3, slave2.getServerId() ); }
@Test public void roundRobinWithTwoSlavesAndPushFactorOneWhenSlaveIsAdded() { SlavePriority roundRobin = SlavePriorities.roundRobin(); Slave slave1 = roundRobin.prioritize( slaves( 2, 3 ) ).iterator().next(); Slave slave2 = roundRobin.prioritize( slaves( 2, 3 ) ).iterator().next(); Slave slave3 = roundRobin.prioritize( slaves( 2, 3, 4 ) ).iterator().next(); assertEquals( 2, slave1.getServerId() ); assertEquals( 3, slave2.getServerId() ); assertEquals( 4, slave3.getServerId() ); }
@Test public void roundRobinWithTwoSlavesAndPushFactorOneWhenSlaveIsRemoved() { SlavePriority roundRobin = SlavePriorities.roundRobin(); Slave slave1 = roundRobin.prioritize( slaves( 2, 3, 4 ) ).iterator().next(); Slave slave2 = roundRobin.prioritize( slaves( 2, 3, 4 ) ).iterator().next(); Slave slave3 = roundRobin.prioritize( slaves( 2, 3 ) ).iterator().next(); assertEquals( 2, slave1.getServerId() ); assertEquals( 3, slave2.getServerId() ); assertEquals( 2, slave3.getServerId() ); }
@Test public void roundRobinWithSingleSlave() { SlavePriority roundRobin = SlavePriorities.roundRobin(); Iterator<Slave> slaves = roundRobin.prioritize( slaves( 2 ) ).iterator(); assertEquals( 2, slaves.next().getServerId() ); }
@Test public void roundRobinWithNoSlaves() { SlavePriority roundRobin = SlavePriorities.roundRobin(); Iterator<Slave> slaves = roundRobin.prioritize( slaves() ).iterator(); assertFalse( slaves.hasNext() ); }