RoundRobinLoadBalancer extends QueryPoolLoadBalancer<Q, E> { @Override public Iterator<Pair<Q, E>> provide() { final CircularSupplier<Q> querySupplier = CircularSupplier.create(queryProvider); final CircularSupplier<E> endpointSupplier = CircularSupplier.create(endpointProvider); return new Iterator<Pair<Q, E>>() { @Override public boolean hasNext() { return true; } @Override public Pair<Q, E> next() { E endpoint = endpointSupplier.pop(); Q query = querySupplier.pop(); return Pair.of(query, endpoint); } @Override public void remove() { throw new UnsupportedOperationException("Read only iterator"); } @Override public String toString() { return "RoundRobinLoadBalancer iterator"; } }; } RoundRobinLoadBalancer();  RoundRobinLoadBalancer(Iterable<Q> queryProvider, Iterable<E> endpointProvider); @Override Iterator<Pair<Q, E>> provide(); @Override String toString();  }
@Test public void shouldProvidePairs() throws Exception { List<Integer> queries = ImmutableList.of(1, 2, 3); List<Integer> endpoints = ImmutableList.of(1, 2); RoundRobinLoadBalancer<Integer, Integer> balancer = new RoundRobinLoadBalancer<Integer, Integer>(queries, endpoints); Iterator<Pair<Integer, Integer>> chunks = balancer.provide(); assertEquals(chunks.next(), Pair.of(1, 1)); assertEquals(chunks.next(), Pair.of(2, 2)); assertEquals(chunks.next(), Pair.of(3, 1)); assertEquals(chunks.next(), Pair.of(1, 2)); assertEquals(chunks.next(), Pair.of(2, 1)); assertEquals(chunks.next(), Pair.of(3, 2)); }
@Test public void shouldProvidePairsForOneEndpoint() throws Exception { List<Integer> queries = ImmutableList.of(1, 2, 3); List<Integer> endpoints = ImmutableList.of(1); RoundRobinLoadBalancer<Integer, Integer> balancer = new RoundRobinLoadBalancer<Integer, Integer>(queries, endpoints); Iterator<Pair<Integer, Integer>> chunks = balancer.provide(); assertEquals(chunks.next(), Pair.of(1, 1)); assertEquals(chunks.next(), Pair.of(2, 1)); assertEquals(chunks.next(), Pair.of(3, 1)); assertEquals(chunks.next(), Pair.of(1, 1)); assertEquals(chunks.next(), Pair.of(2, 1)); assertEquals(chunks.next(), Pair.of(3, 1)); }
@Test public void shouldProvidePairsForOneQuery() throws Exception { List<Integer> queries = ImmutableList.of(1); List<Integer> endpoints = ImmutableList.of(1, 2, 3, 4); RoundRobinLoadBalancer<Integer, Integer> balancer = new RoundRobinLoadBalancer<Integer, Integer>(queries, endpoints); Iterator<Pair<Integer, Integer>> chunks = balancer.provide(); assertEquals(chunks.next(), Pair.of(1, 1)); assertEquals(chunks.next(), Pair.of(1, 2)); assertEquals(chunks.next(), Pair.of(1, 3)); assertEquals(chunks.next(), Pair.of(1, 4)); assertEquals(chunks.next(), Pair.of(1, 1)); assertEquals(chunks.next(), Pair.of(1, 2)); assertEquals(chunks.next(), Pair.of(1, 3)); assertEquals(chunks.next(), Pair.of(1, 4)); }