Github user neykov commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/332#discussion_r79453578
  
    --- Diff: 
utils/common/src/test/java/org/apache/brooklyn/util/net/ReachabilityCheckerTest.java
 ---
    @@ -0,0 +1,89 @@
    +/*
    + * 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 com.google.common.base.Predicate;
    +import com.google.common.collect.ImmutableList;
    +import com.google.common.net.HostAndPort;
    +import com.google.common.util.concurrent.ListeningExecutorService;
    +import com.google.common.util.concurrent.MoreExecutors;
    +import com.google.mockwebserver.MockResponse;
    +import com.google.mockwebserver.MockWebServer;
    +import org.apache.brooklyn.test.Asserts;
    +import org.apache.brooklyn.util.time.Duration;
    +import org.testng.annotations.*;
    +
    +import java.io.IOException;
    +import java.util.NoSuchElementException;
    +import java.util.concurrent.Executors;
    +
    +public class ReachabilityCheckerTest {
    +    protected MockWebServer server;
    +    ListeningExecutorService executor = 
MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
    +
    +    @BeforeClass
    +    public void setUpClass() throws IOException {
    +        executor = 
MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
    +    }
    +
    +    @BeforeMethod
    +    public void setUp() throws IOException {
    +        server = new MockWebServer();
    +        server.enqueue(new MockResponse().setBody("one message"));
    +        server.play();
    +    }
    +
    +    @Test
    +    public void testReachibilityChecker() {
    +        ReachableSocketFinder finder = new 
ReachableSocketFinder(connectionChecker(), executor);
    +        HostAndPort result = 
finder.findOpenSocketOnNode(ImmutableList.of(HostAndPort.fromParts(server.getHostName(),
 server.getPort())), Duration.ONE_MINUTE);
    +        Asserts.assertEquals(result, 
HostAndPort.fromParts(server.getHostName(), server.getPort()));
    +    }
    +
    +    @Test(expectedExceptions = NoSuchElementException.class, 
expectedExceptionsMessageRegExp = "could not connect to any socket in.*")
    +    public void testReachibilityCheckerFails() {
    +        ReachableSocketFinder finder = new 
ReachableSocketFinder(connectionChecker(), executor);
    +        
finder.findOpenSocketOnNode(ImmutableList.of(HostAndPort.fromParts("localhost", 
server.getPort() + 1)), Duration.FIVE_SECONDS);
    +    }
    +
    +    @Test
    +    public void testReachibilityCheckerChoosesLaunched() {
    +        ReachableSocketFinder finder = new 
ReachableSocketFinder(connectionChecker(), executor);
    +        HostAndPort result = 
finder.findOpenSocketOnNode(ImmutableList.of(HostAndPort.fromParts("localhost", 
server.getPort() + 1),HostAndPort.fromParts(server.getHostName(), 
server.getPort())),
    +                Duration.FIVE_SECONDS);
    +        Asserts.assertEquals(result, 
HostAndPort.fromParts(server.getHostName(), server.getPort()));
    +    }
    +
    +    protected Predicate<HostAndPort> connectionChecker() {
    +        return new Predicate<HostAndPort>() {
    +            @Override public boolean apply(HostAndPort input) {
    +                return Networking.isReachable(input);
    +            }};
    +    }
    +
    +    @AfterMethod
    +    public void afterMethod() throws IOException {
    +        server.shutdown();
    +    }
    +
    +    @AfterClass
    +    public void afterClass() {
    +        executor.shutdownNow();
    +    }
    +}
    --- End diff --
    
    You can split the test into two:
     * how `ReachableSocketFinder` behaves - can be used with a mock/recording 
predicate. Doesn't need network connectivity and relying on specific ports 
being opened/closed, also quicker to execute.
     * The predicate - this needs network connectivity, but is a very thin 
wrapper over existing functionality so not too  importnant.


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to