diff options
author | Jim Hahn <jrh3@att.com> | 2019-08-14 17:31:50 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2019-08-21 13:49:54 -0400 |
commit | dfe8fa8bc3e75c186589d21b619baa55454ef8a2 (patch) | |
tree | ca3c7dbbe53815afdacb1c97e62ee97ace3f867c /feature-active-standby-management/src/main/java/org/onap | |
parent | 59e9b9a8b56d563814ef21a23716959f772f9194 (diff) |
Use pseudo time for junits
Modified feature-active-standby-management and feature-lifecycle
to be able to use TestTimeMulti, eliminating the need for sleep()
calls in the junit tests and speeding the tests up significantly.
Also modified feature-active-standby-management to use a memory DB
for its junit tests.
Change-Id: I6d7ae61bb73cbb19ff405b8d9fb660e92732edbb
Issue-ID: POLICY-1968
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'feature-active-standby-management/src/main/java/org/onap')
6 files changed, 73 insertions, 49 deletions
diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyFeature.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyFeature.java index d7c153db..91d30d74 100644 --- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyFeature.java +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyFeature.java @@ -21,14 +21,12 @@ package org.onap.policy.drools.activestandby; import java.io.IOException; -import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.Properties; - import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; - +import org.onap.policy.common.im.MonitorTime; import org.onap.policy.drools.core.PolicySessionFeatureApi; import org.onap.policy.drools.features.PolicyEngineFeatureApi; import org.onap.policy.drools.statemanagement.StateManagementFeatureApi; @@ -163,7 +161,7 @@ public class ActiveStandbyFeature implements ActiveStandbyFeatureApi, synchronized (myPdpSync) { if (myPdp == null) { - myPdp = new DroolsPdpImpl(resourceName,false,4,new Date()); + myPdp = new DroolsPdpImpl(resourceName,false,4,MonitorTime.getInstance().getDate()); } String siteName = ActiveStandbyProperties.getProperty(ActiveStandbyProperties.SITE_NAME); if (siteName == null) { diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpEntity.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpEntity.java index 38ab6e4b..4175068f 100644 --- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpEntity.java +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpEntity.java @@ -32,6 +32,7 @@ import javax.persistence.Temporal; import javax.persistence.TemporalType; import lombok.Getter; import lombok.Setter; +import org.onap.policy.common.im.MonitorTime; import org.onap.policy.drools.activestandby.DroolsPdpObject; @Entity @@ -72,7 +73,7 @@ public class DroolsPdpEntity extends DroolsPdpObject implements Serializable { * Constructor. */ public DroolsPdpEntity() { - updatedDate = new Date(); + updatedDate = MonitorTime.getInstance().getDate(); //When this is translated to a TimeStamp in MySQL, it assumes the date is relative //to the local timezone. So, a value of Date(0) is actually Dec 31 18:00:00 CST 1969 //which is an invalid value for the MySql TimeStamp diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsElectionHandler.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsElectionHandler.java index 85cf88b3..5308cbe6 100644 --- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsElectionHandler.java +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsElectionHandler.java @@ -26,8 +26,9 @@ import java.util.Date; import java.util.List; import java.util.Timer; import java.util.TimerTask; - +import org.onap.policy.common.im.MonitorTime; import org.onap.policy.common.im.StateManagement; +import org.onap.policy.common.utils.time.CurrentTime; import org.onap.policy.drools.statemanagement.StateManagementFeatureApi; import org.onap.policy.drools.statemanagement.StateManagementFeatureApiConstants; import org.slf4j.Logger; @@ -73,6 +74,8 @@ public class DroolsPdpsElectionHandler implements ThreadRunningChecker { private StateManagementFeatureApi stateManagementFeature; + private final CurrentTime currentTime = MonitorTime.getInstance(); + private static boolean isUnitTesting = false; private static boolean isStalled = false; @@ -112,14 +115,14 @@ public class DroolsPdpsElectionHandler implements ThreadRunningChecker { logger.error("Could not get pdpUpdateInterval property. Using default {} ", pdpUpdateInterval, e); } - Date now = new Date(); + Date now = currentTime.getDate(); // Retrieve the ms since the epoch final long nowMs = now.getTime(); // Create the timer which will update the updateDate in DroolsPdpEntity table. // This is the heartbeat - updateWorker = new Timer(); + updateWorker = Factory.getInstance().makeTimer(); // Schedule the TimerUpdateClass to run at 100 ms and run at pdpCheckInterval ms thereafter // NOTE: The first run of the TimerUpdateClass results in myPdp being added to the @@ -127,7 +130,7 @@ public class DroolsPdpsElectionHandler implements ThreadRunningChecker { updateWorker.scheduleAtFixedRate(new TimerUpdateClass(), 100, pdpCheckInterval); // Create the timer which will run the election algorithm - waitTimer = new Timer(); + waitTimer = Factory.getInstance().makeTimer(); // Schedule it to start in startMs ms // (so it will run after the updateWorker and run at pdpUpdateInterval ms thereafter @@ -264,7 +267,7 @@ public class DroolsPdpsElectionHandler implements ThreadRunningChecker { logger.debug("DesignatedWaiter.run: myPdp: {}; Returning, isDesignated= {}", isDesignated, myPdp.getPdpId()); - Date tmpDate = new Date(); + Date tmpDate = currentTime.getDate(); logger.debug("DesignatedWaiter.run (end of run) waitTimerLastRunDate = {}", tmpDate); waitTimerLastRunDate = tmpDate; @@ -559,7 +562,7 @@ public class DroolsPdpsElectionHandler implements ThreadRunningChecker { pdpsConnector.setDesignated(myPdp,false); isDesignated = false; - waitTimerLastRunDate = new Date(); + waitTimerLastRunDate = currentTime.getDate(); logger.debug("DesignatedWaiter.run (designatedPdp == null) waitTimerLastRunDate = {}", waitTimerLastRunDate); myPdp.setUpdatedDate(waitTimerLastRunDate); @@ -573,7 +576,7 @@ public class DroolsPdpsElectionHandler implements ThreadRunningChecker { try { //Keep the order like this. StateManagement is last since it triggers controller init myPdp.setDesignated(true); - myPdp.setDesignatedDate(new Date()); + myPdp.setDesignatedDate(currentTime.getDate()); pdpsConnector.setDesignated(myPdp, true); isDesignated = true; String standbyStatus = stateManagementFeature.getStandbyStatus(); @@ -613,7 +616,7 @@ public class DroolsPdpsElectionHandler implements ThreadRunningChecker { } } - waitTimerLastRunDate = new Date(); + waitTimerLastRunDate = currentTime.getDate(); logger.debug("DesignatedWaiter.run (designatedPdp.getPdpId().equals(myPdp.getPdpId())) " + "waitTimerLastRunDate = " + waitTimerLastRunDate); myPdp.setUpdatedDate(waitTimerLastRunDate); @@ -920,7 +923,7 @@ public class DroolsPdpsElectionHandler implements ThreadRunningChecker { synchronized (checkWaitTimerLock) { try { logger.debug("checkWaitTimer: entry"); - Date now = new Date(); + Date now = currentTime.getDate(); long nowMs = now.getTime(); long waitTimerMs = waitTimerLastRunDate.getTime(); @@ -951,7 +954,7 @@ public class DroolsPdpsElectionHandler implements ThreadRunningChecker { } private long getDWaiterStartMs() { - Date now = new Date(); + Date now = currentTime.getDate(); // Retrieve the ms since the epoch long nowMs = now.getTime(); diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/Factory.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/Factory.java new file mode 100644 index 00000000..fae70074 --- /dev/null +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/Factory.java @@ -0,0 +1,40 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.activestandby; + +import java.util.Timer; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.Setter; + +/** + * Factory for creating various objects. + */ +public class Factory { + + @Getter + @Setter(AccessLevel.PROTECTED) + private static Factory instance = new Factory(); + + public Timer makeTimer() { + return new Timer(); + } +} diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/JpaDroolsPdpsConnector.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/JpaDroolsPdpsConnector.java index ed53f55c..1830d055 100644 --- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/JpaDroolsPdpsConnector.java +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/JpaDroolsPdpsConnector.java @@ -30,7 +30,8 @@ import javax.persistence.EntityManagerFactory; import javax.persistence.FlushModeType; import javax.persistence.LockModeType; import javax.persistence.Query; - +import org.onap.policy.common.im.MonitorTime; +import org.onap.policy.common.utils.time.CurrentTime; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -43,6 +44,8 @@ public class JpaDroolsPdpsConnector implements DroolsPdpsConnector { private static final Logger logger = LoggerFactory.getLogger(JpaDroolsPdpsConnector.class); private EntityManagerFactory emf; + private final CurrentTime currentTime = MonitorTime.getInstance(); + //not sure if we want to use the same entity manager factory //for drools session and pass it in here, or create a new one @@ -114,7 +117,7 @@ public class JpaDroolsPdpsConnector implements DroolsPdpsConnector { if (droolsPdpsList.size() == 1 && (droolsPdpsList.get(0) instanceof DroolsPdpEntity)) { droolsPdpEntity = (DroolsPdpEntity)droolsPdpsList.get(0); em.refresh(droolsPdpEntity); //Make sure we have current values - Date currentDate = new Date(); + Date currentDate = currentTime.getDate(); long difference = currentDate.getTime() - droolsPdpEntity.getUpdatedDate().getTime(); //just set some kind of default here long pdpTimeout = 15000; @@ -156,7 +159,7 @@ public class JpaDroolsPdpsConnector implements DroolsPdpsConnector { droolsPdpEntity.setDesignated(pdp.isDesignated()); //The isDesignated value is not the same and the new one == true if (pdp.isDesignated()) { - droolsPdpEntity.setDesignatedDate(new Date()); + droolsPdpEntity.setDesignatedDate(currentTime.getDate()); } } em.getTransaction().commit(); @@ -255,7 +258,7 @@ public class JpaDroolsPdpsConnector implements DroolsPdpsConnector { if (designated) { em.refresh(droolsPdpEntity); //make sure we get the DB value if (!droolsPdpEntity.isDesignated()) { - droolsPdpEntity.setDesignatedDate(new Date()); + droolsPdpEntity.setDesignatedDate(currentTime.getDate()); } } @@ -369,7 +372,7 @@ public class JpaDroolsPdpsConnector implements DroolsPdpsConnector { // time box that may be. // If the the PDP is not current, we should mark it as not primary in // the database - Date currentDate = new Date(); + Date currentDate = currentTime.getDate(); long difference = currentDate.getTime() - pdp.getUpdatedDate().getTime(); // just set some kind of default here diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/PmStandbyStateChangeNotifier.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/PmStandbyStateChangeNotifier.java index 7669cc22..735e3a2a 100644 --- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/PmStandbyStateChangeNotifier.java +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/PmStandbyStateChangeNotifier.java @@ -20,31 +20,12 @@ package org.onap.policy.drools.activestandby; -/* - * Per MultiSite_v1-10.ppt: - * - * Extends the StateChangeNotifier class and overwrites the abstract handleStateChange() method to get state changes - * and do the following: - * - * When the Standby Status changes (from providingservice) to hotstandby or coldstandby, - * the Active/Standby selection algorithm must stand down if the PDP-D is currently the lead/active node - * and allow another PDP-D to take over. It must also call lock on all engines in the engine management. - * - * When the Standby Status changes from (hotstandby) to coldstandby, the Active/Standby algorithm must NOT assume - * the active/lead role. - * - * When the Standby Status changes (from coldstandby or providingservice) to hotstandby, - * the Active/Standby algorithm may assume the active/lead role if the active/lead fails. - * - * When the Standby Status changes to providingservice (from hotstandby or coldstandby) call unlock on all - * engines in the engine management layer. - */ -import java.util.Date; import java.util.Timer; import java.util.TimerTask; - +import org.onap.policy.common.im.MonitorTime; import org.onap.policy.common.im.StateChangeNotifier; import org.onap.policy.common.im.StateManagement; +import org.onap.policy.common.utils.time.CurrentTime; import org.onap.policy.drools.system.PolicyEngine; import org.onap.policy.drools.system.PolicyEngineConstants; import org.slf4j.Logger; @@ -89,6 +70,8 @@ public class PmStandbyStateChangeNotifier extends StateChangeNotifier { private long waitInterval; private boolean isNowActivating; private String previousStandbyStatus; + private final CurrentTime currentTime = MonitorTime.getInstance(); + private final Factory timerFactory = Factory.getInstance(); public static final String NONE = "none"; public static final String UNSUPPORTED = "unsupported"; public static final String HOTSTANDBY_OR_COLDSTANDBY = "hotstandby_or_coldstandby"; @@ -101,7 +84,7 @@ public class PmStandbyStateChangeNotifier extends StateChangeNotifier { pdpUpdateInterval = Integer.parseInt(ActiveStandbyProperties.getProperty(ActiveStandbyProperties.PDP_UPDATE_INTERVAL)); isWaitingForActivation = false; - startTimeWaitingForActivationMs = new Date().getTime(); + startTimeWaitingForActivationMs = currentTime.getMillis(); // delay the activate so the DesignatedWaiter can run twice - give it an extra 2 seconds waitInterval = 2 * pdpUpdateInterval + 2000L; isNowActivating = false; @@ -222,11 +205,11 @@ public class PmStandbyStateChangeNotifier extends StateChangeNotifier { // Just in case there is an old timer hanging around logger.debug("handleStateChange: PROVIDING_SERVICE cancelling delayActivationTimer."); cancelTimer(); - delayActivateTimer = makeTimer(); + delayActivateTimer = timerFactory.makeTimer(); // delay the activate so the DesignatedWaiter can run twice delayActivateTimer.schedule(new DelayActivateClass(), waitInterval); isWaitingForActivation = true; - startTimeWaitingForActivationMs = new Date().getTime(); + startTimeWaitingForActivationMs = currentTime.getMillis(); logger.debug("handleStateChange: PROVIDING_SERVICE scheduling delayActivationTimer in {} ms", waitInterval); } else { @@ -244,7 +227,7 @@ public class PmStandbyStateChangeNotifier extends StateChangeNotifier { if (isWaitingForActivation) { logger.debug("handleStateChange: PROVIDING_SERVICE isWaitingForActivation = {}", isWaitingForActivation); - long now = new Date().getTime(); + long now = currentTime.getMillis(); long waitTimeMs = now - startTimeWaitingForActivationMs; if (waitTimeMs > 3 * waitInterval) { logger.debug("handleStateChange: PROVIDING_SERVICE looks like the activation wait timer " @@ -327,8 +310,4 @@ public class PmStandbyStateChangeNotifier extends StateChangeNotifier { protected PolicyEngine getPolicyEngineManager() { return PolicyEngineConstants.getManager(); } - - protected Timer makeTimer() { - return new Timer(); - } } |