2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.lockmanager.impl.sql.optimistic;
26 import org.junit.Assert;
27 import org.junit.Test;
28 import org.onap.appc.lockmanager.api.LockException;
29 import org.onap.appc.lockmanager.impl.sql.JdbcLockManager;
30 import org.onap.appc.lockmanager.impl.sql.MySqlLockManagerBaseTests;
31 import org.onap.appc.lockmanager.impl.sql.Synchronizer;
33 import java.util.concurrent.*;
35 public class TestMySqlLockManager extends MySqlLockManagerBaseTests {
38 protected JdbcLockManager createJdbcLockManager(boolean useReal) {
39 return new MySqlLockManagerMock(useReal);
43 public void testConcurrentLockDifferentOwners() throws LockException, InterruptedException, ExecutionException, TimeoutException {
45 final int participantsNo = 2;
46 Synchronizer synchronizer = new Synchronizer(participantsNo) {
48 private boolean wait = true;
51 public void preAddLockRecord(String resource, String owner) {
52 if(Owner.A.name().equals(owner)) {
62 public void postAddLockRecord(String resource, String owner) {
63 if(!Owner.A.name().equals(owner)) {
72 public void preUpdateLockRecord(String resource, String owner) {
73 preAddLockRecord(resource, owner);
77 public void postUpdateLockRecord(String resource, String owner) {
78 postAddLockRecord(resource, owner);
81 if(!setSynchronizer(synchronizer)) {
84 ExecutorService executor = Executors.newFixedThreadPool(participantsNo);
85 // acquireLock by owner A should fail as it will wait for acquireLock by owner B
86 Future<Boolean> future1 = executor.submit(new Callable<Boolean>() {
88 public Boolean call() throws Exception {
90 lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
92 } catch(LockException e) {
93 // this call should fail as Synchronizer delays its lock to make sure the second call locks the resource first
94 Assert.assertEquals("VNF : [" + Resource.Resource1.name() + "] is locked by request id : [" + Owner.B.name() + "]", e.getMessage());
100 // acquireLock by owner B should success
101 Future<Boolean> future2 = executor.submit(new Callable<Boolean>() {
103 public Boolean call() throws Exception {
104 // this call should success as Synchronizer delays the above lock to make sure this call success to lock the resource
105 return lockManager.acquireLock(Resource.Resource1.name(), Owner.B.name());
109 Assert.assertTrue(future2.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS));
110 Assert.assertTrue(future1.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS));
112 future2.cancel(true);
115 future1.cancel(true);
120 public void testConcurrentLockSameOwner() throws LockException, InterruptedException, ExecutionException, TimeoutException {
121 final int participantsNo = 2;
122 Synchronizer synchronizer = new Synchronizer(participantsNo) {
124 private boolean wait = true;
127 public void preAddLockRecord(String resource, String owner) {
137 public void postAddLockRecord(String resource, String owner) {
143 if(!setSynchronizer(synchronizer)) {
146 ExecutorService executor = Executors.newFixedThreadPool(participantsNo);
147 // one acquireLock should return true and the other should return false
148 Callable<Boolean> callable = new Callable<Boolean>() {
150 public Boolean call() throws Exception {
151 return lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
154 Future<Boolean> future1 = executor.submit(callable);
156 Future<Boolean> future2 = executor.submit(callable);
158 boolean future1Res = future1.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS);
159 boolean future2Res = future2.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS);
160 // one of the lock requests should return true, the other one false as lock is requested simultaneously from 2 threads by same owner
161 Assert.assertNotEquals(future1Res, future2Res);
163 future2.cancel(true);
166 future1.cancel(true);
171 public void testConcurrentUnlockSameOwner() throws LockException, InterruptedException, ExecutionException, TimeoutException {
172 lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
173 final int participantsNo = 2;
174 Synchronizer synchronizer = new Synchronizer(participantsNo) {
176 private boolean wait = true;
179 public void preUpdateLockRecord(String resource, String owner) {
181 // make sure second call updates the LockRecord first
190 public void postUpdateLockRecord(String resource, String owner) {
196 if(!setSynchronizer(synchronizer)) {
199 ExecutorService executor = Executors.newFixedThreadPool(participantsNo);
200 Callable<Boolean> callable = new Callable<Boolean>() {
202 public Boolean call() throws Exception {
204 lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
205 // one of the unlock calls should success
207 } catch(LockException e) {
208 // one of the unlock calls should throw the LockException as the resource should already be unlocked by other call
209 Assert.assertEquals("Error unlocking resource [" + Resource.Resource1.name() + "]: resource is not locked", e.getMessage());
214 Future<Boolean> future1 = executor.submit(callable);
216 Future<Boolean> future2 = executor.submit(callable);
218 boolean future1Res = future1.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS);
219 boolean future2Res = future2.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS);
220 // one of the unlock calls should return true, the other one false as unlock is requested simultaneously from 2 threads by same owner
221 Assert.assertNotEquals(future1Res, future2Res);
223 future2.cancel(true);
226 future1.cancel(true);