ReachableSocketFinder { public Iterable<HostAndPort> findOpenSocketsOnNode(final Iterable<? extends HostAndPort> sockets, Duration timeout) { checkNotNull(sockets, "sockets"); checkState(!Iterables.isEmpty(sockets), "No hostAndPort sockets supplied"); checkNotNull(timeout, "timeout"); return Optional.presentInstances(tryReachable(sockets, timeout, true)); } ReachableSocketFinder();  ReachableSocketFinder(Predicate<? super HostAndPort> socketTester);  ReachableSocketFinder(Predicate<? super HostAndPort> socketTester, Duration gracePeriod); HostAndPort findOpenSocketOnNode(final Iterable<? extends HostAndPort> sockets, Duration timeout); Iterable<HostAndPort> findOpenSocketsOnNode(final Iterable<? extends HostAndPort> sockets, Duration timeout);  }
@Test(expectedExceptions=IllegalStateException.class) public void testFindAllWhenNoSocketsThrowsIllegalState() throws Exception { finder.findOpenSocketsOnNode(ImmutableList.<HostAndPort>of(), TIMEOUT); }
@Test public void testGetReachableSockets() throws Exception { reachabilityResults.put(socket1, true); reachabilityResults.put(socket2, false); reachabilityResults.put(socket3, true); final Iterable<HostAndPort> expected = ImmutableList.of(socket1, socket3); final Iterable<HostAndPort> actual = finder.findOpenSocketsOnNode( ImmutableList.of(socket1, socket2, socket3), TIMEOUT); assertEquals(actual, expected, "expected=" + expected + ", actual=" + Iterables.toString(actual)); }
@Test public void testGetAllReachableSocketsEmpty() throws Exception { final Iterable<HostAndPort> expected = ImmutableList.of(); final Iterable<HostAndPort> actual = finder.findOpenSocketsOnNode(ImmutableList.of(socket2), SHORT_TIMEOUT); assertEquals(actual, expected, "expected=" + expected + ", actual=" + Iterables.toString(actual)); }
@Test public void testSocketResultIgnoredIfGracePeriodExpiresAfterFirstResultAvailable() { reachabilityResults.put(socket1, false); reachabilityResults.put(socket2, true); finder = new ReachableSocketFinder(socketTester, Duration.ZERO); final Iterable<HostAndPort> actual = finder.findOpenSocketsOnNode(ImmutableList.of(socket1, socket2), TIMEOUT); Time.sleep(50); reachabilityResults.put(socket1, true); assertEquals(actual, ImmutableList.of(socket2)); }