2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.lockmanager.impl.sql.optimistic;
25 import org.junit.Assert;
26 import org.junit.Test;
27 import org.openecomp.appc.lockmanager.api.LockException;
28 import org.openecomp.appc.lockmanager.impl.sql.JdbcLockManager;
29 import org.openecomp.appc.lockmanager.impl.sql.MySqlLockManagerBaseTests;
30 import org.openecomp.appc.lockmanager.impl.sql.Synchronizer;
32 import java.util.concurrent.*;
34 public class TestMySqlLockManager extends MySqlLockManagerBaseTests {
37 protected JdbcLockManager createJdbcLockManager(boolean useReal) {
38 return new MySqlLockManagerMock(useReal);
42 public void testConcurrentLockDifferentOwners() throws LockException, InterruptedException, ExecutionException, TimeoutException {
44 final int participantsNo = 2;
45 Synchronizer synchronizer = new Synchronizer(participantsNo) {
47 private boolean wait = true;
50 public void preAddLockRecord(String resource, String owner) {
51 if(Owner.A.name().equals(owner)) {
61 public void postAddLockRecord(String resource, String owner) {
62 if(!Owner.A.name().equals(owner)) {
71 public void preUpdateLockRecord(String resource, String owner) {
72 preAddLockRecord(resource, owner);
76 public void postUpdateLockRecord(String resource, String owner) {
77 postAddLockRecord(resource, owner);
80 if(!setSynchronizer(synchronizer)) {
83 ExecutorService executor = Executors.newFixedThreadPool(participantsNo);
84 // acquireLock by owner A should fail as it will wait for acquireLock by owner B
85 Future<Boolean> future1 = executor.submit(new Callable<Boolean>() {
87 public Boolean call() throws Exception {
89 lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
91 } catch(LockException e) {
92 // this call should fail as Synchronizer delays its lock to make sure the second call locks the resource first
93 Assert.assertEquals("Cannot lock resource [" + Resource.Resource1.name() + "] for [" + Owner.A.name() + "]: already locked by [" + Owner.B.name() + "]", e.getMessage());
99 // acquireLock by owner B should success
100 Future<Boolean> future2 = executor.submit(new Callable<Boolean>() {
102 public Boolean call() throws Exception {
103 // this call should success as Synchronizer delays the above lock to make sure this call success to lock the resource
104 return lockManager.acquireLock(Resource.Resource1.name(), Owner.B.name());
108 Assert.assertTrue(future2.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS));
109 Assert.assertTrue(future1.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS));
111 future2.cancel(true);
114 future1.cancel(true);
119 public void testConcurrentLockSameOwner() throws LockException, InterruptedException, ExecutionException, TimeoutException {
120 final int participantsNo = 2;
121 Synchronizer synchronizer = new Synchronizer(participantsNo) {
123 private boolean wait = true;
126 public void preAddLockRecord(String resource, String owner) {
136 public void postAddLockRecord(String resource, String owner) {
142 if(!setSynchronizer(synchronizer)) {
145 ExecutorService executor = Executors.newFixedThreadPool(participantsNo);
146 // one acquireLock should return true and the other should return false
147 Callable<Boolean> callable = new Callable<Boolean>() {
149 public Boolean call() throws Exception {
150 return lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
153 Future<Boolean> future1 = executor.submit(callable);
155 Future<Boolean> future2 = executor.submit(callable);
157 boolean future1Res = future1.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS);
158 boolean future2Res = future2.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS);
159 // one of the lock requests should return true, the other one false as lock is requested simultaneously from 2 threads by same owner
160 Assert.assertNotEquals(future1Res, future2Res);
162 future2.cancel(true);
165 future1.cancel(true);
170 public void testConcurrentUnlockSameOwner() throws LockException, InterruptedException, ExecutionException, TimeoutException {
171 lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
172 final int participantsNo = 2;
173 Synchronizer synchronizer = new Synchronizer(participantsNo) {
175 private boolean wait = true;
178 public void preUpdateLockRecord(String resource, String owner) {
180 // make sure second call updates the LockRecord first
189 public void postUpdateLockRecord(String resource, String owner) {
195 if(!setSynchronizer(synchronizer)) {
198 ExecutorService executor = Executors.newFixedThreadPool(participantsNo);
199 Callable<Boolean> callable = new Callable<Boolean>() {
201 public Boolean call() throws Exception {
203 lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
204 // one of the unlock calls should success
206 } catch(LockException e) {
207 // one of the unlock calls should throw the LockException as the resource should already be unlocked by other call
208 Assert.assertEquals("Error unlocking resource [" + Resource.Resource1.name() + "]: resource is not locked", e.getMessage());
213 Future<Boolean> future1 = executor.submit(callable);
215 Future<Boolean> future2 = executor.submit(callable);
217 boolean future1Res = future1.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS);
218 boolean future2Res = future2.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS);
219 // one of the unlock calls should return true, the other one false as unlock is requested simultaneously from 2 threads by same owner
220 Assert.assertNotEquals(future1Res, future2Res);
222 future2.cancel(true);
225 future1.cancel(true);