From 0b8f4922ebebcf421fb5bc6c25b22380edef6270 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Wed, 13 Jun 2018 12:23:26 -0400 Subject: IntegrityAudit - remove latches from non-test code Change-Id: I5d5cc7d581f78d5551e2fe7447720403bb63ada2 Issue-ID: POLICY-908 Signed-off-by: Jim Hahn --- .../org/onap/policy/common/ia/AuditThread.java | 80 +++++++--------------- 1 file changed, 26 insertions(+), 54 deletions(-) (limited to 'integrity-audit/src/main/java/org/onap/policy/common/ia/AuditThread.java') diff --git a/integrity-audit/src/main/java/org/onap/policy/common/ia/AuditThread.java b/integrity-audit/src/main/java/org/onap/policy/common/ia/AuditThread.java index f1839b12..58ed8b99 100644 --- a/integrity-audit/src/main/java/org/onap/policy/common/ia/AuditThread.java +++ b/integrity-audit/src/main/java/org/onap/policy/common/ia/AuditThread.java @@ -25,10 +25,7 @@ import java.util.Comparator; 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.TimeUnit; - import org.onap.policy.common.ia.jpa.IntegrityAuditEntity; import org.onap.policy.common.logging.eelf.MessageCodes; import org.onap.policy.common.logging.flexlogger.FlexLogger; @@ -107,18 +104,6 @@ public class AuditThread extends Thread { */ private IntegrityAudit integrityAudit; - /** - * A latch is taken from this queue before starting an audit. May be {@code null}. Used by JUnit - * tests. - */ - private BlockingQueue auditLatchQueue; - - /** - * Latch to be decremented when the next audit completes. May be {@code null}. Used by JUnit - * tests to wait for an audit to complete. - */ - private CountDownLatch auditCompletionLatch = null; - /** * AuditThread constructor. * @@ -133,7 +118,7 @@ public class AuditThread extends Thread { int integrityAuditPeriodSeconds, IntegrityAudit integrityAudit) throws IntegrityAuditException { this(resourceName, persistenceUnit, properties, TimeUnit.SECONDS.toMillis(integrityAuditPeriodSeconds), - integrityAudit, null); + integrityAudit); } /** @@ -148,13 +133,12 @@ public class AuditThread extends Thread { * @throws IntegrityAuditException if an error occurs */ public AuditThread(String resourceName, String persistenceUnit, Properties properties, long integrityAuditMillis, - IntegrityAudit integrityAudit, BlockingQueue queue) throws IntegrityAuditException { + IntegrityAudit integrityAudit) throws IntegrityAuditException { this.resourceName = resourceName; this.persistenceUnit = persistenceUnit; this.properties = properties; this.integrityAuditPeriodMillis = integrityAuditMillis; this.integrityAudit = integrityAudit; - this.auditLatchQueue = queue; /* * The DbDAO Constructor registers this node in the IntegrityAuditEntity table. Each @@ -174,14 +158,8 @@ public class AuditThread extends Thread { logger.info("AuditThread.run: Entering"); try { - /* - * For JUnit testing: wait for the first latch, decrement it to indicate that the thread - * has started, and then wait for the next latch, before we actually start doing - * anything. These simply return if there is no latch queue defined. - */ - getNextLatch(); - decrementLatch(); - getNextLatch(); + // for junit testing + runStarted(); /* * Triggers change in designation, unless no other viable candidate. @@ -284,11 +262,8 @@ public class AuditThread extends Thread { * property, otherwise just sleep the normal interval. */ if (auditCompleted) { - // indicate that an audit has completed - decrementLatch(); - - // don't start the next audit cycle until a latch has been provided - getNextLatch(); + // for junit testing: indicate that an audit has completed + auditCompleted(); if (logger.isDebugEnabled()) { logger.debug("AuditThread.run: Audit completed; resourceName=" + this.resourceName @@ -341,29 +316,6 @@ public class AuditThread extends Thread { logger.info("AuditThread.run: Exiting"); } - /** - * Gets the next audit-completion latch from the queue. Blocks, if the queue is empty. - * - * @throws InterruptedException if interrupted while waiting - */ - private void getNextLatch() throws InterruptedException { - BlockingQueue queue = this.auditLatchQueue; - if (queue != null) { - this.auditCompletionLatch = queue.take(); - } - } - - /** - * Decrements the current audit-completion latch, if any. - */ - private void decrementLatch() { - CountDownLatch latch = this.auditCompletionLatch; - if (latch != null) { - this.auditCompletionLatch = null; - latch.countDown(); - } - } - /** * Determines if an exception is an InterruptedException or was caused by an * InterruptedException. @@ -787,6 +739,26 @@ public class AuditThread extends Thread { } + /** + * Indicates that the {@link #run()} method has started. This method simply returns, + * and may overridden by junit tests. + * + * @throws InterruptedException + */ + public void runStarted() throws InterruptedException { + // does nothing + } + + /** + * Indicates that an audit has completed. This method simply returns, and may + * overridden by junit tests. + * + * @throws InterruptedException + */ + public void auditCompleted() throws InterruptedException { + // does nothing + } + /** * Adjusts the thread-sleep-interval to be used when an audit has not been completed. * Used by JUnit tests. -- cgit 1.2.3-korg