From 6b8efd3ef507f53b7cd39ad2ba67a8db6fe73cdd Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Thu, 14 Jun 2018 09:05:50 -0400 Subject: IntegrityMonitory - remove latch for non-test code Change-Id: I95ebcf8aabfa01eb6453a4ba5dd88d11c8f025c9 Issue-ID: POLICY-908 Signed-off-by: Jim Hahn --- .../policy/common/im/IntegrityMonitorTest.java | 47 ++++++++++++++++------ .../policy/common/im/IntegrityMonitorTestBase.java | 13 +++--- 2 files changed, 40 insertions(+), 20 deletions(-) (limited to 'integrity-monitor/src/test') diff --git a/integrity-monitor/src/test/java/org/onap/policy/common/im/IntegrityMonitorTest.java b/integrity-monitor/src/test/java/org/onap/policy/common/im/IntegrityMonitorTest.java index 7f1e5516..091dcc91 100644 --- a/integrity-monitor/src/test/java/org/onap/policy/common/im/IntegrityMonitorTest.java +++ b/integrity-monitor/src/test/java/org/onap/policy/common/im/IntegrityMonitorTest.java @@ -22,23 +22,19 @@ package org.onap.policy.common.im; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; - import java.util.Date; import java.util.List; import java.util.Properties; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.LinkedBlockingQueue; - +import java.util.concurrent.Semaphore; import javax.persistence.EntityTransaction; import javax.persistence.Query; import javax.persistence.TemporalType; - import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import org.onap.policy.common.im.IntegrityMonitor.Factory; import org.onap.policy.common.im.jpa.ForwardProgressEntity; import org.onap.policy.common.im.jpa.ResourceRegistrationEntity; import org.onap.policy.common.im.jpa.StateManagementEntity; @@ -57,7 +53,8 @@ public class IntegrityMonitorTest extends IntegrityMonitorTestBase { private static EntityTransaction et; private static String resourceName; - private BlockingQueue queue; + private Semaphore monitorSem; + private Semaphore junitSem; /** * Set up for test class. @@ -900,9 +897,36 @@ public class IntegrityMonitorTest extends IntegrityMonitorTestBase { private IntegrityMonitor makeMonitor(String resourceName, Properties myProp) throws Exception { IntegrityMonitor.deleteInstance(); - queue = new LinkedBlockingQueue<>(); + monitorSem = new Semaphore(0); + junitSem = new Semaphore(0); + + Factory factory = new IntegrityMonitor.Factory() { + + @Override + public void doSleep(long sleepMs) throws InterruptedException { + /* + * No need to sleep, as the thread won't progress until the + * semaphore is released. + */ + } + + @Override + public void runStarted() throws InterruptedException { + monitorSem.acquire(); + + junitSem.release(); + monitorSem.acquire(); + } + + @Override + public void monitorCompleted() throws InterruptedException { + junitSem.release(); + monitorSem.acquire(); + } + + }; - IntegrityMonitor im = IntegrityMonitor.getInstance(resourceName, myProp, queue); + IntegrityMonitor im = IntegrityMonitor.getInstance(resourceName, myProp, factory); // wait for the monitor thread to start waitStep(); @@ -916,8 +940,7 @@ public class IntegrityMonitorTest extends IntegrityMonitorTestBase { * @throws InterruptedException if the thread is interrupted */ private void waitStep() throws InterruptedException { - CountDownLatch latch = new CountDownLatch(1); - queue.offer(latch); - waitLatch(latch); + monitorSem.release(); + waitSem(junitSem); } } diff --git a/integrity-monitor/src/test/java/org/onap/policy/common/im/IntegrityMonitorTestBase.java b/integrity-monitor/src/test/java/org/onap/policy/common/im/IntegrityMonitorTestBase.java index 0c8259b7..e5562306 100644 --- a/integrity-monitor/src/test/java/org/onap/policy/common/im/IntegrityMonitorTestBase.java +++ b/integrity-monitor/src/test/java/org/onap/policy/common/im/IntegrityMonitorTestBase.java @@ -22,17 +22,14 @@ package org.onap.policy.common.im; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; - import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; -import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; - import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; - import org.onap.policy.common.utils.jpa.EntityTransCloser; import org.onap.policy.common.utils.test.log.logback.ExtractAppender; import org.slf4j.Logger; @@ -243,14 +240,14 @@ public class IntegrityMonitorTestBase { } /** - * Waits for a latch to reach zero. + * Waits for a semaphore to be acquired * - * @param latch the latch + * @param sem the latch * @throws InterruptedException if the thread is interrupted * @throws AssertionError if the latch did not reach zero in the allotted time */ - protected void waitLatch(CountDownLatch latch) throws InterruptedException { - assertTrue(latch.await(WAIT_MS, TimeUnit.SECONDS)); + protected void waitSem(Semaphore sem) throws InterruptedException { + assertTrue(sem.tryAcquire(WAIT_MS, TimeUnit.MILLISECONDS)); } /** -- cgit 1.2.3-korg