summaryrefslogtreecommitdiffstats
path: root/zkfacade/src/test/java/com/yahoo
diff options
context:
space:
mode:
authorHåkon Hallingstad <hakon@verizonmedia.com>2020-09-25 15:40:48 +0200
committerHåkon Hallingstad <hakon@verizonmedia.com>2020-09-25 15:40:48 +0200
commitce446b7bc567d541b061d72ec0c5d2aa6fe7392e (patch)
treec658d6bc0191dce075f5bd0697943de68f2a4441 /zkfacade/src/test/java/com/yahoo
parentafbbe9c191ebdaf9ab8d84c727d53825f69fe64f (diff)
Adds method name to stack trace and adds timeout count and test
Diffstat (limited to 'zkfacade/src/test/java/com/yahoo')
-rw-r--r--zkfacade/src/test/java/com/yahoo/vespa/curator/stats/LockTest.java113
1 files changed, 113 insertions, 0 deletions
diff --git a/zkfacade/src/test/java/com/yahoo/vespa/curator/stats/LockTest.java b/zkfacade/src/test/java/com/yahoo/vespa/curator/stats/LockTest.java
new file mode 100644
index 00000000000..23b603eca5c
--- /dev/null
+++ b/zkfacade/src/test/java/com/yahoo/vespa/curator/stats/LockTest.java
@@ -0,0 +1,113 @@
+package com.yahoo.vespa.curator.stats;// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+import com.yahoo.vespa.curator.Lock;
+import org.apache.curator.framework.recipes.locks.InterProcessLock;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.time.Duration;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class LockTest {
+ private final InterProcessLock mutex = mock(InterProcessLock.class);
+ private final String lockPath = "/lock/path";
+ private final Duration acquireTimeout = Duration.ofSeconds(10);
+ private final Lock lock = new Lock(lockPath, mutex);
+
+ @Before
+ public void setUp() {
+ ThreadLockInfo.clearStaticDataForTesting();
+ }
+
+ @Test
+ public void acquireThrows() throws Exception {
+ Exception exception = new Exception("example curator exception");
+ when(mutex.acquire(anyLong(), any())).thenThrow(exception);
+
+ try {
+ lock.acquire(acquireTimeout);
+ fail();
+ } catch (Exception e) {
+ assertSame(e.getCause(), exception);
+ }
+
+ var expectedCounters = new LockCounters();
+ expectedCounters.invokeAcquireCount.set(1);
+ expectedCounters.acquireFailedCount.set(1);
+ assertEquals(Map.of(lockPath, expectedCounters), ThreadLockInfo.getLockCountersByPath());
+
+ List<LockInfo> slowLockInfos = ThreadLockInfo.getSlowLockInfos();
+ assertEquals(1, slowLockInfos.size());
+ LockInfo slowLockInfo = slowLockInfos.get(0);
+ assertEquals(acquireTimeout, slowLockInfo.getAcquireTimeout());
+ Optional<String> stackTrace = slowLockInfo.getStackTrace();
+ assertTrue(stackTrace.isPresent());
+ assertTrue("bad stacktrace: " + stackTrace.get(), stackTrace.get().contains(".Lock.acquire(Lock.java"));
+ assertEquals(LockInfo.LockState.ACQUIRE_FAILED, slowLockInfo.getLockState());
+ assertTrue(slowLockInfo.getTimeTerminalStateWasReached().isPresent());
+
+ List<ThreadLockInfo> threadLockInfos = ThreadLockInfo.getThreadLockInfos();
+ assertEquals(1, threadLockInfos.size());
+ ThreadLockInfo threadLockInfo = threadLockInfos.get(0);
+ assertEquals(0, threadLockInfo.getLockInfos().size());
+ }
+
+ @Test
+ public void acquireTimesOut() throws Exception {
+ when(mutex.acquire(anyLong(), any())).thenReturn(false);
+
+ try {
+ lock.acquire(acquireTimeout);
+ fail();
+ } catch (Exception e) {
+ assertTrue("unexpected exception: " + e.getMessage(), e.getMessage().contains("Timed out"));
+ }
+
+ var expectedCounters = new LockCounters();
+ expectedCounters.invokeAcquireCount.set(1);
+ expectedCounters.acquireTimedOutCount.set(1);
+ assertEquals(Map.of(lockPath, expectedCounters), ThreadLockInfo.getLockCountersByPath());
+ }
+
+ @Test
+ public void acquired() throws Exception {
+ when(mutex.acquire(anyLong(), any())).thenReturn(true);
+
+ lock.acquire(acquireTimeout);
+
+ var expectedCounters = new LockCounters();
+ expectedCounters.invokeAcquireCount.set(1);
+ expectedCounters.lockAcquiredCount.set(1);
+ expectedCounters.inCriticalRegionCount.set(1);
+ assertEquals(Map.of(lockPath, expectedCounters), ThreadLockInfo.getLockCountersByPath());
+
+ // reenter
+ lock.acquire(acquireTimeout);
+ expectedCounters.invokeAcquireCount.set(2);
+ expectedCounters.lockAcquiredCount.set(2);
+ expectedCounters.inCriticalRegionCount.set(2);
+
+ // inner-most closes
+ lock.close();
+ expectedCounters.inCriticalRegionCount.set(1);
+ expectedCounters.locksReleasedCount.set(1);
+ assertEquals(Map.of(lockPath, expectedCounters), ThreadLockInfo.getLockCountersByPath());
+
+ // outer-most closes
+ lock.close();
+ expectedCounters.inCriticalRegionCount.set(0);
+ expectedCounters.locksReleasedCount.set(2);
+ assertEquals(Map.of(lockPath, expectedCounters), ThreadLockInfo.getLockCountersByPath());
+ }
+}