Fixing intermittent test failures in APEX 84/110684/2
authora.sreekumar <ajith.sreekumar@bell.ca>
Wed, 29 Jul 2020 09:42:43 +0000 (10:42 +0100)
committera.sreekumar <ajith.sreekumar@bell.ca>
Wed, 29 Jul 2020 15:28:18 +0000 (16:28 +0100)
Here the intermittent test failures are fixed by providing a latch to
wait until the DeploymentClient is started. This may need improvement,
but all these deployment classes will be removed soon, hence not worth
the effort.

Change-Id: I1090e991c0448b42af1d9cbd16d96ae1cb5774fd
Issue-ID: POLICY-2654
Signed-off-by: a.sreekumar <ajith.sreekumar@bell.ca>
core/core-deployment/src/main/java/org/onap/policy/apex/core/deployment/DeploymentClient.java
core/core-deployment/src/main/java/org/onap/policy/apex/core/deployment/EngineServiceFacade.java
core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/DummyDeploymentClient.java

index 9033ff4..c324664 100644 (file)
@@ -2,6 +2,7 @@
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
  *  Modifications Copyright (C) 2019 Nordix Foundation.
+ *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -26,8 +27,12 @@ import java.net.InetAddress;
 import java.net.URI;
 import java.net.UnknownHostException;
 import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+import lombok.Getter;
+import lombok.Setter;
 import org.onap.policy.apex.core.infrastructure.messaging.MessageHolder;
 import org.onap.policy.apex.core.infrastructure.messaging.MessageListener;
 import org.onap.policy.apex.core.infrastructure.messaging.MessagingService;
@@ -71,6 +76,9 @@ public class DeploymentClient implements Runnable {
     // Number of messages processed
     private long messagesSent = 0;
     private long messagesReceived = 0;
+    @Getter
+    @Setter
+    private AtomicReference<CountDownLatch> countDownLatch = new AtomicReference<>();
 
     /**
      * Instantiates a new deployment client.
@@ -81,6 +89,7 @@ public class DeploymentClient implements Runnable {
     public DeploymentClient(final String host, final int port) {
         this.host = host;
         this.port = port;
+        countDownLatch.set(new CountDownLatch(1));
     }
 
     /**
@@ -102,6 +111,7 @@ public class DeploymentClient implements Runnable {
 
             service.startConnection();
             started = true;
+            countDownLatch.get().countDown();
             LOGGER.debug("engine<-->deployment client thread started");
         } catch (final Exception e) {
             LOGGER.error("engine<-->deployment client thread exception", e);
@@ -198,6 +208,7 @@ public class DeploymentClient implements Runnable {
             service.stopConnection();
         }
         started = false;
+        countDownLatch.set(new CountDownLatch(1));
         LOGGER.debug("engine<-->deployment test client stopped . . .");
     }
 
index 1a09ffe..7d70960 100644 (file)
@@ -2,6 +2,7 @@
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
  *  Modifications Copyright (C) 2020 Nordix Foundation.
+ *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -26,7 +27,6 @@ import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
 import java.net.URL;
 import java.util.concurrent.TimeUnit;
-import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
 import org.onap.policy.apex.core.protocols.Message;
 import org.onap.policy.apex.core.protocols.engdep.messages.EngineServiceInfoResponse;
 import org.onap.policy.apex.core.protocols.engdep.messages.GetEngineInfo;
@@ -69,7 +69,6 @@ public class EngineServiceFacade {
     // The default message timeout and timeout increment (the amount of time between
     // polls) in
     // milliseconds
-    private static final int CLIENT_START_WAIT_INTERVAL = 100;
     private static final int REPLY_MESSAGE_TIMEOUT_DEFAULT = 10000;
     private static final int REPLY_MESSAGE_TIMEOUT_INCREMENT = 100;
 
@@ -125,12 +124,8 @@ public class EngineServiceFacade {
             clientThread.start();
 
             // Wait for the connection to come up
-            while (!client.isStarted()) {
-                if (clientThread.isAlive()) {
-                    ThreadUtilities.sleep(CLIENT_START_WAIT_INTERVAL);
-                } else {
-                    throw new ApexDeploymentException("could not handshake with server " + hostName + ":" + port);
-                }
+            if (!client.getCountDownLatch().get().await(5L, TimeUnit.SECONDS)) {
+                throw new ApexDeploymentException("could not handshake with server " + hostName + ":" + port);
             }
 
             LOGGER.debug("opened connection to server {}:{} . . .", hostName, port);
index ffb3d2c..5533809 100644 (file)
@@ -2,6 +2,7 @@
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2018 Ericsson. All rights reserved.
  *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
+ *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -27,6 +28,7 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.TimeUnit;
 import lombok.Getter;
@@ -84,7 +86,7 @@ public class DummyDeploymentClient extends DeploymentClient implements Runnable
         thisThread.setName(DeploymentClient.class.getName() + "-" + getHost() + ":" + getPort());
 
         started = true;
-
+        getCountDownLatch().get().countDown();
         // Loop forever, sending messages as they appear on the queue
         await().atLeast(50, TimeUnit.MILLISECONDS).until(() -> !(started && !thisThread.isInterrupted()));
         // Thread has been interrupted
@@ -202,6 +204,7 @@ public class DummyDeploymentClient extends DeploymentClient implements Runnable
             thisThread.interrupt();
         }
         started = false;
+        getCountDownLatch().set(new CountDownLatch(1));
     }
 
     /**