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();  }
@Test(groups = "conn", dataProvider = "urls") public void iterator(final String actual, final LdapURL[] expected) { final RandomConnectionStrategy strategy = new RandomConnectionStrategy(); strategy.initialize(actual, ldapURL -> true); Assert.assertEquals( (HashSet) StreamSupport.stream(strategy.spliterator(), false).collect(Collectors.toCollection(HashSet::new)), (HashSet) Stream.of(expected).collect(Collectors.toCollection(HashSet::new))); Assert.assertEquals( (HashSet) StreamSupport.stream(strategy.spliterator(), false).collect(Collectors.toCollection(HashSet::new)), (HashSet) Stream.of(expected).collect(Collectors.toCollection(HashSet::new))); Assert.assertEquals( (HashSet) StreamSupport.stream(strategy.spliterator(), false).collect(Collectors.toCollection(HashSet::new)), (HashSet) Stream.of(expected).collect(Collectors.toCollection(HashSet::new))); Assert.assertEquals( (HashSet) StreamSupport.stream(strategy.spliterator(), false).collect(Collectors.toCollection(HashSet::new)), (HashSet) Stream.of(expected).collect(Collectors.toCollection(HashSet::new))); Assert.assertEquals( (HashSet) StreamSupport.stream(strategy.spliterator(), false).collect(Collectors.toCollection(HashSet::new)), (HashSet) Stream.of(expected).collect(Collectors.toCollection(HashSet::new))); }
@Test(groups = "conn", dataProvider = "urls") public void hasNext(final String actual, final LdapURL[] expected) { final RandomConnectionStrategy strategy = new RandomConnectionStrategy(); strategy.initialize(actual, ldapURL -> true); final Iterator<LdapURL> iter = 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 RandomConnectionStrategy strategy = new RandomConnectionStrategy(); 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(); }
@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))); }
@Test(groups = "conn") public void firstAndSecondUrlInactive() 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 == 1) { 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(), 1); Assert.assertEquals(strategy.ldapURLSet.getInactiveUrls().size(), 2); 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.getInactiveUrls().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(), 2); Assert.assertEquals(strategy.ldapURLSet.getInactiveUrls().size(), 1); 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))); }