Fix intermittent unit test failures WebSocket 61/105161/4
authorliamfallon <liam.fallon@est.tech>
Mon, 6 Apr 2020 14:41:28 +0000 (15:41 +0100)
committerliamfallon <liam.fallon@est.tech>
Mon, 6 Apr 2020 17:44:17 +0000 (18:44 +0100)
The Web Socket server is marked as started before the start process has
complted. This change fixes that issue.

Issue-ID: POLICY-1470
Change-Id: I5b5ef5832893d884a6551b0516df91cb71d6c622
Signed-off-by: liamfallon <liam.fallon@est.tech>
core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/messaging/impl/ws/server/MessageServerImpl.java
plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorTest.java
services/services-engine/pom.xml
services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/EngDepMessagingServiceTest.java

index 3844b92..5b2167e 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.
@@ -23,6 +24,7 @@ package org.onap.policy.apex.core.infrastructure.messaging.impl.ws.server;
 import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.util.Collection;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.java_websocket.WebSocket;
 import org.onap.policy.apex.core.infrastructure.messaging.MessageHolder;
@@ -47,7 +49,7 @@ public class MessageServerImpl<M> extends InternalMessageBusServer<M> {
     private final String connectionUri;
 
     // Indicates if the web socket server is started or not
-    private boolean isStarted = false;
+    private final AtomicBoolean isStarted = new AtomicBoolean(false);
 
     /**
      * Instantiates a new web socket messaging server for Apex.
@@ -73,7 +75,6 @@ public class MessageServerImpl<M> extends InternalMessageBusServer<M> {
     public void startConnection() {
         // Start reception of connections on the web socket
         start();
-        isStarted = true;
     }
 
     /**
@@ -98,7 +99,7 @@ public class MessageServerImpl<M> extends InternalMessageBusServer<M> {
             Thread.currentThread().interrupt();
             // This can happen in normal operation so ignore
         }
-        isStarted = false;
+        isStarted.set(false);
     }
 
     /**
@@ -141,11 +142,12 @@ public class MessageServerImpl<M> extends InternalMessageBusServer<M> {
      */
     @Override
     public boolean isStarted() {
-        return isStarted;
+        return isStarted.get();
     }
 
     @Override
     public void onStart() {
+        isStarted.set(true);
         LOGGER.debug("started deployment server on URI: {}", connectionUri);
     }
 }
index cbb81f9..1e81059 100644 (file)
@@ -143,7 +143,7 @@ public class RestRequestorTest {
         Response response = null;
 
         // Wait for the required amount of events to be received or for 10 seconds
-        Double getsSoFar = 0.0;
+        double getsSoFar = 0.0;
         for (int i = 0; i < 40; i++) {
             response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
                 .request("application/json").get();
@@ -156,7 +156,7 @@ public class RestRequestorTest {
 
             @SuppressWarnings("unchecked")
             final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
-            getsSoFar = Double.valueOf(jsonMap.get("GET").toString());
+            getsSoFar = Double.parseDouble(jsonMap.get("GET").toString());
 
             if (getsSoFar >= 50.0) {
                 break;
@@ -320,7 +320,7 @@ public class RestRequestorTest {
             + "does not exist or is not defined with the same peered mode"));
     }
 
-    private Double getStatsFromServer(final Client client, final String statToGet) {
+    private double getStatsFromServer(final Client client, final String statToGet) {
         final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
             .request("application/json").get();
 
@@ -329,6 +329,6 @@ public class RestRequestorTest {
 
         @SuppressWarnings("unchecked")
         final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
-        return Double.valueOf(jsonMap.get(statToGet).toString());
+        return Double.parseDouble(jsonMap.get(statToGet).toString());
     }
 }
index 23858a0..3748097 100644 (file)
@@ -1,7 +1,7 @@
 <!--
   ============LICENSE_START=======================================================
    Copyright (C) 2018 Ericsson. All rights reserved.
-   Copyright (C) 2019 Nordix Foundation.
+   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.
             <artifactId>mockito-all</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.awaitility</groupId>
+            <artifactId>awaitility</artifactId>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>org.onap.policy.models</groupId>
             <artifactId>policy-models-pdp</artifactId>
index 901cbea..42a3a55 100644 (file)
@@ -1,27 +1,30 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 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.
  * 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.
- * 
+ *
  * SPDX-License-Identifier: Apache-2.0
  * ============LICENSE_END=========================================================
  */
 
 package org.onap.policy.apex.service.engine.engdep;
 
+import static org.awaitility.Awaitility.await;
 import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
+
+import java.util.concurrent.TimeUnit;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -56,10 +59,10 @@ public class EngDepMessagingServiceTest {
     @Test
     public void testStartStop() throws ApexException {
         edMessagingService.start();
-        assertTrue(edMessagingService.isStarted());
+        await().atMost(1, TimeUnit.SECONDS).until(() -> edMessagingService.isStarted());
         assertFalse(edMessagingService.isStopped());
         edMessagingService.stop();
-        assertTrue(edMessagingService.isStopped());
+        await().atMost(1, TimeUnit.SECONDS).until(() -> edMessagingService.isStopped());
         assertFalse(edMessagingService.isStarted());
     }
 }