2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.lockmanager.impl.sql.optimistic;
27 import org.junit.Assert;
28 import org.junit.Test;
29 import org.onap.appc.lockmanager.api.LockException;
30 import org.onap.appc.lockmanager.impl.sql.JdbcLockManager;
31 import org.onap.appc.lockmanager.impl.sql.MySqlLockManagerBaseTests;
32 import org.onap.appc.lockmanager.impl.sql.Synchronizer;
34 import java.util.concurrent.*;
36 public class TestMySqlLockManager extends MySqlLockManagerBaseTests {
39 protected JdbcLockManager createJdbcLockManager(boolean useReal) {
40 return new MySqlLockManagerMock(useReal);
44 public void testConcurrentLockDifferentOwners() throws LockException, InterruptedException, ExecutionException, TimeoutException {
46 final int participantsNo = 2;
47 Synchronizer synchronizer = new Synchronizer(participantsNo) {
49 private boolean wait = true;
52 public void preAddLockRecord(String resource, String owner) {
53 if(Owner.A.name().equals(owner)) {
63 public void postAddLockRecord(String resource, String owner) {
64 if(!Owner.A.name().equals(owner)) {
73 public void preUpdateLockRecord(String resource, String owner) {
74 preAddLockRecord(resource, owner);
78 public void postUpdateLockRecord(String resource, String owner) {
79 postAddLockRecord(resource, owner);
82 if(!setSynchronizer(synchronizer)) {
85 ExecutorService executor = Executors.newFixedThreadPool(participantsNo);
86 // acquireLock by owner A should fail as it will wait for acquireLock by owner B
87 Future<Boolean> future1 = executor.submit(new Callable<Boolean>() {
89 public Boolean call() throws Exception {
91 lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
93 } catch(LockException e) {
94 // this call should fail as Synchronizer delays its lock to make sure the second call locks the resource first
95 Assert.assertEquals("Cannot lock resource [" + Resource.Resource1.name() + "] for [" + Owner.A.name() + "]: already locked by [" + Owner.B.name() + "]", e.getMessage());
101 // acquireLock by owner B should success
102 Future<Boolean> future2 = executor.submit(new Callable<Boolean>() {
104 public Boolean call() throws Exception {
105 // this call should success as Synchronizer delays the above lock to make sure this call success to lock the resource
106 return lockManager.acquireLock(Resource.Resource1.name(), Owner.B.name());
110 Assert.assertTrue(future2.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS));
111 Assert.assertTrue(future1.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS));
113 future2.cancel(true);
116 future1.cancel(true);
121 public void testConcurrentLockSameOwner() throws LockException, InterruptedException, ExecutionException, TimeoutException {
122 final int participantsNo = 2;
123 Synchronizer synchronizer = new Synchronizer(participantsNo) {
125 private boolean wait = true;
128 public void preAddLockRecord(String resource, String owner) {
138 public void postAddLockRecord(String resource, String owner) {
144 if(!setSynchronizer(synchronizer)) {
147 ExecutorService executor = Executors.newFixedThreadPool(participantsNo);
148 // one acquireLock should return true and the other should return false
149 Callable<Boolean> callable = new Callable<Boolean>() {
151 public Boolean call() throws Exception {
152 return lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
155 Future<Boolean> future1 = executor.submit(callable);
157 Future<Boolean> future2 = executor.submit(callable);
159 boolean future1Res = future1.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS);
160 boolean future2Res = future2.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS);
161 // one of the lock requests should return true, the other one false as lock is requested simultaneously from 2 threads by same owner
162 Assert.assertNotEquals(future1Res, future2Res);
164 future2.cancel(true);
167 future1.cancel(true);
172 public void testConcurrentUnlockSameOwner() throws LockException, InterruptedException, ExecutionException, TimeoutException {
173 lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
174 final int participantsNo = 2;
175 Synchronizer synchronizer = new Synchronizer(participantsNo) {
177 private boolean wait = true;
180 public void preUpdateLockRecord(String resource, String owner) {
182 // make sure second call updates the LockRecord first
191 public void postUpdateLockRecord(String resource, String owner) {
197 if(!setSynchronizer(synchronizer)) {
200 ExecutorService executor = Executors.newFixedThreadPool(participantsNo);
201 Callable<Boolean> callable = new Callable<Boolean>() {
203 public Boolean call() throws Exception {
205 lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
206 // one of the unlock calls should success
208 } catch(LockException e) {
209 // one of the unlock calls should throw the LockException as the resource should already be unlocked by other call
210 Assert.assertEquals("Error unlocking resource [" + Resource.Resource1.name() + "]: resource is not locked", e.getMessage());
215 Future<Boolean> future1 = executor.submit(callable);
217 Future<Boolean> future2 = executor.submit(callable);
219 boolean future1Res = future1.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS);
220 boolean future2Res = future2.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS);
221 // one of the unlock calls should return true, the other one false as unlock is requested simultaneously from 2 threads by same owner
222 Assert.assertNotEquals(future1Res, future2Res);
224 future2.cancel(true);
227 future1.cancel(true);