From 009582f2e2a4bd3b09da3721a8b6587d5ac89723 Mon Sep 17 00:00:00 2001 From: Jorge Hernandez Date: Wed, 13 Sep 2017 07:28:12 -0500 Subject: wait until port is open in HttpClient junit tests As a note, HttpServer tests have retries at the http level. Hopefully this will resolve some timing issues and intermittent junits failures in LF jenkins environment. Issue-ID: POLICY-109 Change-Id: I44628b60d4912be5fc4639e0048791f5655bbd01 Signed-off-by: Jorge Hernandez --- .../org/onap/policy/drools/utils/NetworkUtil.java | 53 ++++++++++++++++++---- 1 file changed, 44 insertions(+), 9 deletions(-) (limited to 'policy-utils/src') diff --git a/policy-utils/src/main/java/org/onap/policy/drools/utils/NetworkUtil.java b/policy-utils/src/main/java/org/onap/policy/drools/utils/NetworkUtil.java index f6c837f5..bd5b8aac 100644 --- a/policy-utils/src/main/java/org/onap/policy/drools/utils/NetworkUtil.java +++ b/policy-utils/src/main/java/org/onap/policy/drools/utils/NetworkUtil.java @@ -7,9 +7,9 @@ * Licensed 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. @@ -20,16 +20,51 @@ package org.onap.policy.drools.utils; +import java.io.IOException; +import java.net.ConnectException; +import java.net.Socket; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * Network Utilities */ public class NetworkUtil { - - /** - * IPv4 Wildcard IP address - */ - public static final String IPv4_WILDCARD_ADDRESS = "0.0.0.0"; - - /* Other methods will be added as needed */ + + public static final Logger logger = LoggerFactory.getLogger(NetworkUtil.class.getName()); + + /** + * IPv4 Wildcard IP address + */ + public static final String IPv4_WILDCARD_ADDRESS = "0.0.0.0"; + + + /** + * try to connect to $host:$port $retries times while we are getting connection failures. + * + * @param host host + * @param port port + * @param retries number of attempts + * @return true is port is open, false otherwise + * @throws InterruptedException if execution has been interrupted + */ + public static boolean isTcpPortOpen(String host, int port, int retries, long interval) + throws InterruptedException, IOException { + int retry = 0; + while (retry < retries) { + try (Socket s = new Socket(host, port)) { + logger.debug("{}:{} connected - retries={} interval={}", host, port, retries, interval); + return true; + } catch (final ConnectException e) { + retry++; + logger.trace("{}:{} connected - retries={} interval={}", host, port, retries, interval, e); + Thread.sleep(interval); + } + } + + logger.warn("{}:{} closed = retries={} interval={}", host, port, retries, interval); + return false; + } } -- cgit 1.2.3-korg