aboutsummaryrefslogtreecommitdiffstats
path: root/controlloop/common/eventmanager/src/test
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2021-05-03 09:11:30 -0400
committerJim Hahn <jrh3@att.com>2021-05-03 10:11:09 -0400
commitcbdf437729ca4d010147acfb208ecd90ef65777c (patch)
tree1505d656c5c455b6a0f69d533244a86e8a0879d7 /controlloop/common/eventmanager/src/test
parent9496f31d3b2a1674f57a0395d050be0d9c6b87f0 (diff)
Add releaseLock() method to event manager
Issue-ID: POLICY-3261 Change-Id: I28a5356ebfc4a6ea1792ef35bc603054208bf73b Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'controlloop/common/eventmanager/src/test')
-rw-r--r--controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManagerTest.java92
1 files changed, 87 insertions, 5 deletions
diff --git a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManagerTest.java b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManagerTest.java
index 596400f94..b930f57f8 100644
--- a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManagerTest.java
+++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManagerTest.java
@@ -60,6 +60,7 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
@RunWith(MockitoJUnitRunner.class)
public class ControlLoopEventManagerTest {
private static final UUID REQ_ID = UUID.randomUUID();
+ private static final String EXPECTED_EXCEPTION = "expected exception";
private static final String CL_NAME = "my-closed-loop-name";
private static final String POLICY_NAME = "my-policy-name";
private static final String POLICY_SCOPE = "my-scope";
@@ -184,15 +185,96 @@ public class ControlLoopEventManagerTest {
// indicate that the first lock failed
locks.get(0).notifyUnavailable();
- verifyLock(OperationResult.FAILURE);
+ verifyLock(OperationResult.FAILURE, ActorConstants.LOCK_OPERATION);
assertTrue(mgr.getOutcomes().isEmpty());
}
- private void verifyLock(OperationResult result) {
+ @Test
+ public void testReleaseLock() {
+ mgr.requestLock(LOCK1);
+ mgr.requestLock(LOCK2);
+
+ // release one lock
+ final CompletableFuture<OperationOutcome> future = mgr.releaseLock(LOCK1);
+
+ // asynchronous, thus should not have executed yet
+ assertThat(future.isDone()).isFalse();
+
+ // asynchronous, thus everything should still be locked
+ for (LockImpl lock : locks) {
+ assertThat(lock.isUnavailable()).isFalse();
+ }
+
+ runExecutor();
+
+ verifyLock(OperationResult.SUCCESS, ActorConstants.UNLOCK_OPERATION);
+ assertThat(mgr.getOutcomes()).isEmpty();
+
+ // first lock should have been released, thus no longer available to the manager
+ assertThat(locks.get(0).isUnavailable()).isTrue();
+
+ // second should still be locked
+ assertThat(locks.get(1).isUnavailable()).isFalse();
+ }
+
+ /**
+ * Tests releaseLock() when there is no lock.
+ */
+ @Test
+ public void testReleaseLockNotLocked() {
+ final CompletableFuture<OperationOutcome> future = mgr.releaseLock(LOCK1);
+
+ // lock didn't exist, so the request should already be complete
+ assertThat(future.isDone()).isTrue();
+
+ verifyLock(OperationResult.SUCCESS, ActorConstants.UNLOCK_OPERATION);
+ assertThat(mgr.getOutcomes()).isEmpty();
+ }
+
+ /**
+ * Tests releaseLock() when lock.free() throws an exception.
+ */
+ @Test
+ public void testReleaseLockException() throws ControlLoopException {
+ mgr = new MyManager(params, REQ_ID) {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ protected void makeLock(String targetEntity, String requestId, int holdSec, LockCallback callback) {
+
+ LockImpl lock = new LockImpl(LockState.ACTIVE, targetEntity, requestId, holdSec, callback) {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ public boolean free() {
+ throw new RuntimeException(EXPECTED_EXCEPTION);
+ }
+ };
+
+ locks.add(lock);
+ callback.lockAvailable(lock);
+ }
+ };
+
+ mgr.requestLock(LOCK1);
+
+ // release the lock
+ final CompletableFuture<OperationOutcome> future = mgr.releaseLock(LOCK1);
+
+ // asynchronous, thus should not have executed yet
+ assertThat(future.isDone()).isFalse();
+
+ runExecutor();
+
+ verifyLock(OperationResult.FAILURE_EXCEPTION, ActorConstants.UNLOCK_OPERATION);
+ assertThat(mgr.getOutcomes()).isEmpty();
+ }
+
+ private void verifyLock(OperationResult result, String lockOperation) {
OperationOutcome outcome = mgr.getOutcomes().poll();
assertNotNull(outcome);
assertEquals(ActorConstants.LOCK_ACTOR, outcome.getActor());
- assertEquals(ActorConstants.LOCK_OPERATION, outcome.getOperation());
+ assertEquals(lockOperation, outcome.getOperation());
assertNotNull(outcome.getEnd());
assertTrue(outcome.isFinalOutcome());
assertEquals(result, outcome.getResult());
@@ -249,6 +331,7 @@ public class ControlLoopEventManagerTest {
public void testGetDataManagerDisabled() throws ControlLoopException {
mgr = new MyManager(params, REQ_ID) {
private static final long serialVersionUID = 1L;
+
@Override
protected String getEnvironmentProperty(String propName) {
return ("guard.disabled".equals(propName) ? "true" : null);
@@ -285,8 +368,7 @@ public class ControlLoopEventManagerTest {
private static ExecutorService executor;
private static List<LockImpl> locks;
- public MyManager(ControlLoopParams params, UUID requestId)
- throws ControlLoopException {
+ public MyManager(ControlLoopParams params, UUID requestId) throws ControlLoopException {
super(params, requestId);
}