aboutsummaryrefslogtreecommitdiffstats
path: root/feature-lifecycle/src/test
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2019-08-14 17:31:50 -0400
committerJim Hahn <jrh3@att.com>2019-08-21 13:49:54 -0400
commitdfe8fa8bc3e75c186589d21b619baa55454ef8a2 (patch)
treeca3c7dbbe53815afdacb1c97e62ee97ace3f867c /feature-lifecycle/src/test
parent59e9b9a8b56d563814ef21a23716959f772f9194 (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-lifecycle/src/test')
-rw-r--r--feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java12
-rw-r--r--feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java65
-rw-r--r--feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateRunningTest.java25
3 files changed, 56 insertions, 46 deletions
diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java
index 550379be..71245335 100644
--- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java
+++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java
@@ -21,7 +21,6 @@
package org.onap.policy.drools.lifecycle;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
-import static org.awaitility.Awaitility.await;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
@@ -59,7 +58,8 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
*/
@Before
public void startActive() throws CoderException {
- fsm = new LifecycleFsm();
+ fsm = makeFsmWithPseudoTime();
+
fsm.setStatusTimerSeconds(15);
assertTrue(fsm.start());
@@ -99,7 +99,7 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
assertEquals("A", fsm.getGroup());
assertEquals("a", fsm.getSubgroup());
assertTrue(fsm.isAlive());
- await().atMost(fsm.getStatusTimerSeconds() + 1, TimeUnit.SECONDS).until(isStatus(PdpState.ACTIVE));
+ waitUntil(fsm.getStatusTimerSeconds() + 1, TimeUnit.SECONDS, isStatus(PdpState.ACTIVE));
}
@Test
@@ -114,7 +114,7 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
assertEquals(PdpState.TERMINATED, fsm.state());
assertFalse(fsm.isAlive());
assertFalse(fsm.state.isAlive());
- await().atMost(1, TimeUnit.SECONDS).until(isStatus(PdpState.TERMINATED));
+ waitUntil(1, TimeUnit.SECONDS, isStatus(PdpState.TERMINATED));
}
@Test
@@ -143,7 +143,7 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
@Test
public void status() {
- await().atMost(fsm.getStatusTimerSeconds() + 1, TimeUnit.SECONDS).until(isStatus(PdpState.ACTIVE));
+ waitUntil(fsm.getStatusTimerSeconds() + 1, TimeUnit.SECONDS, isStatus(PdpState.ACTIVE));
int preCount = fsm.client.getSink().getRecentEvents().length;
assertTrue(fsm.status());
@@ -184,7 +184,7 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
change.setState(PdpState.PASSIVE);
fsm.source.offer(new StandardCoder().encode(change));
assertEquals(PdpState.PASSIVE, fsm.state());
- await().atMost(fsm.getStatusTimerSeconds() + 1, TimeUnit.SECONDS).until(isStatus(PdpState.PASSIVE));
+ waitUntil(fsm.getStatusTimerSeconds() + 1, TimeUnit.SECONDS, isStatus(PdpState.PASSIVE));
fsm.shutdown();
}
diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java
index 711db028..9ecabbe8 100644
--- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java
+++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java
@@ -21,8 +21,6 @@
package org.onap.policy.drools.lifecycle;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
-import static org.awaitility.Awaitility.await;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@@ -37,7 +35,6 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
-import org.awaitility.core.ConditionTimeoutException;
import org.junit.Before;
import org.junit.Test;
import org.onap.policy.common.utils.coder.CoderException;
@@ -63,7 +60,7 @@ public class LifecycleStatePassiveTest extends LifecycleStateRunningTest {
@Before
public void startPassive() {
/* start every test in passive mode */
- fsm = new LifecycleFsm();
+ fsm = makeFsmWithPseudoTime();
fsm.setStatusTimerSeconds(15L);
simpleStart();
}
@@ -78,13 +75,12 @@ public class LifecycleStatePassiveTest extends LifecycleStateRunningTest {
public void controller() {
fsm.start(controllerSupport.getController());
assertSame(controllerSupport.getController(),
- fsm.getController(new ToscaPolicyTypeIdentifier(ControllerSupport.POLICY_TYPE,
- ControllerSupport.POLICY_TYPE_VERSION)));
+ fsm.getController(new ToscaPolicyTypeIdentifier(ControllerSupport.POLICY_TYPE,
+ ControllerSupport.POLICY_TYPE_VERSION)));
fsm.stop(controllerSupport.getController());
- assertNull(fsm.getController(
- new ToscaPolicyTypeIdentifier(ControllerSupport.POLICY_TYPE,
- ControllerSupport.POLICY_TYPE_VERSION)));
+ assertNull(fsm.getController(new ToscaPolicyTypeIdentifier(ControllerSupport.POLICY_TYPE,
+ ControllerSupport.POLICY_TYPE_VERSION)));
fsm.shutdown();
}
@@ -109,8 +105,7 @@ public class LifecycleStatePassiveTest extends LifecycleStateRunningTest {
}
String[] events = fsm.client.getSink().getRecentEvents();
- PdpStatus status =
- new StandardCoder().decode(events[events.length - 1], PdpStatus.class);
+ PdpStatus status = new StandardCoder().decode(events[events.length - 1], PdpStatus.class);
return status.getMessageName() == PdpMessageType.PDP_STATUS && state == status.getState();
};
@@ -133,7 +128,7 @@ public class LifecycleStatePassiveTest extends LifecycleStateRunningTest {
}
@Test
- public void shutdown() throws CoderException {
+ public void shutdown() throws Exception {
simpleStop();
fsm.shutdown();
@@ -147,22 +142,14 @@ public class LifecycleStatePassiveTest extends LifecycleStateRunningTest {
}
private void status(PdpState state) {
- await()
- .atMost(5, TimeUnit.SECONDS)
- .until(isStatus(state, 1));
+ waitUntil(5, TimeUnit.SECONDS, isStatus(state, 1));
- await()
- .atMost(fsm.statusTimerSeconds + 2, TimeUnit.SECONDS)
- .until(isStatus(state, 2));
+ waitUntil(fsm.statusTimerSeconds + 2, TimeUnit.SECONDS, isStatus(state, 2));
- await()
- .atMost(fsm.statusTimerSeconds + 2, TimeUnit.SECONDS)
- .until(isStatus(state, 3));
+ waitUntil(fsm.statusTimerSeconds + 2, TimeUnit.SECONDS, isStatus(state, 3));
assertTrue(fsm.status());
- await()
- .atMost(200, TimeUnit.MILLISECONDS)
- .until(isStatus(state, 4));
+ waitUntil(200, TimeUnit.MILLISECONDS, isStatus(state, 4));
}
@Test
@@ -182,8 +169,8 @@ public class LifecycleStatePassiveTest extends LifecycleStateRunningTest {
assertTrue(fsm.update(update));
int qlength = fsm.client.getSink().getRecentEvents().length;
- PdpStatus lastStatus = new StandardCoder()
- .decode(fsm.client.getSink().getRecentEvents()[qlength - 1], PdpStatus.class);
+ PdpStatus lastStatus = new StandardCoder().decode(fsm.client.getSink().getRecentEvents()[qlength - 1],
+ PdpStatus.class);
assertEquals(update.getRequestId(), lastStatus.getRequestId());
assertEquals(update.getRequestId(), lastStatus.getResponse().getResponseTo());
@@ -193,8 +180,8 @@ public class LifecycleStatePassiveTest extends LifecycleStateRunningTest {
assertEquals("z", fsm.getSubgroup());
assertBasicPassive();
- String rawPolicy =
- new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-operational-restart.json")));
+ String rawPolicy = new String(
+ Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-operational-restart.json")));
ToscaPolicy toscaPolicy = new StandardCoder().decode(rawPolicy, ToscaPolicy.class);
update.setPolicies(Arrays.asList(toscaPolicy));
@@ -287,8 +274,8 @@ public class LifecycleStatePassiveTest extends LifecycleStateRunningTest {
update.setPdpGroup("A");
update.setPdpSubgroup("a");
- String rawPolicy =
- new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-operational-restart.json")));
+ String rawPolicy = new String(
+ Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-operational-restart.json")));
ToscaPolicy toscaPolicy = new StandardCoder().decode(rawPolicy, ToscaPolicy.class);
update.setPolicies(Arrays.asList(toscaPolicy));
@@ -315,9 +302,7 @@ public class LifecycleStatePassiveTest extends LifecycleStateRunningTest {
assertEquals("A", fsm.getGroup());
assertEquals("a", fsm.getSubgroup());
- await()
- .atMost(5, TimeUnit.SECONDS)
- .until(() -> controllerSupport.getController().getDrools().factCount("junits") == 1);
+ waitUntil(5, TimeUnit.SECONDS, () -> controllerSupport.getController().getDrools().factCount("junits") == 1);
assertTrue(controllerSupport.getController().getDrools().delete(ToscaPolicy.class));
assertEquals(0, controllerSupport.getController().getDrools().factCount("junits"));
@@ -331,24 +316,24 @@ public class LifecycleStatePassiveTest extends LifecycleStateRunningTest {
assertFalse(fsm.state.isAlive());
}
- private void assertExtendedTerminated() throws CoderException {
+ private void assertExtendedTerminated() throws Exception {
assertBasicTerminated();
assertTrue(fsm.statusTask.isCancelled());
assertTrue(fsm.statusTask.isDone());
+ // verify there are no outstanding tasks that might change the state
+ assertTrue(time.isEmpty());
+
+ assertFalse(fsm.client.getSink().isAlive());
+
String[] events = fsm.client.getSink().getRecentEvents();
- PdpStatus status =
- new StandardCoder().decode(events[events.length - 1], PdpStatus.class);
+ PdpStatus status = new StandardCoder().decode(events[events.length - 1], PdpStatus.class);
assertEquals("drools", status.getPdpType());
assertEquals(PdpState.TERMINATED, status.getState());
assertEquals(PdpHealthStatus.HEALTHY, status.getHealthy());
assertEquals(NetworkUtil.getHostname(), status.getName());
assertEquals(fsm.getName(), status.getName());
assertEquals(PdpMessageType.PDP_STATUS, status.getMessageName());
-
- assertThatThrownBy( () -> await()
- .atMost(2 * fsm.statusTimerSeconds, TimeUnit.SECONDS)
- .until(isStatus(PdpState.TERMINATED, events.length))).isInstanceOf(ConditionTimeoutException.class);
}
private void assertBasicPassive() {
diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateRunningTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateRunningTest.java
index 8ac47e55..b5b59767 100644
--- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateRunningTest.java
+++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateRunningTest.java
@@ -23,8 +23,13 @@ package org.onap.policy.drools.lifecycle;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
+import org.onap.policy.common.utils.time.PseudoScheduledExecutorService;
+import org.onap.policy.common.utils.time.TestTimeMulti;
import org.onap.policy.drools.persistence.SystemPersistenceConstants;
import org.onap.policy.drools.utils.logging.LoggerUtil;
@@ -32,6 +37,7 @@ public abstract class LifecycleStateRunningTest {
private static final String CONTROLLER_NAME = "lifecycle";
protected static ControllerSupport controllerSupport = new ControllerSupport(CONTROLLER_NAME);
+ protected TestTimeMulti time;
protected LifecycleFsm fsm;
/**
@@ -59,4 +65,23 @@ public abstract class LifecycleStateRunningTest {
}
SystemPersistenceConstants.getManager().setConfigurationDir(null);
}
+
+ /**
+ * Creates an FSM that uses pseudo time.
+ * @return a new FSM
+ */
+ public LifecycleFsm makeFsmWithPseudoTime() {
+ time = new TestTimeMulti();
+
+ return new LifecycleFsm() {
+ @Override
+ protected ScheduledExecutorService makeExecutor() {
+ return new PseudoScheduledExecutorService(time);
+ }
+ };
+ }
+
+ public void waitUntil(long twait, TimeUnit units, Callable<Boolean> condition) {
+ time.waitUntil(twait, units, condition);
+ }
}