Fix random failures in ManualClientEndTest
[policy/engine.git] / PolicyEngineAPI / src / main / java / org / onap / policy / std / ManualClientEnd.java
index 2fe6dc0..b274fa1 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * PolicyEngineAPI
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. 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.
@@ -22,7 +22,6 @@ package org.onap.policy.std;
 
 import java.net.URI;
 import java.util.concurrent.CountDownLatch;
-import javax.websocket.ClientEndpoint;
 import org.java_websocket.client.WebSocketClient;
 import org.java_websocket.handshake.ServerHandshake;
 import org.onap.policy.api.NotificationScheme;
@@ -33,13 +32,12 @@ import org.onap.policy.common.logging.flexlogger.Logger;
 import org.onap.policy.std.StdPDPNotification;
 import org.onap.policy.xacml.api.XACMLErrorConstants;
 
-@ClientEndpoint
 public class ManualClientEnd extends WebSocketClient {
-    private static CountDownLatch latch;
-    private static StdPDPNotification notification = null;
-    private static String resultJson = null;
-    private static Logger logger = FlexLogger.getLogger(ManualClientEnd.class.getName());
-    private static ManualClientEnd client;
+    private static volatile CountDownLatch latch;
+    private static volatile StdPDPNotification notification = null;
+    private static volatile String resultJson = null;
+    private static final Logger logger = FlexLogger.getLogger(ManualClientEnd.class.getName());
+    private static volatile ManualClientEnd client;
 
     public ManualClientEnd(URI serverUri) {
         super(serverUri);
@@ -63,12 +61,11 @@ public class ManualClientEnd extends WebSocketClient {
         logger.info("Manual Notification Recieved Message from : " + getURI() + ", Notification: " + message);
         ManualClientEnd.resultJson = message;
         try {
-            ManualClientEnd.notification = NotificationUnMarshal.notificationJSON(message);
-            latch.countDown();
+            ManualClientEnd.notification = NotificationUnMarshal.notificationJSON(message);     
         } catch (Exception e) {
             logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
-            latch.countDown();
         }
+        latch.countDown();
     }
 
     @Override
@@ -93,9 +90,10 @@ public class ManualClientEnd extends WebSocketClient {
 
         try {
             client = new ManualClientEnd(new URI(url + "notifications"));
+            client.setConnectionLostTimeout(0);
             client.connect();
             latch.await();
-            client.close();
+            client.closeBlocking();
         } catch (Exception e) {
             logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
         }