RoundRobinBalancingPolicy implements LoadBalancingPolicy { @Override public HClientPool getPool(Collection<HClientPool> pools, Set<CassandraHost> excludeHosts) { HClientPool pool = getPoolSafely(pools); if ( excludeHosts != null && excludeHosts.size() > 0 ) { while ( excludeHosts.contains(pool.getCassandraHost()) ) { pool = getPoolSafely(pools); if ( excludeHosts.size() >= pools.size() ) break; } } return pool; } RoundRobinBalancingPolicy(); @Override HClientPool getPool(Collection<HClientPool> pools,
      Set<CassandraHost> excludeHosts); @Override HClientPool createConnection(HClientFactory clientFactory, CassandraHost host, CassandraClientMonitor monitor);  }
@Test public void testGetPoolOk() { roundRobinBalancingPolicy = new RoundRobinBalancingPolicy(); assertEquals(poolWith5Active.getNumActive(), roundRobinBalancingPolicy.getPool(pools, null).getNumActive()); assertEquals(poolWith7Active.getNumActive(), roundRobinBalancingPolicy.getPool(pools, null).getNumActive()); assertEquals(poolWith10Active.getNumActive(), roundRobinBalancingPolicy.getPool(pools, null).getNumActive()); assertEquals(poolWith5Active.getNumActive(), roundRobinBalancingPolicy.getPool(pools, null).getNumActive()); assertEquals(poolWith7Active.getNumActive(), roundRobinBalancingPolicy.getPool(pools, null).getNumActive()); assertEquals(poolWith10Active.getNumActive(), roundRobinBalancingPolicy.getPool(pools, null).getNumActive()); assertEquals(poolWith5Active.getNumActive(), roundRobinBalancingPolicy.getPool(pools, null).getNumActive()); assertEquals(poolWith7Active.getNumActive(), roundRobinBalancingPolicy.getPool(pools, null).getNumActive()); assertEquals(poolWith10Active.getNumActive(), roundRobinBalancingPolicy.getPool(pools, null).getNumActive()); for (int x=0; x<(256*256); x++) { assert roundRobinBalancingPolicy.getPool(pools, null).getNumActive() >= 5; } }
@Test public void testIgnoreExhausted() { Mockito.when(poolWith5Active.getCassandraHost()).thenReturn(new CassandraHost("127.0.0.1:9160")); Mockito.when(poolWith7Active.getCassandraHost()).thenReturn(new CassandraHost("127.0.0.2:9161")); Mockito.when(poolWith10Active.getCassandraHost()).thenReturn(new CassandraHost("127.0.0.3:9162")); roundRobinBalancingPolicy = new RoundRobinBalancingPolicy(); assertEquals(poolWith7Active, roundRobinBalancingPolicy.getPool(pools, new HashSet<CassandraHost>(Arrays.asList(new CassandraHost("127.0.0.1:9160"))))); assertEquals(poolWith10Active, roundRobinBalancingPolicy.getPool(pools, new HashSet<CassandraHost>(Arrays.asList(new CassandraHost("127.0.0.1:9160"))))); assertEquals(poolWith7Active, roundRobinBalancingPolicy.getPool(pools, new HashSet<CassandraHost>(Arrays.asList(new CassandraHost("127.0.0.1:9160"))))); assertEquals(poolWith10Active, roundRobinBalancingPolicy.getPool(pools, new HashSet<CassandraHost>(Arrays.asList(new CassandraHost("127.0.0.1:9160"))))); }
@Test public void testIgnoreExhaustedAll() { Mockito.when(poolWith5Active.getCassandraHost()).thenReturn(new CassandraHost("127.0.0.1:9160")); Mockito.when(poolWith7Active.getCassandraHost()).thenReturn(new CassandraHost("127.0.0.2:9161")); Mockito.when(poolWith10Active.getCassandraHost()).thenReturn(new CassandraHost("127.0.0.3:9162")); roundRobinBalancingPolicy = new RoundRobinBalancingPolicy(); assertNotNull(roundRobinBalancingPolicy.getPool(pools, new HashSet<CassandraHost>(Arrays.asList(new CassandraHost("127.0.0.1:9160"),new CassandraHost("127.0.0.2:9161"),new CassandraHost("127.0.0.3:9162"))))); }