Fix concureny issues in test code 25/46325/1
authorDenes Nemeth <denes.nemeth@nokia.com>
Sun, 6 May 2018 06:42:51 +0000 (08:42 +0200)
committerDenes Nemeth <denes.nemeth@nokia.com>
Sun, 6 May 2018 06:43:55 +0000 (08:43 +0200)
Change-Id: I566131a121e7cd41ff235a103bbfcd4801b96d18
Signed-off-by: Denes Nemeth <denes.nemeth@nokia.com>
Issue-ID: VFC-728

nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/util/SystemFunctions.java
nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/util/TestSystemFunctions.java

index 468761c..51d76ac 100644 (file)
@@ -57,7 +57,7 @@ public class SystemFunctions {
             Thread.sleep(millis);
         } catch (InterruptedException e) {
             Thread.currentThread().interrupt();
-            throw new UserInvisibleError("Interrupted while sleep", e);
+            throw new UserInvisibleError("Interrupted while sleeping", e);
         }
     }
 
index 29dc616..1b34d28 100644 (file)
@@ -19,6 +19,7 @@ package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util;
 import java.util.Base64;
 import java.util.HashSet;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
 import org.junit.Test;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.NokiaSvnfmApplication;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.Useless;
@@ -43,28 +44,40 @@ public class TestSystemFunctions {
      * test interrupted sleep
      */
     @Test
+    @SuppressWarnings("squid:S2925") //testing asynchronous execution
     public void testInterruptedSleep() throws Exception {
+        AtomicBoolean entered = new AtomicBoolean(false);
         long start = System.currentTimeMillis();
         Set<RuntimeException> exceptions = new HashSet<>();
         class Inter extends Thread {
             @Override
             public void run() {
                 try {
-                    SystemFunctions.systemFunctions().sleep(10000);
+                    entered.set(true);
+                    SystemFunctions.systemFunctions().sleep(1000000);
                 } catch (RuntimeException e) {
                     exceptions.add(e);
+                    throw e;
                 }
             }
         }
         Inter inter = new Inter();
         inter.start();
+        //wait for thread to enter waiting
+        while(!entered.get() && inter.getState() != Thread.State.TIMED_WAITING && (System.currentTimeMillis() < start + 60*1000) ){
+            Thread.sleep(10);
+        }
+        if(!(System.currentTimeMillis() < start + 60*1000)){
+            throw new RuntimeException("Thread did not enter waiting state");
+        }
         //when
         inter.interrupt();
-
         //verify
-        while (exceptions.size() != 1) {
-
+        while (exceptions.size() != 1 && (System.currentTimeMillis() < start + 60*1000)) {
+            Thread.sleep(10);
         }
+        assertEquals(1, exceptions.size());
+        assertEquals("Interrupted while sleeping", exceptions.iterator().next().getMessage());
     }
 
     /**