diff options
author | Denes Nemeth <denes.nemeth@nokia.com> | 2018-05-06 08:42:51 +0200 |
---|---|---|
committer | Denes Nemeth <denes.nemeth@nokia.com> | 2018-05-06 08:43:55 +0200 |
commit | 7ad528140eb987b2a7d66453b13e820a2d782245 (patch) | |
tree | 88c7c11d6ae323e9b4e149f0bac964eb169153c6 /nokiav2 | |
parent | 2205fe1c99f84709d78edf202a24ad1c240f8a86 (diff) |
Fix concureny issues in test code
Change-Id: I566131a121e7cd41ff235a103bbfcd4801b96d18
Signed-off-by: Denes Nemeth <denes.nemeth@nokia.com>
Issue-ID: VFC-728
Diffstat (limited to 'nokiav2')
2 files changed, 18 insertions, 5 deletions
diff --git a/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/util/SystemFunctions.java b/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/util/SystemFunctions.java index 468761c3..51d76ac7 100644 --- a/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/util/SystemFunctions.java +++ b/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/util/SystemFunctions.java @@ -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); } } diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/util/TestSystemFunctions.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/util/TestSystemFunctions.java index 29dc616f..1b34d28b 100644 --- a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/util/TestSystemFunctions.java +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/util/TestSystemFunctions.java @@ -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()); } /** |