diff options
author | Jim Hahn <jrh3@att.com> | 2018-06-21 16:23:19 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2018-06-21 17:17:10 -0400 |
commit | 9afa523e5147c971a0d8d0405361f3c3e8faa6ce (patch) | |
tree | 05566af1d59b8da6338435384a0fed481c76b179 /integrity-audit/src/main/java/org/onap/policy/common/ia/DbAudit.java | |
parent | ec62d8d34ae4724b81d29f6314951c277f423ba4 (diff) |
IntegrityAuditor: remove sleep from junit tests
Modified the code to use a CurrentTime object for its "time" operations
(e.g., sleep(), currentTimeInMillis()). Modified junit tests to replace
the CurrentTime object with TestTime objects so they don't actually
do any sleeping.
Reformat "commit" message.
Remove TODO from junit test.
Init testTime in junit setUp().
Add AuditorTime and test classes.
Change "latch" to "semaphore" in comments.
Change time units in junit test from SECONDS to MILLISECONDS.
Add sleep() method to auditor test class.
Reorder field qualifiers.
Change utils scope to "compile" in pom.
Change-Id: I8aa8b642b315156c00422192e4aa8e47b4503c2f
Issue-ID: POLICY-908
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'integrity-audit/src/main/java/org/onap/policy/common/ia/DbAudit.java')
-rw-r--r-- | integrity-audit/src/main/java/org/onap/policy/common/ia/DbAudit.java | 97 |
1 files changed, 12 insertions, 85 deletions
diff --git a/integrity-audit/src/main/java/org/onap/policy/common/ia/DbAudit.java b/integrity-audit/src/main/java/org/onap/policy/common/ia/DbAudit.java index 4c4104f6..d7fbd749 100644 --- a/integrity-audit/src/main/java/org/onap/policy/common/ia/DbAudit.java +++ b/integrity-audit/src/main/java/org/onap/policy/common/ia/DbAudit.java @@ -1,4 +1,4 @@ -/*- +/* * ============LICENSE_START======================================================= * Integrity Audit * ================================================================================ @@ -29,9 +29,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Properties; import java.util.Set; - import javax.persistence.Table; - import org.apache.commons.lang3.SerializationUtils; import org.apache.commons.lang3.builder.RecursiveToStringStyle; import org.apache.commons.lang3.builder.ReflectionToStringBuilder; @@ -48,8 +46,8 @@ public class DbAudit { private static final Logger logger = FlexLogger.getLogger(DbAudit.class); - private static long dbAuditUpdateMillis = 5000L; - private static long dbAuditSleepMillis = 2000L; + private static final long DB_AUDIT_UPDATE_MS = 5000L; + private static final long DB_AUDIT_SLEEP_MS = 2000L; DbDAO dbDao = null; @@ -145,7 +143,7 @@ public class DbAudit { Map<String, Set<Object>> misMatchedMap = new HashMap<>(); // We need to keep track of how long the audit is taking - long startTime = System.currentTimeMillis(); + long startTime = AuditorTime.getInstance().getMillis(); // Retrieve all instances of the class for each node if (logger.isDebugEnabled()) { @@ -209,15 +207,15 @@ public class DbAudit { } // end for (IntegrityAuditEntity iae : iaeList) // Time check - if ((System.currentTimeMillis() - startTime) >= dbAuditUpdateMillis) { + if ((AuditorTime.getInstance().getMillis() - startTime) >= DB_AUDIT_UPDATE_MS) { // update the timestamp dbDao.setLastUpdated(); // reset the startTime - startTime = System.currentTimeMillis(); + startTime = AuditorTime.getInstance().getMillis(); } else { // sleep a couple seconds to break up the activity if (logger.isDebugEnabled()) { - logger.debug("dbAudit: Sleeping " + dbAuditSleepMillis + "ms"); + logger.debug("dbAudit: Sleeping " + DB_AUDIT_SLEEP_MS + "ms"); } sleep(); if (logger.isDebugEnabled()) { @@ -244,7 +242,7 @@ public class DbAudit { // again classNameSet = new HashSet<>(misMatchedMap.keySet()); // We need to keep track of how long the audit is taking - startTime = System.currentTimeMillis(); + startTime = AuditorTime.getInstance().getMillis(); // Retrieve all instances of the class for each node if (logger.isDebugEnabled()) { @@ -315,15 +313,15 @@ public class DbAudit { } } // Time check - if ((System.currentTimeMillis() - startTime) >= dbAuditUpdateMillis) { + if ((AuditorTime.getInstance().getMillis() - startTime) >= DB_AUDIT_UPDATE_MS) { // update the timestamp dbDao.setLastUpdated(); // reset the startTime - startTime = System.currentTimeMillis(); + startTime = AuditorTime.getInstance().getMillis(); } else { // sleep a couple seconds to break up the activity if (logger.isDebugEnabled()) { - logger.debug("dbAudit: Second comparison; sleeping " + dbAuditSleepMillis + "ms"); + logger.debug("dbAudit: Second comparison; sleeping " + DB_AUDIT_SLEEP_MS + "ms"); } sleep(); if (logger.isDebugEnabled()) { @@ -352,7 +350,7 @@ public class DbAudit { */ private void sleep() throws IntegrityAuditException { try { - Thread.sleep(dbAuditSleepMillis); + AuditorTime.getInstance().sleep(DB_AUDIT_SLEEP_MS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); @@ -361,40 +359,6 @@ public class DbAudit { } /** - * dbAuditSimulate simulates the DB audit. - * - * @param resourceName the resouce name - * @param persistenceUnit the persistence unit - * @param simulationIterations the simulations iterations - * @param simulationIntervalMs the simulation interval in milliseconds - * @throws DbAuditException if an error occurs - */ - public void dbAuditSimulate(String resourceName, String persistenceUnit, long simulationIterations, - long simulationIntervalMs) throws DbAuditException { - - try { - logger.info("dbAuditSimulate: Starting audit simulation for resourceName=" + resourceName - + ", persistenceUnit=" + persistenceUnit); - - for (int i = 0; i < simulationIterations; i++) { - dbDao.setLastUpdated(); - logger.info("dbAuditSimulate: i=" + i + ", sleeping " + simulationIntervalMs + "ms"); - Thread.sleep(simulationIntervalMs); - } - - logger.info("dbAuditSimulate: Finished audit simulation for resourceName=" + resourceName - + ", persistenceUnit=" + persistenceUnit); - - } catch (DbDaoTransactionException e) { - throw new DbAuditException(e); - - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - throw new DbAuditException(e); - } - } - - /** * compareEntries() will compare the lists of entries from the DB. * * @param myEntries the entries @@ -496,41 +460,4 @@ public class DbAudit { throw new IntegrityAuditException(e); } } - - /** - * Gets the audit-update time. - * - * @return the audit-update time, in milliseconds - */ - protected static long getDbAuditUpdateMillis() { - return dbAuditUpdateMillis; - } - - /** - * Sets the audit-update time. - * - * @param dbAuditUpdateMillis the new audit update time, in milliseconds - */ - protected static void setDbAuditUpdateMillis(long dbAuditUpdateMillis) { - DbAudit.dbAuditUpdateMillis = dbAuditUpdateMillis; - } - - /** - * Gets the audit-sleep time. - * - * @return the audit-sleep time, in milliseconds - */ - protected static long getDbAuditSleepMillis() { - return dbAuditSleepMillis; - } - - /** - * Sets the audit-sleep time. - * - * @param dbAuditSleepMillis the new audit sleep time, in milliseconds - */ - protected static void setDbAuditSleepMillis(long dbAuditSleepMillis) { - DbAudit.dbAuditSleepMillis = dbAuditSleepMillis; - } - } |