Repository: brooklyn-server Updated Branches: refs/heads/master 17aa62213 -> 91a35e208
Short cut ReachableSocketFinder.findOpenSocketOnNode And use the default grace period in ReachableSocketFinderTest Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/01f935cd Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/01f935cd Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/01f935cd Branch: refs/heads/master Commit: 01f935cd5956ddee8d91983a4b6ad4542b6f1de8 Parents: 17aa622 Author: Sam Corbett <sam.corb...@cloudsoftcorp.com> Authored: Mon Feb 27 16:54:19 2017 +0000 Committer: Sam Corbett <sam.corb...@cloudsoftcorp.com> Committed: Mon Feb 27 16:54:19 2017 +0000 ---------------------------------------------------------------------- .../brooklyn/util/net/ReachableSocketFinder.java | 16 +++++++++------- .../util/net/ReachableSocketFinderTest.java | 8 ++++---- 2 files changed, 13 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/01f935cd/utils/common/src/main/java/org/apache/brooklyn/util/net/ReachableSocketFinder.java ---------------------------------------------------------------------- diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/net/ReachableSocketFinder.java b/utils/common/src/main/java/org/apache/brooklyn/util/net/ReachableSocketFinder.java index 7e17949..c00a6a3 100644 --- a/utils/common/src/main/java/org/apache/brooklyn/util/net/ReachableSocketFinder.java +++ b/utils/common/src/main/java/org/apache/brooklyn/util/net/ReachableSocketFinder.java @@ -86,12 +86,13 @@ public class ReachableSocketFinder { checkNotNull(sockets, "sockets"); checkState(!Iterables.isEmpty(sockets), "No hostAndPort sockets supplied"); checkNotNull(timeout, "timeout"); - Iterator<HostAndPort> iter = findOpenSocketsOnNode(sockets, timeout).iterator(); + Iterable<HostAndPort> reachable = Optional.presentInstances(tryReachable(sockets, timeout, false)); + Iterator<HostAndPort> iter = reachable.iterator(); if (iter.hasNext()) { return iter.next(); } else { LOG.warn("No sockets in {} reachable after {}", sockets, timeout); - throw new NoSuchElementException("could not connect to any socket in " + sockets); + throw new NoSuchElementException("Could not connect to any socket in " + sockets); } } @@ -110,7 +111,7 @@ public class ReachableSocketFinder { checkNotNull(sockets, "sockets"); checkState(!Iterables.isEmpty(sockets), "No hostAndPort sockets supplied"); checkNotNull(timeout, "timeout"); - return Optional.presentInstances(tryReachable(sockets, timeout)); + return Optional.presentInstances(tryReachable(sockets, timeout, true)); } /** @@ -118,8 +119,9 @@ public class ReachableSocketFinder { * according to {@link #socketTester} and absent values for those not. Checks are concurrent. * The iterable returned is ordered according sockets. */ - private Iterable<Optional<HostAndPort>> tryReachable(Iterable<? extends HostAndPort> sockets, final Duration timeout) { - LOG.debug("blocking on reachable sockets in {} for {}", sockets, timeout); + private Iterable<Optional<HostAndPort>> tryReachable( + Iterable<? extends HostAndPort> sockets, final Duration timeout, final boolean useGracePeriod) { + LOG.debug("Blocking on reachable sockets in {} for {}", sockets, timeout); final List<ListenableFuture<Optional<HostAndPort>>> futures = Lists.newArrayList(); final AtomicReference<Stopwatch> sinceFirstCompleted = new AtomicReference<>(); final ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool()); @@ -134,7 +136,8 @@ public class ReachableSocketFinder { private boolean gracePeriodExpired() { Stopwatch firstCompleted = sinceFirstCompleted.get(); - return firstCompleted != null && gracePeriod.subtract(Duration.of(firstCompleted)).isNegative(); + return firstCompleted != null + && (!useGracePeriod || gracePeriod.subtract(Duration.of(firstCompleted)).isNegative()); } /** Checks checker for completion and reschedules it if time allows. */ @@ -175,7 +178,6 @@ public class ReachableSocketFinder { } } })); - } ImmutableList.Builder<Optional<HostAndPort>> results = ImmutableList.builder(); http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/01f935cd/utils/common/src/test/java/org/apache/brooklyn/util/net/ReachableSocketFinderTest.java ---------------------------------------------------------------------- diff --git a/utils/common/src/test/java/org/apache/brooklyn/util/net/ReachableSocketFinderTest.java b/utils/common/src/test/java/org/apache/brooklyn/util/net/ReachableSocketFinderTest.java index 0d91e09..cd1dc00 100644 --- a/utils/common/src/test/java/org/apache/brooklyn/util/net/ReachableSocketFinderTest.java +++ b/utils/common/src/test/java/org/apache/brooklyn/util/net/ReachableSocketFinderTest.java @@ -25,7 +25,6 @@ import java.util.Map; import java.util.NoSuchElementException; import java.util.concurrent.Callable; import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; import org.apache.brooklyn.test.Asserts; import org.apache.brooklyn.util.javalang.JavaClassNames; @@ -72,7 +71,7 @@ public class ReachableSocketFinderTest { public void setUp() throws Exception { reachabilityResults = Maps.newConcurrentMap(); executor = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool()); - finder = new ReachableSocketFinder(socketTester, Duration.ZERO); + finder = new ReachableSocketFinder(socketTester); } @AfterMethod(alwaysRun=true) @@ -118,7 +117,7 @@ public class ReachableSocketFinderTest { // When port is reached, it completes reachabilityResults.put(socket1, true); - assertEquals(future.get(5, TimeUnit.SECONDS), socket1); + assertEquals(future.get(), socket1); } @Test @@ -143,7 +142,8 @@ public class ReachableSocketFinderTest { public void testSocketResultIgnoredIfGracePeriodExpiresAfterFirstResultAvailable() { reachabilityResults.put(socket1, false); reachabilityResults.put(socket2, true); - + // Override the default test grace period. + finder = new ReachableSocketFinder(socketTester, Duration.ZERO); final Iterable<HostAndPort> actual = finder.findOpenSocketsOnNode(ImmutableList.of(socket1, socket2), TIMEOUT); // Sleep through the grace period. Time.sleep(50);