From 7ad528140eb987b2a7d66453b13e820a2d782245 Mon Sep 17 00:00:00 2001 From: Denes Nemeth Date: Sun, 6 May 2018 08:42:51 +0200 Subject: Fix concureny issues in test code Change-Id: I566131a121e7cd41ff235a103bbfcd4801b96d18 Signed-off-by: Denes Nemeth Issue-ID: VFC-728 --- .../vnfm/svnfm/nokia/util/SystemFunctions.java | 2 +- .../vnfm/svnfm/nokia/util/TestSystemFunctions.java | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) (limited to 'nokiav2') 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 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()); } /** -- cgit 1.2.3-korg