@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(); } } }
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 { 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 { 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); }
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); }
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);  }