DnsSrvConnectionStrategy extends AbstractConnectionStrategy { @Override public synchronized Iterator<LdapURL> iterator() { if (!isInitialized()) { throw new IllegalStateException("Strategy is not initialized"); } if (Instant.now().isAfter(expirationTime)) { populate(ldapUrls, ldapURLSet); } return new DefaultLdapURLIterator(ldapURLSet.getUrls()); } DnsSrvConnectionStrategy();  DnsSrvConnectionStrategy(final Duration ttl);  DnsSrvConnectionStrategy(final DNSContextFactory factory);  DnsSrvConnectionStrategy(final DNSContextFactory factory, final Duration ttl);  DnsSrvConnectionStrategy(final DNSContextFactory factory, final Duration ttl, final boolean ssl); @Override void populate(final String urls, final LdapURLSet urlSet); @Override synchronized Iterator<LdapURL> iterator();  }
@Test(groups = "conn") public void firstUrlInactive() throws Exception { final DnsSrvConnectionStrategy strategy = new DnsSrvConnectionStrategy(contextFactory); final ConnectionConfig cc = new ConnectionConfig(); cc.setLdapUrl("dns: 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 DnsSrvConnectionStrategy strategy = new DnsSrvConnectionStrategy(contextFactory); final ConnectionConfig cc = new ConnectionConfig(); cc.setLdapUrl("dns: 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: 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); }