aboutsummaryrefslogtreecommitdiffstats
path: root/policy-core/src/test/java
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2018-06-25 17:23:02 -0400
committerJim Hahn <jrh3@att.com>2018-06-25 17:40:04 -0400
commitd8a6909ef0bb3433265295107b1450122eac919a (patch)
tree8a625e85a2712c96fb10d53dae2cb33ac9c6ce65 /policy-core/src/test/java
parentb5a47dcb16af9e90e46d08d0b5faedb3302bca8b (diff)
Added expiration timer to default locking strategy
Fixed comment (missing "}"). Changed default age from 15 hours to 15 minutes. Only increment test time by 1 when testing hashCode. Change-Id: I70ef6cfd34e31ded45b3975f33e5b0ba22afa627 Issue-ID: POLICY-912 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'policy-core/src/test/java')
-rw-r--r--policy-core/src/test/java/org/onap/policy/drools/core/lock/LockRequestFutureTest.java2
-rw-r--r--policy-core/src/test/java/org/onap/policy/drools/core/lock/PolicyResourceLockManagerTest.java6
-rw-r--r--policy-core/src/test/java/org/onap/policy/drools/core/lock/SimpleLockManagerTest.java164
3 files changed, 167 insertions, 5 deletions
diff --git a/policy-core/src/test/java/org/onap/policy/drools/core/lock/LockRequestFutureTest.java b/policy-core/src/test/java/org/onap/policy/drools/core/lock/LockRequestFutureTest.java
index 2dd90f00..7ac9e31c 100644
--- a/policy-core/src/test/java/org/onap/policy/drools/core/lock/LockRequestFutureTest.java
+++ b/policy-core/src/test/java/org/onap/policy/drools/core/lock/LockRequestFutureTest.java
@@ -23,7 +23,7 @@ package org.onap.policy.drools.core.lock;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
-import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
diff --git a/policy-core/src/test/java/org/onap/policy/drools/core/lock/PolicyResourceLockManagerTest.java b/policy-core/src/test/java/org/onap/policy/drools/core/lock/PolicyResourceLockManagerTest.java
index fe883acf..26732e42 100644
--- a/policy-core/src/test/java/org/onap/policy/drools/core/lock/PolicyResourceLockManagerTest.java
+++ b/policy-core/src/test/java/org/onap/policy/drools/core/lock/PolicyResourceLockManagerTest.java
@@ -24,9 +24,9 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyBoolean;
-import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyBoolean;
+import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
diff --git a/policy-core/src/test/java/org/onap/policy/drools/core/lock/SimpleLockManagerTest.java b/policy-core/src/test/java/org/onap/policy/drools/core/lock/SimpleLockManagerTest.java
index 833e1c7d..9aeba53c 100644
--- a/policy-core/src/test/java/org/onap/policy/drools/core/lock/SimpleLockManagerTest.java
+++ b/policy-core/src/test/java/org/onap/policy/drools/core/lock/SimpleLockManagerTest.java
@@ -22,6 +22,7 @@ package org.onap.policy.drools.core.lock;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.onap.policy.drools.core.lock.TestUtils.expectException;
import java.util.LinkedList;
@@ -31,8 +32,14 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
+import org.junit.AfterClass;
import org.junit.Before;
+import org.junit.BeforeClass;
import org.junit.Test;
+import org.onap.policy.common.utils.time.CurrentTime;
+import org.onap.policy.common.utils.time.TestTime;
+import org.onap.policy.drools.core.lock.SimpleLockManager.Data;
+import org.powermock.reflect.Whitebox;
public class SimpleLockManagerTest {
@@ -42,18 +49,45 @@ public class SimpleLockManagerTest {
private static final String RESOURCE_A = "resource.a";
private static final String RESOURCE_B = "resource.b";
private static final String RESOURCE_C = "resource.c";
+ private static final String RESOURCE_D = "resource.d";
private static final String OWNER1 = "owner.one";
private static final String OWNER2 = "owner.two";
private static final String OWNER3 = "owner.three";
-
+
+ /**
+ * Name of the "current time" field within the {@link SimpleLockManager} class.
+ */
+ private static final String TIME_FIELD = "currentTime";
+
+ private static CurrentTime savedTime;
+
+ private TestTime testTime;
private Future<Boolean> fut;
private SimpleLockManager mgr;
+
+ @BeforeClass
+ public static void setUpBeforeClass() {
+ savedTime = Whitebox.getInternalState(SimpleLockManager.class, TIME_FIELD);
+ }
+
+ @AfterClass
+ public static void tearDownAfterClass() {
+ Whitebox.setInternalState(SimpleLockManager.class, TIME_FIELD, savedTime);
+ }
@Before
public void setUp() {
+ testTime = new TestTime();
+ Whitebox.setInternalState(SimpleLockManager.class, TIME_FIELD, testTime);
+
mgr = new SimpleLockManager();
}
+
+ @Test
+ public void testCurrentTime() {
+ assertNotNull(savedTime);
+ }
@Test
public void testLock() throws Exception {
@@ -131,6 +165,12 @@ public class SimpleLockManagerTest {
}
@Test
+ public void testUnlock_DiffOwner() {
+ mgr.lock(RESOURCE_A, OWNER1, null);
+ assertFalse(mgr.unlock(RESOURCE_A, OWNER2));
+ }
+
+ @Test
public void testIsLocked() {
assertFalse(mgr.isLocked(RESOURCE_A));
@@ -203,6 +243,128 @@ public class SimpleLockManagerTest {
// different owner
assertFalse(mgr.isLockedBy(RESOURCE_A, OWNER2));
}
+
+ @Test
+ public void testCleanUpLocks() throws Exception {
+ // note: this assumes that MAX_AGE_MS is divisible by 4
+ mgr.lock(RESOURCE_A, OWNER1, null);
+ assertTrue(mgr.isLocked(RESOURCE_A));
+
+ testTime.sleep(10);
+ mgr.lock(RESOURCE_B, OWNER1, null);
+ assertTrue(mgr.isLocked(RESOURCE_A));
+ assertTrue(mgr.isLocked(RESOURCE_B));
+
+ testTime.sleep(SimpleLockManager.MAX_AGE_MS/4);
+ mgr.lock(RESOURCE_C, OWNER1, null);
+ assertTrue(mgr.isLocked(RESOURCE_A));
+ assertTrue(mgr.isLocked(RESOURCE_B));
+ assertTrue(mgr.isLocked(RESOURCE_C));
+
+ testTime.sleep(SimpleLockManager.MAX_AGE_MS/4);
+ mgr.lock(RESOURCE_D, OWNER1, null);
+ assertTrue(mgr.isLocked(RESOURCE_A));
+ assertTrue(mgr.isLocked(RESOURCE_B));
+ assertTrue(mgr.isLocked(RESOURCE_C));
+ assertTrue(mgr.isLocked(RESOURCE_D));
+
+ // sleep remainder of max age - first two should expire
+ testTime.sleep(SimpleLockManager.MAX_AGE_MS/2);
+ assertFalse(mgr.isLocked(RESOURCE_A));
+ assertFalse(mgr.isLocked(RESOURCE_B));
+ assertTrue(mgr.isLocked(RESOURCE_C));
+ assertTrue(mgr.isLocked(RESOURCE_D));
+
+ // another quarter - next one should expire
+ testTime.sleep(SimpleLockManager.MAX_AGE_MS/4);
+ assertFalse(mgr.isLocked(RESOURCE_C));
+ assertTrue(mgr.isLocked(RESOURCE_D));
+
+ // another quarter - last one should expire
+ testTime.sleep(SimpleLockManager.MAX_AGE_MS/4);
+ assertFalse(mgr.isLocked(RESOURCE_D));
+ }
+
+ @Test
+ public void testDataGetXxx() {
+ long ttime = System.currentTimeMillis() + 5;
+ Data data = new Data(OWNER1, RESOURCE_A, ttime);
+
+ assertEquals(OWNER1, data.getOwner());
+ assertEquals(RESOURCE_A, data.getResource());
+ assertEquals(ttime, data.getExpirationMs());
+ }
+
+ @Test
+ public void testDataCompareTo() {
+ long ttime = System.currentTimeMillis() + 50;
+ Data data = new Data(OWNER1, RESOURCE_A, ttime);
+ Data dataSame = new Data(OWNER1, RESOURCE_A, ttime);
+ Data dataDiffExpire = new Data(OWNER1, RESOURCE_A, ttime+1);
+ Data dataDiffOwner = new Data(OWNER2, RESOURCE_A, ttime);
+ Data dataDiffResource = new Data(OWNER1, RESOURCE_B, ttime);
+
+ assertEquals(0, data.compareTo(data));
+ assertEquals(0, data.compareTo(dataSame));
+
+ assertTrue(data.compareTo(dataDiffExpire) < 0);
+ assertTrue(dataDiffExpire.compareTo(data) > 0);
+
+ assertTrue(data.compareTo(dataDiffOwner) < 0);
+ assertTrue(dataDiffOwner.compareTo(data) > 0);
+
+ assertTrue(data.compareTo(dataDiffResource) < 0);
+ assertTrue(dataDiffResource.compareTo(data) > 0);
+ }
+
+ @Test
+ public void testDataHashCode() {
+ long ttime = System.currentTimeMillis() + 1;
+ Data data = new Data(OWNER1, RESOURCE_A, ttime);
+ Data dataSame = new Data(OWNER1, RESOURCE_A, ttime);
+ Data dataDiffExpire = new Data(OWNER1, RESOURCE_A, ttime+1);
+ Data dataDiffOwner = new Data(OWNER2, RESOURCE_A, ttime);
+ Data dataDiffResource = new Data(OWNER1, RESOURCE_B, ttime);
+ Data dataNullOwner = new Data(null, RESOURCE_A, ttime);
+ Data dataNullResource = new Data(OWNER1, null, ttime);
+
+ int hc1 = data.hashCode();
+ assertEquals(hc1, dataSame.hashCode());
+
+ assertTrue(hc1 != dataDiffExpire.hashCode());
+ assertTrue(hc1 != dataDiffOwner.hashCode());
+ assertTrue(hc1 != dataDiffResource.hashCode());
+ assertTrue(hc1 != dataNullOwner.hashCode());
+ assertTrue(hc1 != dataNullResource.hashCode());
+ }
+
+ @Test
+ public void testDataEquals() {
+ long ttime = System.currentTimeMillis() + 50;
+ Data data = new Data(OWNER1, RESOURCE_A, ttime);
+ Data dataSame = new Data(OWNER1, RESOURCE_A, ttime);
+ Data dataDiffExpire = new Data(OWNER1, RESOURCE_A, ttime+1);
+ Data dataDiffOwner = new Data(OWNER2, RESOURCE_A, ttime);
+ Data dataDiffResource = new Data(OWNER1, RESOURCE_B, ttime);
+ Data dataNullOwner = new Data(null, RESOURCE_A, ttime);
+ Data dataNullResource = new Data(OWNER1, null, ttime);
+
+ assertTrue(data.equals(data));
+ assertTrue(data.equals(dataSame));
+
+ assertFalse(data.equals(dataDiffExpire));
+ assertFalse(data.equals(dataDiffOwner));
+ assertFalse(data.equals(dataDiffResource));
+
+ assertFalse(data.equals(null));
+ assertFalse(data.equals("string"));
+
+ assertFalse(dataNullOwner.equals(data));
+ assertFalse(dataNullResource.equals(data));
+
+ assertTrue(dataNullOwner.equals(new Data(null, RESOURCE_A, ttime)));
+ assertTrue(dataNullResource.equals(new Data(OWNER1, null, ttime)));
+ }
@Test
public void testMultiThreaded() throws InterruptedException {