RoundRobinConnectionStrategy extends AbstractConnectionStrategy { @Override public synchronized Iterator<LdapURL> iterator() { if (!isInitialized()) { throw new IllegalStateException("Strategy is not initialized"); } final List<LdapURL> urls = new ArrayList<>(ldapURLSet.getActiveUrls()); if (urls.size() > 1) { for (int i = 0; i < counter.get(); i++) { urls.add(urls.remove(0)); } } urls.addAll(ldapURLSet.getInactiveUrls()); counter.incrementAndGet(); if (iterFunction != null) { return iterFunction.apply(ldapURLSet.getUrls()); } return new DefaultLdapURLIterator(urls); } RoundRobinConnectionStrategy();  RoundRobinConnectionStrategy(final Function<List<LdapURL>, Iterator<LdapURL>> function); @Override synchronized Iterator<LdapURL> iterator();  }
@Test(groups = "conn") public void firstUrlInactive() throws Exception { final RoundRobinConnectionStrategy strategy = new RoundRobinConnectionStrategy(); final ConnectionConfig cc = new ConnectionConfig(); cc.setLdapUrl("ldap: cc.setConnectionStrategy(strategy); final MockConnection conn = new MockConnection(cc); conn.setOpenPredicate(ldapURL -> !ldapURL.getHostname().contains("-1")); conn.setTestPredicate(ldapURL -> true); Assert.assertEquals(strategy.ldapURLSet.getActiveUrls().size(), 3); Assert.assertEquals( strategy.ldapURLSet.getActiveUrls(), Arrays.asList( new LdapURL("ldap: new LdapURL("ldap: new LdapURL("ldap: Assert.assertEquals(strategy.ldapURLSet.getInactiveUrls().size(), 0); conn.open(); Assert.assertEquals(strategy.ldapURLSet.getActiveUrls().size(), 2); Assert.assertEquals( strategy.ldapURLSet.getActiveUrls(), Arrays.asList( new LdapURL("ldap: new LdapURL("ldap: Assert.assertEquals(strategy.ldapURLSet.getInactiveUrls().size(), 1); Assert.assertEquals( strategy.ldapURLSet.getInactiveUrls().iterator().next(), new LdapURL("ldap: Assert.assertEquals( StreamSupport.stream(strategy.spliterator(), false).collect(Collectors.toList()), Arrays.asList( new LdapURL("ldap: new LdapURL("ldap: new LdapURL("ldap: Assert.assertEquals(strategy.ldapURLSet.getActiveUrls().size(), 2); Assert.assertEquals( strategy.ldapURLSet.getActiveUrls(), Arrays.asList( new LdapURL("ldap: new LdapURL("ldap: Assert.assertEquals(strategy.ldapURLSet.getInactiveUrls().size(), 1); Assert.assertEquals( strategy.ldapURLSet.getInactiveUrls().iterator().next(), new LdapURL("ldap: strategy.success(strategy.ldapURLSet.getInactiveUrls().iterator().next()); Assert.assertEquals( StreamSupport.stream(strategy.spliterator(), false).collect(Collectors.toList()), Arrays.asList( new LdapURL("ldap: new LdapURL("ldap: new LdapURL("ldap: Assert.assertEquals(strategy.ldapURLSet.getActiveUrls().size(), 3); Assert.assertEquals( strategy.ldapURLSet.getActiveUrls(), Arrays.asList( new LdapURL("ldap: new LdapURL("ldap: new LdapURL("ldap: Assert.assertEquals(strategy.ldapURLSet.getInactiveUrls().size(), 0); }
@Test(groups = "conn") public void firstAndSecondUrlInactive() throws Exception { final RoundRobinConnectionStrategy strategy = new RoundRobinConnectionStrategy(); final ConnectionConfig cc = new ConnectionConfig(); cc.setLdapUrl("ldap: cc.setConnectionStrategy(strategy); final MockConnection conn = new MockConnection(cc); conn.setOpenPredicate(ldapURL -> !ldapURL.getHostname().contains("-1") && !ldapURL.getHostname().contains("-2")); conn.setTestPredicate(ldapURL -> true); Assert.assertEquals(strategy.ldapURLSet.getActiveUrls().size(), 3); Assert.assertEquals( strategy.ldapURLSet.getActiveUrls(), Arrays.asList( new LdapURL("ldap: new LdapURL("ldap: new LdapURL("ldap: Assert.assertEquals(strategy.ldapURLSet.getInactiveUrls().size(), 0); conn.open(); Assert.assertEquals(strategy.ldapURLSet.getActiveUrls().size(), 1); Assert.assertEquals( strategy.ldapURLSet.getActiveUrls().iterator().next(), new LdapURL("ldap: Assert.assertEquals(strategy.ldapURLSet.getInactiveUrls().size(), 2); Assert.assertEquals( strategy.ldapURLSet.getInactiveUrls(), Arrays.asList( new LdapURL("ldap: new LdapURL("ldap: Assert.assertEquals( StreamSupport.stream(strategy.spliterator(), false).collect(Collectors.toList()), Arrays.asList( new LdapURL("ldap: new LdapURL("ldap: new LdapURL("ldap: Assert.assertEquals(strategy.ldapURLSet.getActiveUrls().size(), 1); Assert.assertEquals( strategy.ldapURLSet.getActiveUrls().iterator().next(), new LdapURL("ldap: Assert.assertEquals(strategy.ldapURLSet.getInactiveUrls().size(), 2); Assert.assertEquals( strategy.ldapURLSet.getInactiveUrls(), Arrays.asList( new LdapURL("ldap: new LdapURL("ldap: strategy.success(strategy.ldapURLSet.getInactiveUrls().iterator().next()); Assert.assertEquals( StreamSupport.stream(strategy.spliterator(), false).collect(Collectors.toList()), Arrays.asList( new LdapURL("ldap: new LdapURL("ldap: new LdapURL("ldap: Assert.assertEquals(strategy.ldapURLSet.getActiveUrls().size(), 2); Assert.assertEquals( strategy.ldapURLSet.getActiveUrls(), Arrays.asList( new LdapURL("ldap: new LdapURL("ldap: Assert.assertEquals(strategy.ldapURLSet.getInactiveUrls().size(), 1); Assert.assertEquals( strategy.ldapURLSet.getInactiveUrls().iterator().next(), new LdapURL("ldap: Assert.assertEquals( StreamSupport.stream(strategy.spliterator(), false).collect(Collectors.toList()), Arrays.asList( new LdapURL("ldap: new LdapURL("ldap: new LdapURL("ldap: Assert.assertEquals(strategy.ldapURLSet.getActiveUrls().size(), 2); Assert.assertEquals( strategy.ldapURLSet.getActiveUrls(), Arrays.asList( new LdapURL("ldap: new LdapURL("ldap: Assert.assertEquals(strategy.ldapURLSet.getInactiveUrls().size(), 1); Assert.assertEquals( strategy.ldapURLSet.getInactiveUrls().iterator().next(), new LdapURL("ldap: strategy.success(strategy.ldapURLSet.getInactiveUrls().iterator().next()); Assert.assertEquals( StreamSupport.stream(strategy.spliterator(), false).collect(Collectors.toList()), Arrays.asList( new LdapURL("ldap: new LdapURL("ldap: new LdapURL("ldap: Assert.assertEquals(strategy.ldapURLSet.getActiveUrls().size(), 3); Assert.assertEquals( strategy.ldapURLSet.getActiveUrls(), Arrays.asList( new LdapURL("ldap: new LdapURL("ldap: new LdapURL("ldap: Assert.assertEquals(strategy.ldapURLSet.getInactiveUrls().size(), 0); }
@Test(groups = "conn", dataProvider = "urls") public void iterator(final String actual, final LdapURL[] expected, final int count) { final RoundRobinConnectionStrategy strategy = new RoundRobinConnectionStrategy(); strategy.initialize(actual, ldapURL -> true); for (int i = 0; i < count; i++) { strategy.iterator(); } Assert.assertEquals( StreamSupport.stream(strategy.spliterator(), false).collect(Collectors.toList()), Arrays.asList(expected)); }
@Test(groups = "conn", dataProvider = "urls") public void hasNext(final String actual, final LdapURL[] expected, final int count) { final RoundRobinConnectionStrategy strategy = new RoundRobinConnectionStrategy(); strategy.initialize(actual, ldapURL -> true); final Iterator<LdapURL> iter = strategy.iterator(); for (int i = 0; i < count; i++) { strategy.iterator(); } int i = 0; while (iter.hasNext()) { iter.next(); i++; } Assert.assertEquals(i, expected.length); }
@Test(groups = "conn") public void noActiveUrls() throws Exception { final RoundRobinConnectionStrategy strategy = new RoundRobinConnectionStrategy(); strategy.initialize( "ldap: ldapURL -> true); strategy.failure(strategy.ldapURLSet.getActiveUrls().iterator().next()); strategy.failure(strategy.ldapURLSet.getActiveUrls().iterator().next()); strategy.failure(strategy.ldapURLSet.getActiveUrls().iterator().next()); strategy.iterator(); strategy.iterator(); strategy.iterator(); strategy.iterator(); }