@Test(groups = "conn") public void firstUrlInactive() throws Exception { final RandomConnectionStrategy strategy = new RandomConnectionStrategy(); final ConnectionConfig cc = new ConnectionConfig(); cc.setLdapUrl("ldap: cc.setConnectionStrategy(strategy); final MockConnection conn = new MockConnection(cc); conn.setOpenPredicate(new Predicate<>() { private int count; @Override public boolean test(final LdapURL url) { if (count == 0) { count++; return false; } return true; } }); conn.setTestPredicate(ldapURL -> true); Assert.assertEquals(strategy.ldapURLSet.getActiveUrls().size(), 3); Assert.assertEquals(strategy.ldapURLSet.getInactiveUrls().size(), 0); conn.open(); Assert.assertEquals(strategy.ldapURLSet.getActiveUrls().size(), 2); Assert.assertEquals(strategy.ldapURLSet.getInactiveUrls().size(), 1); List<LdapURL> applyList = StreamSupport.stream(strategy.spliterator(), false).collect(Collectors.toList()); Assert.assertEquals(applyList.size(), 3); Assert.assertTrue(strategy.ldapURLSet.getActiveUrls().contains(applyList.get(0))); Assert.assertTrue(strategy.ldapURLSet.getActiveUrls().contains(applyList.get(1))); Assert.assertTrue( strategy.ldapURLSet.getInactiveUrls().contains(applyList.get(2))); strategy.success(strategy.ldapURLSet.getInactiveUrls().iterator().next()); applyList = StreamSupport.stream(strategy.spliterator(), false).collect(Collectors.toList()); Assert.assertEquals(applyList.size(), 3); Assert.assertEquals(strategy.ldapURLSet.getActiveUrls().size(), 3); Assert.assertEquals(strategy.ldapURLSet.getInactiveUrls().size(), 0); Assert.assertTrue(strategy.ldapURLSet.getActiveUrls().contains(applyList.get(0))); Assert.assertTrue(strategy.ldapURLSet.getActiveUrls().contains(applyList.get(1))); Assert.assertTrue(strategy.ldapURLSet.getActiveUrls().contains(applyList.get(2))); }
@Override public Iterator<LdapURL> iterator() { if (!isInitialized()) { throw new IllegalStateException("Strategy is not initialized"); } return new Iterator<>() { private final List<LdapURL> active = ldapURLSet.getActiveUrls().stream().collect( Collectors.collectingAndThen( Collectors.toCollection(ArrayList::new), list -> { Collections.shuffle(list); return list; })); private final List<LdapURL> inactive = ldapURLSet.getInactiveUrls().stream().collect( Collectors.collectingAndThen( Collectors.toCollection(ArrayList::new), list -> { Collections.shuffle(list); return list; })); private int i; @Override public boolean hasNext() { return i < active.size() + inactive.size(); } @Override public LdapURL next() { final LdapURL url; if (i < active.size()) { url = active.get(i); } else { url = inactive.get(i - active.size()); } i++; return url; } }; }
RandomConnectionStrategy extends AbstractConnectionStrategy { @Override public Iterator<LdapURL> iterator() { if (!isInitialized()) { throw new IllegalStateException("Strategy is not initialized"); } return new Iterator<>() { private final List<LdapURL> active = ldapURLSet.getActiveUrls().stream().collect( Collectors.collectingAndThen( Collectors.toCollection(ArrayList::new), list -> { Collections.shuffle(list); return list; })); private final List<LdapURL> inactive = ldapURLSet.getInactiveUrls().stream().collect( Collectors.collectingAndThen( Collectors.toCollection(ArrayList::new), list -> { Collections.shuffle(list); return list; })); private int i; @Override public boolean hasNext() { return i < active.size() + inactive.size(); } @Override public LdapURL next() { final LdapURL url; if (i < active.size()) { url = active.get(i); } else { url = inactive.get(i - active.size()); } i++; return url; } }; } }
RandomConnectionStrategy extends AbstractConnectionStrategy { @Override public Iterator<LdapURL> iterator() { if (!isInitialized()) { throw new IllegalStateException("Strategy is not initialized"); } return new Iterator<>() { private final List<LdapURL> active = ldapURLSet.getActiveUrls().stream().collect( Collectors.collectingAndThen( Collectors.toCollection(ArrayList::new), list -> { Collections.shuffle(list); return list; })); private final List<LdapURL> inactive = ldapURLSet.getInactiveUrls().stream().collect( Collectors.collectingAndThen( Collectors.toCollection(ArrayList::new), list -> { Collections.shuffle(list); return list; })); private int i; @Override public boolean hasNext() { return i < active.size() + inactive.size(); } @Override public LdapURL next() { final LdapURL url; if (i < active.size()) { url = active.get(i); } else { url = inactive.get(i - active.size()); } i++; return url; } }; }  }
RandomConnectionStrategy extends AbstractConnectionStrategy { @Override public Iterator<LdapURL> iterator() { if (!isInitialized()) { throw new IllegalStateException("Strategy is not initialized"); } return new Iterator<>() { private final List<LdapURL> active = ldapURLSet.getActiveUrls().stream().collect( Collectors.collectingAndThen( Collectors.toCollection(ArrayList::new), list -> { Collections.shuffle(list); return list; })); private final List<LdapURL> inactive = ldapURLSet.getInactiveUrls().stream().collect( Collectors.collectingAndThen( Collectors.toCollection(ArrayList::new), list -> { Collections.shuffle(list); return list; })); private int i; @Override public boolean hasNext() { return i < active.size() + inactive.size(); } @Override public LdapURL next() { final LdapURL url; if (i < active.size()) { url = active.get(i); } else { url = inactive.get(i - active.size()); } i++; return url; } }; }  @Override Iterator<LdapURL> iterator(); }
RandomConnectionStrategy extends AbstractConnectionStrategy { @Override public Iterator<LdapURL> iterator() { if (!isInitialized()) { throw new IllegalStateException("Strategy is not initialized"); } return new Iterator<>() { private final List<LdapURL> active = ldapURLSet.getActiveUrls().stream().collect( Collectors.collectingAndThen( Collectors.toCollection(ArrayList::new), list -> { Collections.shuffle(list); return list; })); private final List<LdapURL> inactive = ldapURLSet.getInactiveUrls().stream().collect( Collectors.collectingAndThen( Collectors.toCollection(ArrayList::new), list -> { Collections.shuffle(list); return list; })); private int i; @Override public boolean hasNext() { return i < active.size() + inactive.size(); } @Override public LdapURL next() { final LdapURL url; if (i < active.size()) { url = active.get(i); } else { url = inactive.get(i - active.size()); } i++; return url; } }; }  @Override Iterator<LdapURL> iterator();  }