2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 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.drools.system.internal;
24 import static org.junit.jupiter.api.Assertions.assertFalse;
25 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.Mockito.doAnswer;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.never;
32 import static org.mockito.Mockito.spy;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.when;
36 import java.io.Serial;
37 import java.util.concurrent.ScheduledExecutorService;
38 import org.junit.jupiter.api.AfterEach;
39 import org.junit.jupiter.api.BeforeEach;
40 import org.junit.jupiter.api.Test;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 import org.onap.policy.drools.core.lock.AlwaysFailLock;
44 import org.onap.policy.drools.core.lock.Lock;
45 import org.onap.policy.drools.core.lock.LockCallback;
46 import org.onap.policy.drools.core.lock.LockState;
48 class LockManagerTest {
49 private static final String OWNER_KEY = "my key";
50 private static final String RESOURCE = "my resource";
51 private static final String RESOURCE2 = "my resource #2";
52 private static final int HOLD_SEC = 100;
55 private LockCallback callback;
58 private ScheduledExecutorService exsvc;
60 private MyManager mgr;
62 private AutoCloseable closeable;
65 * Resets fields and creates {@link #mgr}.
69 closeable = MockitoAnnotations.openMocks(this);
72 args.getArgument(0, Runnable.class).run();
74 }).when(exsvc).execute(any());
76 mgr = new MyManager();
80 public void tearDown() throws Exception {
86 assertFalse(mgr.isAlive());
87 assertFalse(mgr.isLocked());
90 assertTrue(mgr.isAlive());
91 assertFalse(mgr.isLocked());
94 assertFalse(mgr.isAlive());
99 assertTrue(mgr.start());
100 assertTrue(mgr.isAlive());
102 assertFalse(mgr.start());
103 assertTrue(mgr.isAlive());
106 assertTrue(mgr.start());
107 assertTrue(mgr.isAlive());
112 assertFalse(mgr.stop());
115 assertTrue(mgr.stop());
116 assertFalse(mgr.isAlive());
120 void testShutdown() {
123 assertFalse(mgr.isAlive());
126 assertFalse(mgr.isAlive());
130 void testIsLocked() {
131 assertFalse(mgr.isLocked());
132 assertFalse(mgr.isAlive());
135 assertTrue(mgr.isLocked());
136 assertFalse(mgr.isAlive());
139 assertFalse(mgr.isLocked());
144 assertTrue(mgr.lock());
145 assertTrue(mgr.isLocked());
147 assertFalse(mgr.lock());
148 assertTrue(mgr.isLocked());
151 assertTrue(mgr.lock());
152 assertTrue(mgr.isLocked());
157 assertFalse(mgr.unlock());
160 assertTrue(mgr.unlock());
161 assertFalse(mgr.isLocked());
165 void testCreateLock() {
166 Lock lock = mgr.createLock(RESOURCE, OWNER_KEY, HOLD_SEC, callback, false);
167 assertTrue(lock.isActive());
168 verify(callback).lockAvailable(lock);
169 verify(callback, never()).lockUnavailable(lock);
171 // should not be able to lock it again
172 LockCallback callback2 = mock(LockCallback.class);
173 Lock lock2 = mgr.createLock(RESOURCE, OWNER_KEY, HOLD_SEC, callback2, false);
174 assertTrue(lock2.isUnavailable());
175 verify(callback2, never()).lockAvailable(lock2);
176 verify(callback2).lockUnavailable(lock2);
178 // should be able to lock another resource
179 LockCallback callback3 = mock(LockCallback.class);
180 Lock lock3 = mgr.createLock(RESOURCE2, OWNER_KEY, HOLD_SEC, callback3, false);
181 assertTrue(lock3.isActive());
182 verify(callback3).lockAvailable(lock3);
183 verify(callback3, never()).lockUnavailable(lock3);
187 * Tests createLock() when the feature instance has changed.
190 void testCreateLockInstanceChanged() {
192 when(mgr.hasInstanceChanged()).thenReturn(true);
194 Lock lock = mgr.createLock(RESOURCE, OWNER_KEY, HOLD_SEC, callback, false);
195 assertInstanceOf(AlwaysFailLock.class, lock);
196 assertTrue(lock.isUnavailable());
198 verify(callback, never()).lockAvailable(lock);
199 verify(callback).lockUnavailable(lock);
203 void testGetResource2lock() {
204 assertNotNull(mgr.getResource2lock());
207 private class MyManager extends LockManager<MyLock> {
210 protected boolean hasInstanceChanged() {
215 protected void finishLock(MyLock lock) {
220 protected MyLock makeLock(LockState waiting, String resourceId, String ownerKey, int holdSec,
221 LockCallback callback) {
222 return new MyLock(waiting, resourceId, ownerKey, holdSec, callback);
227 private class MyLock extends FeatureLockImpl {
229 private static final long serialVersionUID = 1L;
231 public MyLock(LockState waiting, String resourceId, String ownerKey, int holdSec, LockCallback callback) {
232 super(waiting, resourceId, ownerKey, holdSec, callback);
236 public boolean free() {
241 public void extend(int holdSec, LockCallback callback) {
246 protected boolean addToFeature() {
251 public void notifyAvailable() {
252 getCallback().lockAvailable(this);
256 public void notifyUnavailable() {
257 getCallback().lockUnavailable(this);
261 protected ScheduledExecutorService getThreadPool() {