diff options
author | Jim Hahn <jrh3@att.com> | 2018-06-27 11:04:11 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2018-06-29 11:07:51 -0400 |
commit | cecd4ac43f199ad9f020d38fb7d1651b296d1703 (patch) | |
tree | 827cbcc055db03dd3e21a91fdfff9926f77b635c /controlloop/common/guard/src/test | |
parent | 9e865fe364ed6aa3445d9b1f1844378c52feab42 (diff) |
Add time limit to local guard locking facility
Modified the local policy guard locking facility to add
a time parameter.
Modified the control loop event manager to extend the lock
when lockCurrentOperation() is re-invoked.
Modified the rules to retract the lock if the lock request
was denied.
Reorder assertions in junit test.
Change-Id: Ic9b77acbb4881a5a516f30eb56664bad1a5c4d7e
Issue-ID: POLICY-872
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'controlloop/common/guard/src/test')
-rw-r--r-- | controlloop/common/guard/src/test/java/org/onap/policy/guard/PolicyGuardTest.java | 79 |
1 files changed, 35 insertions, 44 deletions
diff --git a/controlloop/common/guard/src/test/java/org/onap/policy/guard/PolicyGuardTest.java b/controlloop/common/guard/src/test/java/org/onap/policy/guard/PolicyGuardTest.java index b073fe5a9..9cd34b70a 100644 --- a/controlloop/common/guard/src/test/java/org/onap/policy/guard/PolicyGuardTest.java +++ b/controlloop/common/guard/src/test/java/org/onap/policy/guard/PolicyGuardTest.java @@ -25,13 +25,12 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doThrow; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.util.UUID; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Future; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; @@ -46,6 +45,7 @@ import org.onap.policy.guard.impl.VNFTargetLock; public class PolicyGuardTest { private static final String INSTANCENAME = "targetInstance"; + private static final int LOCK_SEC = 10; private static Factory saveFactory; @@ -131,7 +131,7 @@ public class PolicyGuardTest { // Test isLocked before and after lock added assertFalse(PolicyGuard.isLocked(type, INSTANCENAME, uuid)); - LockResult<GuardResult, TargetLock> result = PolicyGuard.lockTarget(type, INSTANCENAME, uuid, dlcb); + LockResult<GuardResult, TargetLock> result = PolicyGuard.lockTarget(type, INSTANCENAME, uuid, dlcb, LOCK_SEC); assertTrue(PolicyGuard.isLocked(type, INSTANCENAME, uuid)); assertEquals(GuardResult.LOCK_ACQUIRED, result.getA()); @@ -155,7 +155,7 @@ public class PolicyGuardTest { // Test isLocked before and after lock added assertFalse(PolicyGuard.isLocked(type, INSTANCENAME, uuid)); - LockResult<GuardResult, TargetLock> result = PolicyGuard.lockTarget(type, INSTANCENAME, uuid, dlcb); + LockResult<GuardResult, TargetLock> result = PolicyGuard.lockTarget(type, INSTANCENAME, uuid, dlcb, LOCK_SEC); assertTrue(PolicyGuard.isLocked(type, INSTANCENAME, uuid)); assertEquals(GuardResult.LOCK_ACQUIRED, result.getA()); @@ -180,7 +180,7 @@ public class PolicyGuardTest { // Test isLocked before and after lock added assertFalse(PolicyGuard.isLocked(type, INSTANCENAME, uuid)); - LockResult<GuardResult, TargetLock> result = PolicyGuard.lockTarget(type, INSTANCENAME, uuid, dlcb); + LockResult<GuardResult, TargetLock> result = PolicyGuard.lockTarget(type, INSTANCENAME, uuid, dlcb, LOCK_SEC); assertTrue(PolicyGuard.isLocked(type, INSTANCENAME, uuid)); assertEquals(GuardResult.LOCK_ACQUIRED, result.getA()); @@ -204,7 +204,7 @@ public class PolicyGuardTest { // Test isLocked before and after lock added assertFalse(PolicyGuard.isLocked(type, INSTANCENAME, uuid)); - LockResult<GuardResult, TargetLock> result = PolicyGuard.lockTarget(type, INSTANCENAME, uuid, dlcb); + LockResult<GuardResult, TargetLock> result = PolicyGuard.lockTarget(type, INSTANCENAME, uuid, dlcb, LOCK_SEC); assertFalse(PolicyGuard.isLocked(type, INSTANCENAME, uuid)); assertEquals(GuardResult.LOCK_EXCEPTION, result.getA()); @@ -221,18 +221,12 @@ public class PolicyGuardTest { // Test isLocked before and after lock added assertFalse(PolicyGuard.isLocked(type, INSTANCENAME, uuid)); - LockResult<GuardResult, TargetLock> result = PolicyGuard.lockTarget(type, INSTANCENAME, uuid, dlcb); + LockResult<GuardResult, TargetLock> result = PolicyGuard.lockTarget(type, INSTANCENAME, uuid, dlcb, LOCK_SEC); assertTrue(PolicyGuard.isLocked(type, INSTANCENAME, uuid)); assertEquals(GuardResult.LOCK_ACQUIRED, result.getA()); assertEquals(VMTargetLock.class, result.getB().getClass()); - result = PolicyGuard.lockTarget(type, INSTANCENAME, uuid, dlcb); - assertTrue(PolicyGuard.isLocked(type, INSTANCENAME, uuid)); - - assertEquals(GuardResult.LOCK_DENIED, result.getA()); - assertNull(result.getB()); - // Test isLocked after lock removed PolicyGuard.unlockTarget(new DummyTargetLock(type, uuid)); assertFalse(PolicyGuard.isLocked(type, INSTANCENAME, uuid)); @@ -248,13 +242,16 @@ public class PolicyGuardTest { // Test isLocked before and after lock added assertFalse(PolicyGuard.isLocked(type, INSTANCENAME, uuid)); - LockResult<GuardResult, TargetLock> result = PolicyGuard.lockTarget(type, INSTANCENAME, uuid, dlcb); + LockResult<GuardResult, TargetLock> result = PolicyGuard.lockTarget(type, INSTANCENAME, uuid, dlcb, LOCK_SEC); assertTrue(PolicyGuard.isLocked(type, INSTANCENAME, uuid)); assertEquals(GuardResult.LOCK_ACQUIRED, result.getA()); assertEquals(VMTargetLock.class, result.getB().getClass()); - result = PolicyGuard.lockTarget(type, INSTANCENAME, uuid, dlcb); + UUID uuid2 = UUID.randomUUID(); + result = PolicyGuard.lockTarget(type, INSTANCENAME, uuid2, dlcb, LOCK_SEC); + assertFalse(PolicyGuard.isLocked(type, INSTANCENAME, uuid2)); + assertTrue(PolicyGuard.isLocked(type, INSTANCENAME, uuid)); assertEquals(GuardResult.LOCK_DENIED, result.getA()); @@ -283,47 +280,41 @@ public class PolicyGuardTest { public void testManagerLockTarget() throws Exception { TargetType type = TargetType.VM; - @SuppressWarnings("unchecked") - Future<Boolean> fut = mock(Future.class); - mgr = mock(PolicyResourceLockManager.class); - when(mgr.lock(any(), any(), any())).thenReturn(fut); LockResult<GuardResult, TargetLock> result; // acquired - when(fut.get()).thenReturn(true); - result = PolicyGuard.lockTarget(type, INSTANCENAME, uuid, dlcb); + when(mgr.lock(anyString(), anyString(), anyInt())).thenReturn(true); + result = PolicyGuard.lockTarget(type, INSTANCENAME, uuid, dlcb, LOCK_SEC); + verify(mgr).lock(INSTANCENAME, type.toString()+":"+uuid.toString(), LOCK_SEC); assertEquals(GuardResult.LOCK_ACQUIRED, result.getA()); assertEquals(VMTargetLock.class, result.getB().getClass()); // denied - when(fut.get()).thenReturn(false); - result = PolicyGuard.lockTarget(type, INSTANCENAME, uuid, dlcb); + when(mgr.lock(anyString(), anyString(), anyInt())).thenReturn(false); + result = PolicyGuard.lockTarget(type, INSTANCENAME, uuid, dlcb, LOCK_SEC+2); + verify(mgr).lock(INSTANCENAME, type.toString()+":"+uuid.toString(), LOCK_SEC+2); assertEquals(GuardResult.LOCK_DENIED, result.getA()); assertNull(result.getB()); + } - // illegal state exception - doThrow(new IllegalStateException("state expected")).when(fut).get(); - result = PolicyGuard.lockTarget(type, INSTANCENAME, uuid, dlcb); - assertEquals(GuardResult.LOCK_DENIED, result.getA()); - assertNull(result.getB()); - assertFalse(Thread.currentThread().isInterrupted()); + @Test + public void testManagerLockTargetTargetLockInt() throws Exception { + TargetType type = TargetType.VM; + DummyTargetLock lock = new DummyTargetLock(type, uuid); + + mgr = mock(PolicyResourceLockManager.class); - // interrupted exception - doThrow(new InterruptedException("interrupt expected")).when(fut).get(); - result = PolicyGuard.lockTarget(type, INSTANCENAME, uuid, dlcb); - assertEquals(GuardResult.LOCK_EXCEPTION, result.getA()); - assertNull(result.getB()); - // verify interrupted & reset interrupt status - assertTrue(Thread.interrupted()); + // acquired + when(mgr.lock(anyString(), anyString(), anyInt())).thenReturn(true); + assertEquals(GuardResult.LOCK_ACQUIRED, PolicyGuard.lockTarget(lock, LOCK_SEC)); + verify(mgr).lock(INSTANCENAME, type.toString()+":"+uuid.toString(), LOCK_SEC); - // exec exception - doThrow(new ExecutionException(new IllegalArgumentException("exec expected"))).when(fut).get(); - result = PolicyGuard.lockTarget(type, INSTANCENAME, uuid, dlcb); - assertEquals(GuardResult.LOCK_EXCEPTION, result.getA()); - assertNull(result.getB()); - assertFalse(Thread.currentThread().isInterrupted()); + // denied + when(mgr.lock(anyString(), anyString(), anyInt())).thenReturn(false); + assertEquals(GuardResult.LOCK_DENIED, PolicyGuard.lockTarget(lock, LOCK_SEC+1)); + verify(mgr).lock(INSTANCENAME, type.toString()+":"+uuid.toString(), LOCK_SEC+1); } @Test(expected = IllegalArgumentException.class) |