Fix Web Socket server connection failure 44/107744/1
authorliamfallon <liam.fallon@est.tech>
Fri, 15 May 2020 14:09:37 +0000 (15:09 +0100)
committerliamfallon <liam.fallon@est.tech>
Fri, 15 May 2020 14:16:18 +0000 (15:16 +0100)
In the Web Socket unit test, sometimes the server does not start quickly
enough and is not available when the wek socket client connects. This
review changes the unit test to wait for up to 2 seconds for the web
socket server to come up.

Issue-ID: POLICY-2571
Change-Id: I0c970783f368d691d07683bad0cdc28b681e5334
Signed-off-by: liamfallon <liam.fallon@est.tech>
core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/messaging/stringmessaging/WsStringMessageClient.java
core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/messaging/stringmessaging/WsStringMessageServer.java
core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/messaging/stringmessaging/WsStringMessager.java
core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/messaging/EndToEndStringMessagingTest.java

index 98bffdb..968ec97 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ *  Modifications Copyright (C) 2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -135,4 +136,12 @@ public class WsStringMessageClient implements WsStringMessager {
             wsStringMessageListener.receiveString(messageString);
         }
     }
+
+    /**
+     * {@inheritDoc}.
+     */
+    @Override
+    public boolean isStarted() {
+        return service.isStarted();
+    }
 }
index 41fb82a..6b1dc67 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
- *  Modifications Copyright (C) 2019 Nordix Foundation.
+ *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 package org.onap.policy.apex.core.infrastructure.messaging.stringmessaging;
 
 import com.google.common.eventbus.Subscribe;
+
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.UnknownHostException;
+
 import org.onap.policy.apex.core.infrastructure.messaging.MessageListener;
 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
 import org.onap.policy.apex.core.infrastructure.messaging.MessagingService;
@@ -72,17 +74,15 @@ public class WsStringMessageServer implements WsStringMessager {
             String lanaddress = "unknown";
             try {
                 lanaddress = MessagingUtils.getLocalHostLanAddress().getHostAddress();
-            }
-            catch (final UnknownHostException ignore) {
-                LOGGER.debug("Failed to find name of local address name",ignore);
+            } catch (final UnknownHostException ignore) {
+                LOGGER.debug("Failed to find name of local address name", ignore);
             }
             LOGGER.debug("web socket string message server LAN address=" + lanaddress);
             String hostaddress = "unknown";
             try {
                 hostaddress = InetAddress.getLocalHost().getHostAddress();
-            }
-            catch (final UnknownHostException ignore) {
-                LOGGER.debug("Failed to find name of local address",ignore);
+            } catch (final UnknownHostException ignore) {
+                LOGGER.debug("Failed to find name of local address", ignore);
             }
             LOGGER.debug("web socket string message server host address=" + hostaddress);
         }
@@ -145,4 +145,12 @@ public class WsStringMessageServer implements WsStringMessager {
             wsStringMessageListener.receiveString(messageString);
         }
     }
+
+    /**
+     * {@inheritDoc}.
+     */
+    @Override
+    public boolean isStarted() {
+        return service.isStarted();
+    }
 }
index 2a731b0..c6db1ff 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ *  Modifications Copyright (C) 2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -48,4 +49,9 @@ public interface WsStringMessager {
      * @param stringMessage the string message to send
      */
     void sendString(String stringMessage);
+
+    /**
+     * Check if the string messager is started.
+     */
+    boolean isStarted();
 }
index 5a13901..35d3485 100644 (file)
@@ -55,6 +55,8 @@ public class EndToEndStringMessagingTest {
         assertNotNull(server);
         server.start(new WsStringServerMessageListener());
 
+        await().atMost(2, TimeUnit.SECONDS).until(() -> server.isStarted());
+
         try {
             client = new WsStringMessageClient("localhost", 44441);
             assertNotNull(client);