2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2022 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2023-2024 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.distributed.locking;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatCode;
26 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
27 import static org.assertj.core.api.Assertions.assertThatThrownBy;
28 import static org.junit.jupiter.api.Assertions.assertEquals;
29 import static org.junit.jupiter.api.Assertions.assertFalse;
30 import static org.junit.jupiter.api.Assertions.assertNotNull;
31 import static org.junit.jupiter.api.Assertions.assertNotSame;
32 import static org.junit.jupiter.api.Assertions.assertNull;
33 import static org.junit.jupiter.api.Assertions.assertSame;
34 import static org.junit.jupiter.api.Assertions.assertTrue;
35 import static org.mockito.ArgumentMatchers.any;
36 import static org.mockito.ArgumentMatchers.anyBoolean;
37 import static org.mockito.ArgumentMatchers.anyLong;
38 import static org.mockito.ArgumentMatchers.eq;
39 import static org.mockito.Mockito.doThrow;
40 import static org.mockito.Mockito.lenient;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.never;
43 import static org.mockito.Mockito.times;
44 import static org.mockito.Mockito.verify;
45 import static org.mockito.Mockito.when;
47 import java.io.ByteArrayInputStream;
48 import java.io.ByteArrayOutputStream;
49 import java.io.ObjectInputStream;
50 import java.io.ObjectOutputStream;
51 import java.sql.Connection;
52 import java.sql.DriverManager;
53 import java.sql.PreparedStatement;
54 import java.sql.ResultSet;
55 import java.sql.SQLException;
56 import java.sql.SQLTransientException;
57 import java.util.ArrayList;
58 import java.util.List;
59 import java.util.Properties;
60 import java.util.concurrent.Executors;
61 import java.util.concurrent.RejectedExecutionException;
62 import java.util.concurrent.ScheduledExecutorService;
63 import java.util.concurrent.ScheduledFuture;
64 import java.util.concurrent.Semaphore;
65 import java.util.concurrent.TimeUnit;
66 import java.util.concurrent.atomic.AtomicBoolean;
67 import java.util.concurrent.atomic.AtomicInteger;
68 import java.util.concurrent.atomic.AtomicReference;
69 import org.apache.commons.dbcp2.BasicDataSource;
70 import org.junit.jupiter.api.AfterAll;
71 import org.junit.jupiter.api.AfterEach;
72 import org.junit.jupiter.api.BeforeAll;
73 import org.junit.jupiter.api.BeforeEach;
74 import org.junit.jupiter.api.Test;
75 import org.junit.jupiter.api.extension.ExtendWith;
76 import org.kie.api.runtime.KieSession;
77 import org.mockito.ArgumentCaptor;
78 import org.mockito.Mock;
79 import org.mockito.MockitoAnnotations;
80 import org.mockito.junit.jupiter.MockitoExtension;
81 import org.onap.policy.common.utils.services.OrderedServiceImpl;
82 import org.onap.policy.distributed.locking.DistributedLockManager.DistributedLock;
83 import org.onap.policy.drools.core.PolicySession;
84 import org.onap.policy.drools.core.lock.Lock;
85 import org.onap.policy.drools.core.lock.LockCallback;
86 import org.onap.policy.drools.core.lock.LockState;
87 import org.onap.policy.drools.features.PolicyEngineFeatureApi;
88 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
89 import org.onap.policy.drools.system.PolicyEngine;
90 import org.onap.policy.drools.system.PolicyEngineConstants;
91 import org.springframework.test.util.ReflectionTestUtils;
93 @ExtendWith(MockitoExtension.class)
94 class DistributedLockManagerTest {
95 private static final long EXPIRE_SEC = 900L;
96 private static final long RETRY_SEC = 60L;
97 private static final String POLICY_ENGINE_EXECUTOR_FIELD = "executorService";
98 private static final String OTHER_HOST = "other-host";
99 private static final String OTHER_OWNER = "other-owner";
100 private static final String EXPECTED_EXCEPTION = "expected exception";
101 private static final String DB_CONNECTION =
102 "jdbc:h2:mem:pooling;INIT=CREATE SCHEMA IF NOT EXISTS pooling\\;SET SCHEMA pooling";
103 private static final String DB_USER = "user";
104 private static final String DB_PASSWORD = "password";
105 private static final String OWNER_KEY = "my key";
106 private static final String RESOURCE = "my resource";
107 private static final String RESOURCE2 = "my resource #2";
108 private static final String RESOURCE3 = "my resource #3";
109 private static final String RESOURCE4 = "my resource #4";
110 private static final String RESOURCE5 = "my resource #5";
111 private static final int HOLD_SEC = 100;
112 private static final int HOLD_SEC2 = 120;
113 private static final int MAX_THREADS = 5;
114 private static final int MAX_LOOPS = 100;
115 private static final boolean TRANSIENT = true;
116 private static final boolean PERMANENT = false;
118 // number of execute() calls before the first lock attempt
119 private static final int PRE_LOCK_EXECS = 1;
121 // number of execute() calls before the first schedule attempt
122 private static final int PRE_SCHED_EXECS = 1;
124 private static Connection conn = null;
125 private static ScheduledExecutorService saveExec;
126 private static ScheduledExecutorService realExec;
129 private PolicyEngine engine;
132 private KieSession kieSess;
135 private ScheduledExecutorService exsvc;
138 private ScheduledFuture<?> checker;
141 private LockCallback callback;
144 private BasicDataSource dataSource;
146 private DistributedLock lock;
148 private AtomicInteger numActive;
149 private AtomicInteger numSuccess;
150 private DistributedLockManager feature;
152 AutoCloseable closeable;
155 * Configures the location of the property files and creates the DB.
157 * @throws SQLException if the DB cannot be created
160 static void setUpBeforeClass() throws SQLException {
161 SystemPersistenceConstants.getManager().setConfigurationDir("src/test/resources");
162 PolicyEngineConstants.getManager().configure(new Properties());
164 conn = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
166 try (PreparedStatement createStmt = conn.prepareStatement("create table pooling.locks "
167 + "(resourceId VARCHAR(128), host VARCHAR(128), owner VARCHAR(128), "
168 + "expirationTime TIMESTAMP DEFAULT 0, PRIMARY KEY (resourceId))")) {
169 createStmt.executeUpdate();
172 saveExec = (ScheduledExecutorService) ReflectionTestUtils.getField(PolicyEngineConstants.getManager(),
173 POLICY_ENGINE_EXECUTOR_FIELD);
175 realExec = Executors.newScheduledThreadPool(3);
179 * Restores static fields.
182 static void tearDownAfterClass() throws SQLException {
183 ReflectionTestUtils.setField(PolicyEngineConstants.getManager(), POLICY_ENGINE_EXECUTOR_FIELD, saveExec);
189 * Initializes the mocks and creates a feature that uses {@link #exsvc} to execute
192 * @throws SQLException if the lock records cannot be deleted from the DB
195 void setUp() throws SQLException {
196 closeable = MockitoAnnotations.openMocks(this);
197 // grant() and deny() calls will come through here and be immediately executed
198 PolicySession session = new PolicySession(null, null, kieSess) {
200 public void insertDrools(Object object) {
201 ((Runnable) object).run();
205 session.setPolicySession();
207 numActive = new AtomicInteger(0);
208 numSuccess = new AtomicInteger(0);
212 feature = new MyLockingFeature(true);
216 void tearDown() throws Exception {
222 private void cleanDb() throws SQLException {
223 try (PreparedStatement stmt = conn.prepareStatement("DELETE FROM pooling.locks")) {
224 stmt.executeUpdate();
228 private void shutdownFeature() {
229 if (feature != null) {
230 feature.afterStop(engine);
236 * Tests that the feature is found in the expected service sets.
239 void testServiceApis() {
240 assertTrue(new OrderedServiceImpl<>(PolicyEngineFeatureApi.class).getList().stream()
241 .anyMatch(obj -> obj instanceof DistributedLockManager));
245 void testGetSequenceNumber() {
246 assertEquals(1000, feature.getSequenceNumber());
250 void testBeforeCreateLockManager() {
251 assertSame(feature, feature.beforeCreateLockManager());
255 * Tests beforeCreate(), when getProperties() throws a runtime exception.
258 void testBeforeCreateLockManagerEx() {
261 feature = new MyLockingFeature(false) {
263 protected Properties getProperties() {
264 throw new IllegalArgumentException(EXPECTED_EXCEPTION);
268 assertThatThrownBy(() -> feature.beforeCreateLockManager())
269 .isInstanceOf(DistributedLockManagerException.class);
273 void testAfterStart() {
274 // verify that cleanup & expire check are both added to the queue
275 verify(exsvc).execute(any());
276 verify(exsvc).schedule(any(Runnable.class), anyLong(), any());
280 * Tests afterStart(), when thread pool throws a runtime exception.
283 void testAfterStartExInThreadPool() {
286 feature = new MyLockingFeature(false);
288 doThrow(new IllegalArgumentException(EXPECTED_EXCEPTION)).when(exsvc).execute(any());
290 assertThatThrownBy(() -> feature.afterStart(engine)).isInstanceOf(DistributedLockManagerException.class);
294 void testDeleteExpiredDbLocks() throws SQLException {
295 // add records: two expired, one not
296 insertRecord(RESOURCE, feature.getUuidString(), -1);
297 insertRecord(RESOURCE2, feature.getUuidString(), HOLD_SEC2);
298 insertRecord(RESOURCE3, OTHER_OWNER, 0);
299 insertRecord(RESOURCE4, OTHER_OWNER, HOLD_SEC);
301 // get the clean-up function and execute it
302 ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class);
303 verify(exsvc).execute(captor.capture());
305 long tbegin = System.currentTimeMillis();
306 Runnable action = captor.getValue();
309 assertFalse(recordInRange(RESOURCE, feature.getUuidString(), HOLD_SEC2, tbegin));
310 assertTrue(recordInRange(RESOURCE2, feature.getUuidString(), HOLD_SEC2, tbegin));
311 assertFalse(recordInRange(RESOURCE3, OTHER_OWNER, HOLD_SEC, tbegin));
312 assertTrue(recordInRange(RESOURCE4, OTHER_OWNER, HOLD_SEC, tbegin));
314 assertEquals(2, getRecordCount());
318 * Tests deleteExpiredDbLocks(), when getConnection() throws an exception.
321 void testDeleteExpiredDbLocksEx() {
322 feature = new InvalidDbLockingFeature(TRANSIENT);
324 // get the clean-up function and execute it
325 ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class);
326 verify(exsvc).execute(captor.capture());
328 Runnable action = captor.getValue();
330 // should not throw an exception
335 void testAfterStop() {
337 verify(checker).cancel(anyBoolean());
339 feature = new DistributedLockManager();
341 // shutdown without calling afterStart()
347 * Tests afterStop(), when the data source throws an exception when close() is called.
350 void testAfterStopEx() {
353 // use a data source that throws an exception when closed
354 feature = new InvalidDbLockingFeature(TRANSIENT);
356 assertThatCode(this::shutdownFeature).doesNotThrowAnyException();
360 void testCreateLock() throws SQLException {
361 verify(exsvc).execute(any());
363 lock = getLock(RESOURCE, callback);
364 assertTrue(lock.isWaiting());
366 verify(exsvc, times(PRE_LOCK_EXECS + 1)).execute(any());
368 // this lock should fail
369 LockCallback callback2 = mock(LockCallback.class);
370 DistributedLock lock2 = getLock(RESOURCE, callback2);
371 assertTrue(lock2.isUnavailable());
372 verify(callback2, never()).lockAvailable(lock2);
373 verify(callback2).lockUnavailable(lock2);
375 // this should fail, too
376 LockCallback callback3 = mock(LockCallback.class);
377 DistributedLock lock3 = getLock(RESOURCE, callback3);
378 assertTrue(lock3.isUnavailable());
379 verify(callback3, never()).lockAvailable(lock3);
380 verify(callback3).lockUnavailable(lock3);
382 // no change to first
383 assertTrue(lock.isWaiting());
385 // no callbacks to the first lock
386 verify(callback, never()).lockAvailable(lock);
387 verify(callback, never()).lockUnavailable(lock);
389 assertTrue(lock.isWaiting());
390 assertEquals(0, getRecordCount());
393 assertTrue(lock.isActive());
394 assertEquals(1, getRecordCount());
396 verify(callback).lockAvailable(lock);
397 verify(callback, never()).lockUnavailable(lock);
399 // this should succeed
400 DistributedLock lock4 = getLock(RESOURCE2, callback);
401 assertTrue(lock4.isWaiting());
403 // after running checker, original records should still remain
404 runChecker(0, EXPIRE_SEC);
405 assertEquals(1, getRecordCount());
406 verify(callback, never()).lockUnavailable(lock);
410 * Tests createLock() when the feature is not the latest instance.
413 void testCreateLockNotLatestInstance() {
414 DistributedLockManager.setLatestInstance(null);
416 Lock newLock = feature.createLock(RESOURCE, OWNER_KEY, HOLD_SEC, callback, false);
417 assertTrue(newLock.isUnavailable());
418 verify(callback, never()).lockAvailable(any());
419 verify(callback).lockUnavailable(newLock);
423 void testCheckExpired() throws SQLException {
424 lock = getLock(RESOURCE, callback);
427 LockCallback callback2 = mock(LockCallback.class);
428 final DistributedLock lock2 = getLock(RESOURCE2, callback2);
431 LockCallback callback3 = mock(LockCallback.class);
432 final DistributedLock lock3 = getLock(RESOURCE3, callback3);
435 LockCallback callback4 = mock(LockCallback.class);
436 final DistributedLock lock4 = getLock(RESOURCE4, callback4);
439 LockCallback callback5 = mock(LockCallback.class);
440 final DistributedLock lock5 = getLock(RESOURCE5, callback5);
443 assertEquals(5, getRecordCount());
446 updateRecord(RESOURCE, feature.getPdpName(), feature.getUuidString(), -1);
448 // change host of another record
449 updateRecord(RESOURCE3, OTHER_HOST, feature.getUuidString(), HOLD_SEC);
451 // change uuid of another record
452 updateRecord(RESOURCE5, feature.getPdpName(), OTHER_OWNER, HOLD_SEC);
455 runChecker(0, EXPIRE_SEC);
458 assertTrue(lock.isUnavailable());
459 assertTrue(lock2.isActive());
460 assertTrue(lock3.isUnavailable());
461 assertTrue(lock4.isActive());
462 assertTrue(lock5.isUnavailable());
468 verify(callback).lockUnavailable(lock);
469 verify(callback3).lockUnavailable(lock3);
470 verify(callback5).lockUnavailable(lock5);
472 verify(callback2, never()).lockUnavailable(lock2);
473 verify(callback4, never()).lockUnavailable(lock4);
475 // another check should have been scheduled, with the normal interval
476 runChecker(1, EXPIRE_SEC);
480 * Tests checkExpired(), when schedule() throws an exception.
483 void testCheckExpiredExecRejected() {
484 // arrange for execution to be rejected
485 when(exsvc.schedule(any(Runnable.class), anyLong(), any()))
486 .thenThrow(new RejectedExecutionException(EXPECTED_EXCEPTION));
488 runChecker(0, EXPIRE_SEC);
492 * Tests checkExpired(), when getConnection() throws an exception.
495 void testCheckExpiredSqlEx() {
496 // use a data source that throws an exception when getConnection() is called
497 feature = new InvalidDbLockingFeature(TRANSIENT);
499 runChecker(0, EXPIRE_SEC);
501 // it should have scheduled another check, sooner
502 runChecker(0, RETRY_SEC);
506 * Tests checkExpired(), when getConnection() throws an exception and the feature is
510 void testCheckExpiredSqlExFeatureStopped() {
511 // use a data source that throws an exception when getConnection() is called
512 feature = new InvalidDbLockingFeature(TRANSIENT) {
514 protected SQLException makeEx() {
516 return super.makeEx();
520 runChecker(0, EXPIRE_SEC);
522 // it should NOT have scheduled another check
523 verify(exsvc, times(1)).schedule(any(Runnable.class), anyLong(), any());
527 void testExpireLocks() throws SQLException {
528 AtomicReference<DistributedLock> freeLock = new AtomicReference<>(null);
530 feature = new MyLockingFeature(true) {
532 protected BasicDataSource makeDataSource() throws SQLException {
533 // get the real data source
534 BasicDataSource src2 = super.makeDataSource();
536 when(dataSource.getConnection()).thenAnswer(answer -> {
537 DistributedLock lck = freeLock.getAndSet(null);
546 return src2.getConnection();
553 lock = getLock(RESOURCE, callback);
556 LockCallback callback2 = mock(LockCallback.class);
557 final DistributedLock lock2 = getLock(RESOURCE2, callback2);
560 LockCallback callback3 = mock(LockCallback.class);
561 final DistributedLock lock3 = getLock(RESOURCE3, callback3);
562 // don't run doLock for lock3 - leave it in the waiting state
564 LockCallback callback4 = mock(LockCallback.class);
565 final DistributedLock lock4 = getLock(RESOURCE4, callback4);
568 assertEquals(3, getRecordCount());
571 updateRecord(RESOURCE, feature.getPdpName(), feature.getUuidString(), -1);
573 // arrange to free lock4 while the checker is running
577 runChecker(0, EXPIRE_SEC);
580 assertTrue(lock.isUnavailable());
581 assertTrue(lock2.isActive());
582 assertTrue(lock3.isWaiting());
583 assertTrue(lock4.isUnavailable());
586 verify(exsvc, times(PRE_LOCK_EXECS + 5)).execute(any());
588 verify(callback).lockUnavailable(lock);
589 verify(callback2, never()).lockUnavailable(lock2);
590 verify(callback3, never()).lockUnavailable(lock3);
591 verify(callback4, never()).lockUnavailable(lock4);
595 void testDistributedLockNoArgs() {
596 DistributedLock newLock = new DistributedLock();
597 assertNull(newLock.getResourceId());
598 assertNull(newLock.getOwnerKey());
599 assertNull(newLock.getCallback());
600 assertEquals(0, newLock.getHoldSec());
604 void testDistributedLock() {
605 assertThatIllegalArgumentException()
606 .isThrownBy(() -> feature.createLock(RESOURCE, OWNER_KEY, -1, callback, false))
607 .withMessageContaining("holdSec");
609 // should generate no exception
610 feature.createLock(RESOURCE, OWNER_KEY, HOLD_SEC, callback, false);
614 void testDistributedLockSerializable() throws Exception {
615 DistributedLock newLock = getLock(RESOURCE, callback);
616 newLock = roundTrip(newLock);
618 assertTrue(newLock.isWaiting());
620 assertEquals(RESOURCE, newLock.getResourceId());
621 assertEquals(OWNER_KEY, newLock.getOwnerKey());
622 assertNull(newLock.getCallback());
623 assertEquals(HOLD_SEC, newLock.getHoldSec());
628 lock = getLock(RESOURCE, callback);
629 assertFalse(lock.isActive());
631 // execute the doLock() call
634 assertTrue(lock.isActive());
636 // the callback for the lock should have been run in the foreground thread
637 verify(callback).lockAvailable(lock);
641 void testDistributedLockDeny() {
643 feature.createLock(RESOURCE, OWNER_KEY, HOLD_SEC, callback, false);
645 // get another lock - should fail
646 lock = getLock(RESOURCE, callback);
648 assertTrue(lock.isUnavailable());
650 // the callback for the second lock should have been run in the foreground thread
651 verify(callback).lockUnavailable(lock);
653 // should only have a request for the first lock
654 verify(exsvc, times(PRE_LOCK_EXECS + 1)).execute(any());
658 void testDistributedLockFree() {
659 lock = getLock(RESOURCE, callback);
661 assertTrue(lock.free());
662 assertTrue(lock.isUnavailable());
664 // run both requests associated with the lock
668 // should not have changed state
669 assertTrue(lock.isUnavailable());
671 // attempt to free it again
672 assertFalse(lock.free());
674 // should not have queued anything else
675 verify(exsvc, times(PRE_LOCK_EXECS + 2)).execute(any());
677 // new lock should succeed
678 DistributedLock lock2 = getLock(RESOURCE, callback);
679 assertNotSame(lock2, lock);
680 assertTrue(lock2.isWaiting());
684 * Tests that free() works on a serialized lock with a new feature.
686 * @throws Exception if an error occurs
689 void testDistributedLockFreeSerialized() throws Exception {
690 DistributedLock newLock = getLock(RESOURCE, callback);
692 feature = new MyLockingFeature(true);
694 newLock = roundTrip(newLock);
695 assertTrue(newLock.free());
696 assertTrue(newLock.isUnavailable());
700 * Tests free() on a serialized lock without a feature.
702 * @throws Exception if an error occurs
705 void testDistributedLockFreeNoFeature() throws Exception {
706 DistributedLock newLock = getLock(RESOURCE, callback);
708 DistributedLockManager.setLatestInstance(null);
710 newLock = roundTrip(newLock);
711 assertFalse(newLock.free());
712 assertTrue(newLock.isUnavailable());
716 * Tests the case where the lock is freed and doUnlock called between the call to
717 * isUnavailable() and the call to compute().
720 void testDistributedLockFreeUnlocked() {
721 feature = new FreeWithFreeLockingFeature(true);
723 lock = getLock(RESOURCE, callback);
725 assertFalse(lock.free());
726 assertTrue(lock.isUnavailable());
730 * Tests the case where the lock is freed, but doUnlock is not completed, between the
731 * call to isUnavailable() and the call to compute().
734 void testDistributedLockFreeLockFreed() {
735 feature = new FreeWithFreeLockingFeature(false);
737 lock = getLock(RESOURCE, callback);
739 assertFalse(lock.free());
740 assertTrue(lock.isUnavailable());
744 void testDistributedLockExtend() {
745 lock = getLock(RESOURCE, callback);
747 // lock2 should be denied - called back by this thread
748 DistributedLock lock2 = getLock(RESOURCE, callback);
749 verify(callback, never()).lockAvailable(lock2);
750 verify(callback).lockUnavailable(lock2);
752 // lock2 will still be denied - called back by this thread
753 lock2.extend(HOLD_SEC, callback);
754 verify(callback, times(2)).lockUnavailable(lock2);
756 // force lock2 to be active - should still be denied
757 ReflectionTestUtils.setField(lock2, "state", LockState.ACTIVE);
758 lock2.extend(HOLD_SEC, callback);
759 verify(callback, times(3)).lockUnavailable(lock2);
761 assertThatIllegalArgumentException().isThrownBy(() -> lock.extend(-1, callback))
762 .withMessageContaining("holdSec");
766 assertTrue(lock.isActive());
768 // now extend the first lock
769 LockCallback callback2 = mock(LockCallback.class);
770 lock.extend(HOLD_SEC2, callback2);
771 assertTrue(lock.isWaiting());
773 // execute doExtend()
775 lock.extend(HOLD_SEC2, callback2);
776 assertEquals(HOLD_SEC2, lock.getHoldSec());
777 verify(callback2).lockAvailable(lock);
778 verify(callback2, never()).lockUnavailable(lock);
782 * Tests that extend() works on a serialized lock with a new feature.
784 * @throws Exception if an error occurs
787 void testDistributedLockExtendSerialized() throws Exception {
788 DistributedLock newLock = getLock(RESOURCE, callback);
792 assertTrue(newLock.isActive());
794 feature = new MyLockingFeature(true);
796 newLock = roundTrip(newLock);
797 assertTrue(newLock.isActive());
799 LockCallback mockCallback = mock(LockCallback.class);
801 newLock.extend(HOLD_SEC, mockCallback);
802 assertTrue(newLock.isWaiting());
804 // run doExtend (in new feature)
806 assertTrue(newLock.isActive());
808 verify(mockCallback).lockAvailable(newLock);
809 verify(mockCallback, never()).lockUnavailable(newLock);
813 * Tests extend() on a serialized lock without a feature.
815 * @throws Exception if an error occurs
818 void testDistributedLockExtendNoFeature() throws Exception {
819 DistributedLock newLock = getLock(RESOURCE, callback);
823 assertTrue(newLock.isActive());
825 DistributedLockManager.setLatestInstance(null);
827 newLock = roundTrip(newLock);
828 assertTrue(newLock.isActive());
830 LockCallback scallback = mock(LockCallback.class);
832 newLock.extend(HOLD_SEC, scallback);
833 assertTrue(newLock.isUnavailable());
835 verify(scallback, never()).lockAvailable(newLock);
836 verify(scallback).lockUnavailable(newLock);
840 * Tests the case where the lock is freed and doUnlock called between the call to
841 * isUnavailable() and the call to compute().
844 void testDistributedLockExtendUnlocked() {
845 feature = new FreeWithFreeLockingFeature(true);
847 lock = getLock(RESOURCE, callback);
849 lock.extend(HOLD_SEC2, callback);
850 assertTrue(lock.isUnavailable());
851 verify(callback).lockUnavailable(lock);
855 * Tests the case where the lock is freed, but doUnlock is not completed, between the
856 * call to isUnavailable() and the call to compute().
859 void testDistributedLockExtendLockFreed() {
860 feature = new FreeWithFreeLockingFeature(false);
862 lock = getLock(RESOURCE, callback);
864 lock.extend(HOLD_SEC2, callback);
865 assertTrue(lock.isUnavailable());
866 verify(callback).lockUnavailable(lock);
870 void testDistributedLockScheduleRequest() {
871 lock = getLock(RESOURCE, callback);
874 verify(callback).lockAvailable(lock);
878 void testDistributedLockRescheduleRequest() {
879 // use a data source that throws an exception when getConnection() is called
880 InvalidDbLockingFeature invfeat = new InvalidDbLockingFeature(TRANSIENT);
883 lock = getLock(RESOURCE, callback);
885 // invoke doLock - should fail and reschedule
888 // should still be waiting
889 assertTrue(lock.isWaiting());
890 verify(callback, never()).lockUnavailable(lock);
892 // free the lock while doLock is executing
893 invfeat.freeLock = true;
895 // try scheduled request - should just invoke doUnlock
898 // should still be waiting
899 assertTrue(lock.isUnavailable());
900 verify(callback, never()).lockUnavailable(lock);
902 // should have scheduled a retry of doUnlock
903 verify(exsvc, times(PRE_SCHED_EXECS + 2)).schedule(any(Runnable.class), anyLong(), any());
907 void testDistributedLockGetNextRequest() {
908 lock = getLock(RESOURCE, callback);
911 * run doLock. This should cause getNextRequest() to be called twice, once with a
912 * request in the queue, and the second time with request=null.
918 * Tests getNextRequest(), where the same request is still in the queue the second
922 void testDistributedLockGetNextRequestSameRequest() {
923 // force reschedule to be invoked
924 feature = new InvalidDbLockingFeature(TRANSIENT);
926 lock = getLock(RESOURCE, callback);
929 * run doLock. This should cause getNextRequest() to be called twice, once with a
930 * request in the queue, and the second time with the same request again.
934 verify(exsvc, times(PRE_SCHED_EXECS + 1)).schedule(any(Runnable.class), anyLong(), any());
938 void testDistributedLockDoRequest() {
939 lock = getLock(RESOURCE, callback);
941 assertTrue(lock.isWaiting());
943 // run doLock via doRequest
946 assertTrue(lock.isActive());
950 * Tests doRequest(), when doRequest() is already running within another thread.
953 void testDistributedLockDoRequestBusy() {
955 * this feature will invoke a request in a background thread while it's being run
956 * in a foreground thread.
958 AtomicBoolean running = new AtomicBoolean(false);
959 AtomicBoolean returned = new AtomicBoolean(false);
961 feature = new MyLockingFeature(true) {
963 protected DistributedLock makeLock(LockState state, String resourceId, String ownerKey, int holdSec,
964 LockCallback callback) {
965 return new DistributedLock(state, resourceId, ownerKey, holdSec, callback, feature) {
966 private static final long serialVersionUID = 1L;
969 protected boolean doDbInsert(Connection conn) throws SQLException {
971 // already inside the thread - don't recurse any further
972 return super.doDbInsert(conn);
977 Thread thread = new Thread(() -> {
978 // run doLock from within the new thread
981 thread.setDaemon(true);
984 // wait for the background thread to complete before continuing
987 } catch (InterruptedException ignore) {
988 Thread.currentThread().interrupt();
991 returned.set(!thread.isAlive());
993 return super.doDbInsert(conn);
999 lock = getLock(RESOURCE, callback);
1004 assertTrue(returned.get());
1008 * Tests doRequest() when an exception occurs while the lock is in the WAITING state.
1010 * @throws SQLException if an error occurs
1013 void testDistributedLockDoRequestRunExWaiting() throws SQLException {
1014 // throw run-time exception
1015 when(dataSource.getConnection()).thenThrow(new IllegalStateException(EXPECTED_EXCEPTION));
1017 // use a data source that throws an exception when getConnection() is called
1018 feature = new MyLockingFeature(true) {
1020 protected BasicDataSource makeDataSource() {
1025 lock = getLock(RESOURCE, callback);
1027 // invoke doLock - should NOT reschedule
1030 assertTrue(lock.isUnavailable());
1031 verify(callback).lockUnavailable(lock);
1033 verify(exsvc, times(PRE_SCHED_EXECS)).schedule(any(Runnable.class), anyLong(), any());
1037 * Tests doRequest() when an exception occurs while the lock is in the UNAVAILABLE
1040 * @throws SQLException if an error occurs
1043 void testDistributedLockDoRequestRunExUnavailable() throws SQLException {
1044 // throw run-time exception
1045 when(dataSource.getConnection()).thenAnswer(answer -> {
1047 throw new IllegalStateException(EXPECTED_EXCEPTION);
1050 // use a data source that throws an exception when getConnection() is called
1051 feature = new MyLockingFeature(true) {
1053 protected BasicDataSource makeDataSource() {
1058 lock = getLock(RESOURCE, callback);
1060 // invoke doLock - should NOT reschedule
1063 assertTrue(lock.isUnavailable());
1064 verify(callback, never()).lockUnavailable(lock);
1066 verify(exsvc, times(PRE_SCHED_EXECS)).schedule(any(Runnable.class), anyLong(), any());
1070 * Tests doRequest() when the retry count gets exhausted.
1073 void testDistributedLockDoRequestRetriesExhaustedWhileLocking() {
1074 // use a data source that throws an exception when getConnection() is called
1075 feature = new InvalidDbLockingFeature(TRANSIENT);
1077 lock = getLock(RESOURCE, callback);
1079 // invoke doLock - should fail and reschedule
1082 // should still be waiting
1083 assertTrue(lock.isWaiting());
1084 verify(callback, never()).lockUnavailable(lock);
1086 // try again, via SCHEDULER - first retry fails
1089 // should still be waiting
1090 assertTrue(lock.isWaiting());
1091 verify(callback, never()).lockUnavailable(lock);
1093 // try again, via SCHEDULER - final retry fails
1095 assertTrue(lock.isUnavailable());
1097 // now callback should have been called
1098 verify(callback).lockUnavailable(lock);
1102 * Tests doRequest() when a non-transient DB exception is thrown.
1105 void testDistributedLockDoRequestNotTransient() {
1107 * use a data source that throws a PERMANENT exception when getConnection() is
1110 feature = new InvalidDbLockingFeature(PERMANENT);
1112 lock = getLock(RESOURCE, callback);
1114 // invoke doLock - should fail
1117 assertTrue(lock.isUnavailable());
1118 verify(callback).lockUnavailable(lock);
1120 // should not have scheduled anything new
1121 verify(exsvc, times(PRE_LOCK_EXECS + 1)).execute(any());
1122 verify(exsvc, times(PRE_SCHED_EXECS)).schedule(any(Runnable.class), anyLong(), any());
1126 void testDistributedLockDoLock() throws SQLException {
1127 lock = getLock(RESOURCE, callback);
1129 // invoke doLock - should simply do an insert
1130 long tbegin = System.currentTimeMillis();
1133 assertEquals(1, getRecordCount());
1134 assertTrue(recordInRange(RESOURCE, feature.getUuidString(), HOLD_SEC, tbegin));
1135 verify(callback).lockAvailable(lock);
1139 * Tests doLock() when the lock is freed before doLock runs.
1141 * @throws SQLException if an error occurs
1144 void testDistributedLockDoLockFreed() throws SQLException {
1145 lock = getLock(RESOURCE, callback);
1147 lock.setState(LockState.UNAVAILABLE);
1149 // invoke doLock - should do nothing
1152 assertEquals(0, getRecordCount());
1154 verify(callback, never()).lockAvailable(lock);
1158 * Tests doLock() when a DB exception is thrown.
1161 void testDistributedLockDoLockEx() {
1162 // use a data source that throws an exception when getConnection() is called
1163 feature = new InvalidDbLockingFeature(PERMANENT);
1165 lock = getLock(RESOURCE, callback);
1167 // invoke doLock - should simply do an insert
1170 // lock should have failed due to exception
1171 verify(callback).lockUnavailable(lock);
1175 * Tests doLock() when an (expired) record already exists, thus requiring doUpdate()
1179 void testDistributedLockDoLockNeedingUpdate() throws SQLException {
1180 // insert an expired record
1181 insertRecord(RESOURCE, feature.getUuidString(), 0);
1183 lock = getLock(RESOURCE, callback);
1185 // invoke doLock - should simply do an update
1187 verify(callback).lockAvailable(lock);
1191 * Tests doLock() when a locked record already exists.
1194 void testDistributedLockDoLockAlreadyLocked() throws SQLException {
1195 // insert an expired record
1196 insertRecord(RESOURCE, OTHER_OWNER, HOLD_SEC);
1198 lock = getLock(RESOURCE, callback);
1203 // lock should have failed because it's already locked
1204 verify(callback).lockUnavailable(lock);
1208 void testDistributedLockDoUnlock() throws SQLException {
1209 lock = getLock(RESOURCE, callback);
1216 // invoke doUnlock()
1217 long tbegin = System.currentTimeMillis();
1220 assertEquals(0, getRecordCount());
1221 assertFalse(recordInRange(RESOURCE, feature.getUuidString(), HOLD_SEC, tbegin));
1223 assertTrue(lock.isUnavailable());
1225 // no more callbacks should have occurred
1226 verify(callback, times(1)).lockAvailable(lock);
1227 verify(callback, never()).lockUnavailable(lock);
1231 * Tests doUnlock() when a DB exception is thrown.
1234 void testDistributedLockDoUnlockEx() {
1235 feature = new InvalidDbLockingFeature(PERMANENT);
1237 lock = getLock(RESOURCE, callback);
1239 // do NOT invoke doLock() - it will fail without a DB connection
1243 // invoke doUnlock()
1246 assertTrue(lock.isUnavailable());
1248 // no more callbacks should have occurred
1249 verify(callback, never()).lockAvailable(lock);
1250 verify(callback, never()).lockUnavailable(lock);
1254 void testDistributedLockDoExtend() throws SQLException {
1255 lock = getLock(RESOURCE, callback);
1258 LockCallback callback2 = mock(LockCallback.class);
1259 lock.extend(HOLD_SEC2, callback2);
1262 long tbegin = System.currentTimeMillis();
1265 assertEquals(1, getRecordCount());
1266 assertTrue(recordInRange(RESOURCE, feature.getUuidString(), HOLD_SEC2, tbegin));
1268 assertTrue(lock.isActive());
1270 // no more callbacks should have occurred
1271 verify(callback).lockAvailable(lock);
1272 verify(callback, never()).lockUnavailable(lock);
1274 // extension should have succeeded
1275 verify(callback2).lockAvailable(lock);
1276 verify(callback2, never()).lockUnavailable(lock);
1280 * Tests doExtend() when the lock is freed before doExtend runs.
1282 * @throws SQLException if an error occurs
1285 void testDistributedLockDoExtendFreed() throws SQLException {
1286 lock = getLock(RESOURCE, callback);
1287 lock.extend(HOLD_SEC2, callback);
1289 lock.setState(LockState.UNAVAILABLE);
1291 // invoke doExtend - should do nothing
1294 assertEquals(0, getRecordCount());
1296 verify(callback, never()).lockAvailable(lock);
1300 * Tests doExtend() when the lock record is missing from the DB, thus requiring an
1303 * @throws SQLException if an error occurs
1306 void testDistributedLockDoExtendInsertNeeded() throws SQLException {
1307 lock = getLock(RESOURCE, callback);
1310 LockCallback callback2 = mock(LockCallback.class);
1311 lock.extend(HOLD_SEC2, callback2);
1313 // delete the record so it's forced to re-insert it
1317 long tbegin = System.currentTimeMillis();
1320 assertEquals(1, getRecordCount());
1321 assertTrue(recordInRange(RESOURCE, feature.getUuidString(), HOLD_SEC2, tbegin));
1323 assertTrue(lock.isActive());
1325 // no more callbacks should have occurred
1326 verify(callback).lockAvailable(lock);
1327 verify(callback, never()).lockUnavailable(lock);
1329 // extension should have succeeded
1330 verify(callback2).lockAvailable(lock);
1331 verify(callback2, never()).lockUnavailable(lock);
1335 * Tests doExtend() when both update and insert fail.
1338 void testDistributedLockDoExtendNeitherSucceeds() {
1340 * this feature will create a lock that returns false when doDbUpdate() is
1341 * invoked, or when doDbInsert() is invoked a second time
1343 feature = new MyLockingFeature(true) {
1345 protected DistributedLock makeLock(LockState state, String resourceId, String ownerKey, int holdSec,
1346 LockCallback callback) {
1347 return new DistributedLock(state, resourceId, ownerKey, holdSec, callback, feature) {
1348 private static final long serialVersionUID = 1L;
1349 private int ntimes = 0;
1352 protected boolean doDbInsert(Connection conn) throws SQLException {
1357 return super.doDbInsert(conn);
1361 protected boolean doDbUpdate(Connection conn) {
1368 lock = getLock(RESOURCE, callback);
1371 LockCallback callback2 = mock(LockCallback.class);
1372 lock.extend(HOLD_SEC2, callback2);
1377 assertTrue(lock.isUnavailable());
1379 // no more callbacks should have occurred
1380 verify(callback).lockAvailable(lock);
1381 verify(callback, never()).lockUnavailable(lock);
1383 // extension should have failed
1384 verify(callback2, never()).lockAvailable(lock);
1385 verify(callback2).lockUnavailable(lock);
1389 * Tests doExtend() when an exception occurs.
1391 * @throws SQLException if an error occurs
1394 void testDistributedLockDoExtendEx() throws SQLException {
1395 lock = getLock(RESOURCE, callback);
1399 * delete the record and insert one with a different owner, which will cause
1400 * doDbInsert() to throw an exception
1403 insertRecord(RESOURCE, OTHER_OWNER, HOLD_SEC);
1405 LockCallback callback2 = mock(LockCallback.class);
1406 lock.extend(HOLD_SEC2, callback2);
1411 assertTrue(lock.isUnavailable());
1413 // no more callbacks should have occurred
1414 verify(callback).lockAvailable(lock);
1415 verify(callback, never()).lockUnavailable(lock);
1417 // extension should have failed
1418 verify(callback2, never()).lockAvailable(lock);
1419 verify(callback2).lockUnavailable(lock);
1423 void testDistributedLockToString() {
1424 String text = getLock(RESOURCE, callback).toString();
1425 assertNotNull(text);
1426 assertThat(text).doesNotContain("ownerInfo").doesNotContain("callback");
1430 void testMakeThreadPool() {
1431 // use a REAL feature to test this
1432 feature = new DistributedLockManager();
1434 // this should create a thread pool
1435 feature.beforeCreateLockManager();
1436 feature.afterStart(engine);
1438 assertThatCode(this::shutdownFeature).doesNotThrowAnyException();
1442 * Performs a multithreaded test of the locking facility.
1444 * @throws InterruptedException if the current thread is interrupted while waiting for
1445 * the background threads to complete
1448 void testMultiThreaded() throws InterruptedException {
1449 ReflectionTestUtils.setField(PolicyEngineConstants.getManager(), POLICY_ENGINE_EXECUTOR_FIELD, realExec);
1451 feature = new DistributedLockManager();
1452 feature.beforeCreateLockManager();
1453 feature.afterStart(PolicyEngineConstants.getManager());
1455 List<MyThread> threads = new ArrayList<>(MAX_THREADS);
1456 for (int x = 0; x < MAX_THREADS; ++x) {
1457 threads.add(new MyThread());
1460 threads.forEach(Thread::start);
1462 for (MyThread thread : threads) {
1464 assertFalse(thread.isAlive());
1467 for (MyThread thread : threads) {
1468 if (thread.err != null) {
1473 assertTrue(numSuccess.get() > 0);
1476 private DistributedLock getLock(String resource, LockCallback callback) {
1477 return (DistributedLock) feature.createLock(resource, DistributedLockManagerTest.OWNER_KEY,
1478 DistributedLockManagerTest.HOLD_SEC, callback, false);
1481 private DistributedLock roundTrip(DistributedLock lock) throws Exception {
1482 ByteArrayOutputStream baos = new ByteArrayOutputStream();
1483 try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
1484 oos.writeObject(lock);
1487 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
1488 try (ObjectInputStream ois = new ObjectInputStream(bais)) {
1489 return (DistributedLock) ois.readObject();
1494 * Runs the checkExpired() action.
1496 * @param nskip number of actions in the work queue to skip
1497 * @param schedSec number of seconds for which the checker should have been scheduled
1499 private void runChecker(int nskip, long schedSec) {
1500 ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class);
1501 verify(exsvc, times(nskip + 1)).schedule(captor.capture(), eq(schedSec), eq(TimeUnit.SECONDS));
1502 Runnable action = captor.getAllValues().get(nskip);
1507 * Runs a lock action (e.g., doLock, doUnlock).
1509 * @param nskip number of actions in the work queue to skip
1510 * @param nadditional number of additional actions that appear in the work queue
1511 * <i>after</i> the desired action
1513 void runLock(int nskip, int nadditional) {
1514 ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class);
1515 verify(exsvc, times(PRE_LOCK_EXECS + nskip + nadditional + 1)).execute(captor.capture());
1517 Runnable action = captor.getAllValues().get(PRE_LOCK_EXECS + nskip);
1522 * Runs a scheduled action (e.g., "retry" action).
1524 * @param nskip number of actions in the work queue to skip
1526 void runSchedule(int nskip) {
1527 ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class);
1528 verify(exsvc, times(PRE_SCHED_EXECS + nskip + 1)).schedule(captor.capture(), anyLong(), any());
1530 Runnable action = captor.getAllValues().get(PRE_SCHED_EXECS + nskip);
1535 * Gets a count of the number of lock records in the DB.
1537 * @return the number of lock records in the DB
1538 * @throws SQLException if an error occurs accessing the DB
1540 private int getRecordCount() throws SQLException {
1541 try (PreparedStatement stmt = conn.prepareStatement("SELECT count(*) FROM pooling.locks");
1542 ResultSet result = stmt.executeQuery()) {
1544 if (result.next()) {
1545 return result.getInt(1);
1554 * Determines if there is a record for the given resource whose expiration time is in
1555 * the expected range.
1557 * @param resourceId ID of the resource of interest
1558 * @param uuidString UUID string of the owner
1559 * @param holdSec seconds for which the lock was to be held
1560 * @param tbegin earliest time, in milliseconds, at which the record could have been
1561 * inserted into the DB
1562 * @return {@code true} if a record is found, {@code false} otherwise
1563 * @throws SQLException if an error occurs accessing the DB
1565 private boolean recordInRange(String resourceId, String uuidString, int holdSec, long tbegin) throws SQLException {
1566 try (PreparedStatement stmt =
1567 conn.prepareStatement("SELECT timestampdiff(second, now(), expirationTime) FROM pooling.locks"
1568 + " WHERE resourceId=? AND host=? AND owner=?")) {
1570 stmt.setString(1, resourceId);
1571 stmt.setString(2, feature.getPdpName());
1572 stmt.setString(3, uuidString);
1574 try (ResultSet result = stmt.executeQuery()) {
1575 if (result.next()) {
1576 int remaining = result.getInt(1);
1577 long maxDiff = System.currentTimeMillis() - tbegin;
1578 return (remaining >= 0 && holdSec - remaining <= maxDiff);
1588 * Inserts a record into the DB.
1590 * @param resourceId ID of the resource of interest
1591 * @param uuidString UUID string of the owner
1592 * @param expireOffset offset, in seconds, from "now", at which the lock should expire
1593 * @throws SQLException if an error occurs accessing the DB
1595 private void insertRecord(String resourceId, String uuidString, int expireOffset) throws SQLException {
1596 this.insertRecord(resourceId, feature.getPdpName(), uuidString, expireOffset);
1599 private void insertRecord(String resourceId, String hostName, String uuidString, int expireOffset)
1600 throws SQLException {
1601 try (PreparedStatement stmt =
1602 conn.prepareStatement("INSERT INTO pooling.locks (resourceId, host, owner, expirationTime) "
1603 + "values (?, ?, ?, timestampadd(second, ?, now()))")) {
1605 stmt.setString(1, resourceId);
1606 stmt.setString(2, hostName);
1607 stmt.setString(3, uuidString);
1608 stmt.setInt(4, expireOffset);
1610 assertEquals(1, stmt.executeUpdate());
1615 * Updates a record in the DB.
1617 * @param resourceId ID of the resource of interest
1618 * @param newUuid UUID string of the <i>new</i> owner
1619 * @param expireOffset offset, in seconds, from "now", at which the lock should expire
1620 * @throws SQLException if an error occurs accessing the DB
1622 private void updateRecord(String resourceId, String newHost, String newUuid, int expireOffset) throws SQLException {
1623 try (PreparedStatement stmt = conn.prepareStatement("UPDATE pooling.locks SET host=?, owner=?,"
1624 + " expirationTime=timestampadd(second, ?, now()) WHERE resourceId=?")) {
1626 stmt.setString(1, newHost);
1627 stmt.setString(2, newUuid);
1628 stmt.setInt(3, expireOffset);
1629 stmt.setString(4, resourceId);
1631 assertEquals(1, stmt.executeUpdate());
1636 * Feature that uses <i>exsvc</i> to execute requests.
1638 private class MyLockingFeature extends DistributedLockManager {
1640 public MyLockingFeature(boolean init) {
1643 exsvc = mock(ScheduledExecutorService.class);
1644 lenient().when(exsvc.schedule(any(Runnable.class), anyLong(), any())).thenAnswer(ans -> checker);
1645 ReflectionTestUtils.setField(PolicyEngineConstants.getManager(), POLICY_ENGINE_EXECUTOR_FIELD, exsvc);
1648 beforeCreateLockManager();
1656 * Feature whose data source all throws exceptions.
1658 private class InvalidDbLockingFeature extends MyLockingFeature {
1659 private final boolean isTransient;
1660 private boolean freeLock = false;
1662 InvalidDbLockingFeature(boolean isTransient) {
1663 // pass "false" because we have to set the error code BEFORE calling
1667 this.isTransient = isTransient;
1669 this.beforeCreateLockManager();
1671 this.afterStart(engine);
1675 protected BasicDataSource makeDataSource() throws SQLException {
1676 lenient().when(dataSource.getConnection()).thenAnswer(answer -> {
1685 doThrow(makeEx()).when(dataSource).close();
1690 protected SQLException makeEx() {
1692 return new SQLException(new SQLTransientException(EXPECTED_EXCEPTION));
1695 return new SQLException(EXPECTED_EXCEPTION);
1701 * Feature whose locks free themselves while free() is already running.
1703 private class FreeWithFreeLockingFeature extends MyLockingFeature {
1704 private final boolean relock;
1706 public FreeWithFreeLockingFeature(boolean relock) {
1708 this.relock = relock;
1712 protected DistributedLock makeLock(LockState state, String resourceId, String ownerKey, int holdSec,
1713 LockCallback callback) {
1715 return new DistributedLock(state, resourceId, ownerKey, holdSec, callback, feature) {
1716 private static final long serialVersionUID = 1L;
1717 private boolean checked = false;
1720 public boolean isUnavailable() {
1722 return super.isUnavailable();
1727 // release and relock
1735 createLock(RESOURCE, getOwnerKey(), HOLD_SEC, mock(LockCallback.class), false);
1745 * Thread used with the multithreaded test. It repeatedly attempts to get a lock,
1746 * extend it, and then unlock it.
1748 private class MyThread extends Thread {
1749 AssertionError err = null;
1758 for (int x = 0; x < MAX_LOOPS; ++x) {
1762 } catch (AssertionError e) {
1767 private void makeAttempt() {
1769 Semaphore sem = new Semaphore(0);
1771 LockCallback cb = new LockCallback() {
1773 public void lockAvailable(Lock lock) {
1778 public void lockUnavailable(Lock lock) {
1783 Lock newLock = feature.createLock(RESOURCE, getName(), HOLD_SEC, cb, false);
1785 // wait for callback, whether available or unavailable
1786 assertTrue(sem.tryAcquire(5, TimeUnit.SECONDS));
1787 if (!newLock.isActive()) {
1791 numSuccess.incrementAndGet();
1793 assertEquals(1, numActive.incrementAndGet());
1795 newLock.extend(HOLD_SEC2, cb);
1796 assertTrue(sem.tryAcquire(5, TimeUnit.SECONDS));
1797 assertTrue(newLock.isActive());
1799 // decrement BEFORE free()
1800 numActive.decrementAndGet();
1802 assertTrue(newLock.free());
1803 assertTrue(newLock.isUnavailable());
1805 } catch (InterruptedException e) {
1806 Thread.currentThread().interrupt();
1807 throw new AssertionError("interrupted", e);