### Question:
ReachableSocketFinder { public HostAndPort findOpenSocketOnNode(final Collection<? extends HostAndPort> sockets, Duration timeout) { checkNotNull(sockets, "sockets"); checkState(sockets.size() > 0, "No hostAndPort sockets supplied"); LOG.debug("blocking on any reachable socket in {} for {}", sockets, timeout); final AtomicReference<HostAndPort> result = new AtomicReference<HostAndPort>(); boolean passed = Repeater.create("socket-reachable") .limitTimeTo(timeout) .backoffTo(Duration.FIVE_SECONDS) .until(new Callable<Boolean>() { public Boolean call() { Optional<HostAndPort> reachableSocket = tryReachable(sockets, Duration.seconds(2)); if (reachableSocket.isPresent()) { result.compareAndSet(null, reachableSocket.get()); return true; } return false; }}) .run(); if (passed) { LOG.debug("<< socket {} opened", result); assert result.get() != null; return result.get(); } else { LOG.warn("No sockets in {} reachable after {}", sockets, timeout); throw new NoSuchElementException("could not connect to any socket in " + sockets); } } ReachableSocketFinder(ListeningExecutorService userExecutor);  ReachableSocketFinder(Predicate<HostAndPort> socketTester, ListeningExecutorService userExecutor); HostAndPort findOpenSocketOnNode(final Collection<? extends HostAndPort> sockets, Duration timeout);  }### Answer:
@Test(expectedExceptions=IllegalStateException.class) public void testWhenNoSocketsThrowsIllegalState() throws Exception { finder.findOpenSocketOnNode(ImmutableList.<HostAndPort>of(), Duration.TEN_SECONDS); }
@Test public void testReturnsReachableSocket() throws Exception { reachabilityResults.put(socket1, true); reachabilityResults.put(socket2, false); assertEquals(finder.findOpenSocketOnNode(ImmutableList.<HostAndPort>of(socket1, socket2), Duration.TEN_SECONDS), socket1); reachabilityResults.put(socket1, false); reachabilityResults.put(socket2, true); assertEquals(finder.findOpenSocketOnNode(ImmutableList.<HostAndPort>of(socket1, socket2), Duration.TEN_SECONDS), socket2); }
@Test public void testPollsUntilPortReachable() throws Exception { reachabilityResults.put(socket1, false); reachabilityResults.put(socket2, false); final ListenableFuture<HostAndPort> future = executor.submit(new Callable<HostAndPort>() { @Override public HostAndPort call() throws Exception { return finder.findOpenSocketOnNode(ImmutableList.<HostAndPort>of(socket1, socket2), Duration.TEN_SECONDS); }}); Asserts.succeedsContinually(new Runnable() { @Override public void run() { assertFalse(future.isDone()); }}); reachabilityResults.put(socket1, true); assertEquals(future.get(30, TimeUnit.SECONDS), socket1); }
@Test(groups="Integration") public void testReturnsRealReachableSocket() throws Exception { ReachableSocketFinder realFinder = new ReachableSocketFinder(executor); ServerSocket socket = connectToPort(); try { HostAndPort addr = HostAndPort.fromParts(socket.getInetAddress().getHostAddress(), socket.getLocalPort()); HostAndPort wrongAddr = HostAndPort.fromParts(socket.getInetAddress().getHostAddress(), findAvailablePort()); assertEquals(realFinder.findOpenSocketOnNode(ImmutableList.of(addr, wrongAddr), Duration.ONE_MINUTE), addr); } finally { if (socket != null) { socket.close(); } } }
@Test(groups="Integration") public void testFailsIfRealSocketUnreachable() throws Exception { ReachableSocketFinder realFinder = new ReachableSocketFinder(executor); HostAndPort wrongAddr = HostAndPort.fromParts(Networking.getLocalHost().getHostAddress(), findAvailablePort()); try { HostAndPort result = realFinder.findOpenSocketOnNode(ImmutableList.of(wrongAddr), Duration.FIVE_SECONDS); fail("Expected failure, but got "+result); } catch (NoSuchElementException e) { } }