Return-Path: X-Original-To: apmail-brooklyn-dev-archive@minotaur.apache.org Delivered-To: apmail-brooklyn-dev-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 8BB4217B47 for ; Tue, 17 Nov 2015 12:09:28 +0000 (UTC) Received: (qmail 8353 invoked by uid 500); 17 Nov 2015 12:09:28 -0000 Delivered-To: apmail-brooklyn-dev-archive@brooklyn.apache.org Received: (qmail 8312 invoked by uid 500); 17 Nov 2015 12:09:28 -0000 Mailing-List: contact dev-help@brooklyn.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@brooklyn.incubator.apache.org Delivered-To: mailing list dev@brooklyn.incubator.apache.org Received: (qmail 8301 invoked by uid 99); 17 Nov 2015 12:09:27 -0000 Received: from Unknown (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 17 Nov 2015 12:09:27 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id 81A69C2A46 for ; Tue, 17 Nov 2015 12:09:27 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.399 X-Spam-Level: X-Spam-Status: No, score=0.399 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-0.582, URIBL_BLOCKED=0.001] autolearn=disabled Received: from mx1-us-east.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id 7zbiaEBIHfoF for ; Tue, 17 Nov 2015 12:09:18 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-us-east.apache.org (ASF Mail Server at mx1-us-east.apache.org) with SMTP id 1FAAE42AEE for ; Tue, 17 Nov 2015 12:09:18 +0000 (UTC) Received: (qmail 8244 invoked by uid 99); 17 Nov 2015 12:09:17 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 17 Nov 2015 12:09:17 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 7CDB3DFDCC; Tue, 17 Nov 2015 12:09:17 +0000 (UTC) From: neykov To: dev@brooklyn.incubator.apache.org Reply-To: dev@brooklyn.incubator.apache.org References: In-Reply-To: Subject: [GitHub] incubator-brooklyn pull request: Fix jclouds sshable Content-Type: text/plain Message-Id: <20151117120917.7CDB3DFDCC@git1-us-west.apache.org> Date: Tue, 17 Nov 2015 12:09:17 +0000 (UTC) Github user neykov commented on a diff in the pull request: https://github.com/apache/incubator-brooklyn/pull/1034#discussion_r45052171 --- Diff: utils/common/src/test/java/org/apache/brooklyn/util/net/ReachableSocketFinderTest.java --- @@ -0,0 +1,165 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.brooklyn.util.net; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.fail; + +import java.net.ServerSocket; +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; +import org.apache.brooklyn.util.time.Duration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import com.google.common.base.Predicate; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Maps; +import com.google.common.net.HostAndPort; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.ListeningExecutorService; +import com.google.common.util.concurrent.MoreExecutors; + +public class ReachableSocketFinderTest { + + private static final Logger LOG = LoggerFactory.getLogger(ReachableSocketFinderTest.class); + + private HostAndPort socket1; + private HostAndPort socket2; + private Map reachabilityResults; + private ListeningExecutorService executor; + private Predicate socketTester; + private ReachableSocketFinder finder; + + @BeforeMethod(alwaysRun=true) + public void setUp() throws Exception { + socket1 = HostAndPort.fromParts("1.1.1.1", 1111); + socket2 = HostAndPort.fromParts("1.1.1.2", 1112); + reachabilityResults = Maps.newConcurrentMap(); + executor = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool()); + socketTester = new Predicate() { + @Override public boolean apply(HostAndPort input) { + return Boolean.TRUE.equals(reachabilityResults.get(input)); + } + }; + + finder = new ReachableSocketFinder(socketTester, executor); + } + + @AfterMethod(alwaysRun=true) + public void tearDown() throws Exception { + if (executor != null) executor.shutdownNow(); + } + + @Test(expectedExceptions=IllegalStateException.class) + public void testWhenNoSocketsThrowsIllegalState() throws Exception { + finder.findOpenSocketOnNode(ImmutableList.of(), Duration.TEN_SECONDS); + } + + @Test + public void testReturnsReachableSocket() throws Exception { + reachabilityResults.put(socket1, true); + reachabilityResults.put(socket2, false); + assertEquals(finder.findOpenSocketOnNode(ImmutableList.of(socket1, socket2), Duration.TEN_SECONDS), socket1); + + reachabilityResults.put(socket1, false); + reachabilityResults.put(socket2, true); + assertEquals(finder.findOpenSocketOnNode(ImmutableList.of(socket1, socket2), Duration.TEN_SECONDS), socket2); + } + + @Test + public void testPollsUntilPortReachable() throws Exception { + reachabilityResults.put(socket1, false); + reachabilityResults.put(socket2, false); + final ListenableFuture future = executor.submit(new Callable() { + @Override public HostAndPort call() throws Exception { + return finder.findOpenSocketOnNode(ImmutableList.of(socket1, socket2), Duration.TEN_SECONDS); + }}); + + // Should keep trying + Asserts.succeedsContinually(new Runnable() { + @Override public void run() { + assertFalse(future.isDone()); + }}); + + // When port is reached, it completes + reachabilityResults.put(socket1, true); + assertEquals(future.get(30, TimeUnit.SECONDS), socket1); + } + + // Mark as integration, as can't rely (in Apache infra) for a port to stay unused during test! + @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(); + } + } + } + + // Mark as integration, as can't rely (in Apache infra) for a port to stay unused during test! + // And slow test - takes 5 seconds. + @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) { + // success + } + } + + private ServerSocket connectToPort() throws Exception { + int port = findAvailablePort(); + LOG.info("acquired port on "+port+" for test "+JavaClassNames.niceClassAndMethod()); + return new ServerSocket(port); --- End diff -- Could use port 0 to avoid someone else stealing the port. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---