aboutsummaryrefslogtreecommitdiffstats
path: root/feature-distributed-locking/src/test/java/org
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2019-10-30 16:28:55 -0400
committerJim Hahn <jrh3@att.com>2019-10-31 16:24:04 -0400
commitd437070115d2e7c151deb2dea680007e20651413 (patch)
treec3a1a7c02919f69bc747b7b465c1c5cf5bffe1c6 /feature-distributed-locking/src/test/java/org
parent01abf11819c0cd797bc3170fbcce04cffb020b27 (diff)
Refactor duplicate code from lock managers
Change-Id: I8910a1a4267d824f064b52c6ad08945590bd9617 Issue-ID: POLICY-2203 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'feature-distributed-locking/src/test/java/org')
-rw-r--r--feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/DistributedLockManagerTest.java104
1 files changed, 39 insertions, 65 deletions
diff --git a/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/DistributedLockManagerTest.java b/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/DistributedLockManagerTest.java
index 0ba92c51..c996d8d9 100644
--- a/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/DistributedLockManagerTest.java
+++ b/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/DistributedLockManagerTest.java
@@ -30,6 +30,7 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doThrow;
@@ -55,6 +56,7 @@ import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -115,16 +117,19 @@ public class DistributedLockManagerTest {
private static ScheduledExecutorService realExec;
@Mock
+ private PolicyEngine engine;
+
+ @Mock
private ScheduledExecutorService exsvc;
@Mock
- private LockCallback callback;
+ private ScheduledFuture<?> checker;
@Mock
- private BasicDataSource datasrc;
+ private LockCallback callback;
@Mock
- private PolicyEngine engine;
+ private BasicDataSource datasrc;
private DistributedLock lock;
@@ -153,7 +158,6 @@ public class DistributedLockManagerTest {
saveExec = Whitebox.getInternalState(PolicyEngineConstants.getManager(), POLICY_ENGINE_EXECUTOR_FIELD);
realExec = Executors.newScheduledThreadPool(3);
- Whitebox.setInternalState(PolicyEngineConstants.getManager(), POLICY_ENGINE_EXECUTOR_FIELD, realExec);
}
/**
@@ -181,8 +185,6 @@ public class DistributedLockManagerTest {
cleanDb();
- when(engine.getExecutorService()).thenReturn(exsvc);
-
feature = new MyLockingFeature(true);
}
@@ -214,52 +216,12 @@ public class DistributedLockManagerTest {
.anyMatch(obj -> obj instanceof DistributedLockManager));
}
- /**
- * Tests constructor() when properties are invalid.
- */
- @Test
- public void testDistributedLockManagerInvalidProperties() {
- // use properties containing an invalid value
- Properties props = new Properties();
- props.setProperty(DistributedLockProperties.EXPIRE_CHECK_SEC, "abc");
-
- feature = new MyLockingFeature(false) {
- @Override
- protected Properties getProperties(String fileName) {
- return props;
- }
- };
-
- assertThatThrownBy(() -> feature.afterStart(engine)).isInstanceOf(DistributedLockManagerException.class);
- }
-
@Test
public void testGetSequenceNumber() {
assertEquals(1000, feature.getSequenceNumber());
}
@Test
- public void testStartableApi() {
- assertTrue(feature.isAlive());
- assertTrue(feature.start());
- assertTrue(feature.stop());
- feature.shutdown();
-
- // above should have had no effect
- assertTrue(feature.isAlive());
-
- feature.afterStop(engine);
- assertFalse(feature.isAlive());
- }
-
- @Test
- public void testLockApi() {
- assertFalse(feature.isLocked());
- assertTrue(feature.lock());
- assertTrue(feature.unlock());
- }
-
- @Test
public void testBeforeCreateLockManager() {
assertSame(feature, feature.beforeCreateLockManager(engine, new Properties()));
}
@@ -298,8 +260,7 @@ public class DistributedLockManagerTest {
feature = new MyLockingFeature(false);
- when(exsvc.schedule(any(Runnable.class), anyLong(), any()))
- .thenThrow(new IllegalArgumentException(EXPECTED_EXCEPTION));
+ doThrow(new IllegalArgumentException(EXPECTED_EXCEPTION)).when(exsvc).execute(any());
assertThatThrownBy(() -> feature.afterStart(engine)).isInstanceOf(DistributedLockManagerException.class);
}
@@ -350,6 +311,7 @@ public class DistributedLockManagerTest {
@Test
public void testAfterStop() {
shutdownFeature();
+ verify(checker).cancel(anyBoolean());
feature = new DistributedLockManager();
@@ -424,7 +386,7 @@ public class DistributedLockManagerTest {
}
/**
- * Tests lock() when the feature is not the latest instance.
+ * Tests createLock() when the feature is not the latest instance.
*/
@Test
public void testCreateLockNotLatestInstance() {
@@ -522,6 +484,27 @@ public class DistributedLockManagerTest {
runChecker(0, 0, RETRY_SEC);
}
+ /**
+ * Tests checkExpired(), when getConnection() throws an exception and the feature is
+ * no longer alive.
+ */
+ @Test
+ public void testCheckExpiredSqlExFeatureStopped() {
+ // use a data source that throws an exception when getConnection() is called
+ feature = new InvalidDbLockingFeature(TRANSIENT) {
+ @Override
+ protected SQLException makeEx() {
+ this.stop();
+ return super.makeEx();
+ }
+ };
+
+ runChecker(0, 0, EXPIRE_SEC);
+
+ // it should NOT have scheduled another check
+ verify(exsvc, times(1)).schedule(any(Runnable.class), anyLong(), any());
+ }
+
@Test
public void testExpireLocks() throws SQLException {
AtomicReference<DistributedLock> freeLock = new AtomicReference<>(null);
@@ -637,20 +620,6 @@ public class DistributedLockManagerTest {
verify(callback).lockAvailable(lock);
}
- /**
- * Tests grant() when the lock is already unavailable.
- */
- @Test
- public void testDistributedLockGrantUnavailable() {
- DistributedLock lock = getLock(RESOURCE, OWNER_KEY, HOLD_SEC, callback, false);
- lock.setState(LockState.UNAVAILABLE);
- lock.grant();
-
- assertTrue(lock.isUnavailable());
- verify(callback, never()).lockAvailable(any());
- verify(callback, never()).lockUnavailable(any());
- }
-
@Test
public void testDistributedLockDeny() {
// get a lock
@@ -1464,6 +1433,8 @@ public class DistributedLockManagerTest {
*/
@Test
public void testMultiThreaded() throws InterruptedException {
+ Whitebox.setInternalState(PolicyEngineConstants.getManager(), POLICY_ENGINE_EXECUTOR_FIELD, realExec);
+
feature = new DistributedLockManager();
feature.beforeCreateLockManager(PolicyEngineConstants.getManager(), new Properties());
feature.afterStart(PolicyEngineConstants.getManager());
@@ -1661,10 +1632,12 @@ public class DistributedLockManagerTest {
shutdownFeature();
exsvc = mock(ScheduledExecutorService.class);
- when(engine.getExecutorService()).thenReturn(exsvc);
+ when(exsvc.schedule(any(Runnable.class), anyLong(), any())).thenAnswer(ans -> checker);
+ Whitebox.setInternalState(PolicyEngineConstants.getManager(), POLICY_ENGINE_EXECUTOR_FIELD, exsvc);
if (init) {
beforeCreateLockManager(engine, new Properties());
+ start();
afterStart(engine);
}
}
@@ -1685,6 +1658,7 @@ public class DistributedLockManagerTest {
this.isTransient = isTransient;
this.beforeCreateLockManager(engine, new Properties());
+ this.start();
this.afterStart(engine);
}
@@ -1704,7 +1678,7 @@ public class DistributedLockManagerTest {
return datasrc;
}
- private SQLException makeEx() {
+ protected SQLException makeEx() {
if (isTransient) {
return new SQLException(new SQLTransientException(EXPECTED_EXCEPTION));