2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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.openecomp.appc.lockmanager.impl.sql.optimistic;
24 import org.junit.Assert;
25 import org.junit.Test;
26 import org.openecomp.appc.lockmanager.api.LockException;
27 import org.openecomp.appc.lockmanager.impl.sql.JdbcLockManager;
28 import org.openecomp.appc.lockmanager.impl.sql.MySqlLockManagerBaseTests;
29 import org.openecomp.appc.lockmanager.impl.sql.Synchronizer;
31 import java.util.concurrent.*;
33 public class TestMySqlLockManager extends MySqlLockManagerBaseTests {
36 protected JdbcLockManager createJdbcLockManager(boolean useReal) {
37 return new MySqlLockManagerMock(useReal);
41 public void testConcurrentLockDifferentOwners() throws LockException, InterruptedException, ExecutionException, TimeoutException {
43 final int participantsNo = 2;
44 Synchronizer synchronizer = new Synchronizer(participantsNo) {
46 private boolean wait = true;
49 public void preAddLockRecord(String resource, String owner) {
50 if(Owner.A.name().equals(owner)) {
60 public void postAddLockRecord(String resource, String owner) {
61 if(!Owner.A.name().equals(owner)) {
70 public void preUpdateLockRecord(String resource, String owner) {
71 preAddLockRecord(resource, owner);
75 public void postUpdateLockRecord(String resource, String owner) {
76 postAddLockRecord(resource, owner);
79 if(!setSynchronizer(synchronizer)) {
82 ExecutorService executor = Executors.newFixedThreadPool(participantsNo);
83 // acquireLock by owner A should fail as it will wait for acquireLock by owner B
84 Future<Boolean> future1 = executor.submit(new Callable<Boolean>() {
86 public Boolean call() throws Exception {
88 lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
90 } catch(LockException e) {
91 // this call should fail as Synchronizer delays its lock to make sure the second call locks the resource first
92 Assert.assertEquals("Cannot lock resource [" + Resource.Resource1.name() + "] for [" + Owner.A.name() + "]: already locked by [" + Owner.B.name() + "]", e.getMessage());
98 // acquireLock by owner B should success
99 Future<Boolean> future2 = executor.submit(new Callable<Boolean>() {
101 public Boolean call() throws Exception {
102 // this call should success as Synchronizer delays the above lock to make sure this call success to lock the resource
103 return lockManager.acquireLock(Resource.Resource1.name(), Owner.B.name());
107 Assert.assertTrue(future2.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS));
108 Assert.assertTrue(future1.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS));
110 future2.cancel(true);
113 future1.cancel(true);
118 public void testConcurrentLockSameOwner() throws LockException, InterruptedException, ExecutionException, TimeoutException {
119 final int participantsNo = 2;
120 Synchronizer synchronizer = new Synchronizer(participantsNo) {
122 private boolean wait = true;
125 public void preAddLockRecord(String resource, String owner) {
135 public void postAddLockRecord(String resource, String owner) {
141 if(!setSynchronizer(synchronizer)) {
144 ExecutorService executor = Executors.newFixedThreadPool(participantsNo);
145 // one acquireLock should return true and the other should return false
146 Callable<Boolean> callable = new Callable<Boolean>() {
148 public Boolean call() throws Exception {
149 return lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
152 Future<Boolean> future1 = executor.submit(callable);
154 Future<Boolean> future2 = executor.submit(callable);
156 boolean future1Res = future1.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS);
157 boolean future2Res = future2.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS);
158 // one of the lock requests should return true, the other one false as lock is requested simultaneously from 2 threads by same owner
159 Assert.assertNotEquals(future1Res, future2Res);
161 future2.cancel(true);
164 future1.cancel(true);
169 public void testConcurrentUnlockSameOwner() throws LockException, InterruptedException, ExecutionException, TimeoutException {
170 lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
171 final int participantsNo = 2;
172 Synchronizer synchronizer = new Synchronizer(participantsNo) {
174 private boolean wait = true;
177 public void preUpdateLockRecord(String resource, String owner) {
179 // make sure second call updates the LockRecord first
188 public void postUpdateLockRecord(String resource, String owner) {
194 if(!setSynchronizer(synchronizer)) {
197 ExecutorService executor = Executors.newFixedThreadPool(participantsNo);
198 Callable<Boolean> callable = new Callable<Boolean>() {
200 public Boolean call() throws Exception {
202 lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
203 // one of the unlock calls should success
205 } catch(LockException e) {
206 // one of the unlock calls should throw the LockException as the resource should already be unlocked by other call
207 Assert.assertEquals("Error unlocking resource [" + Resource.Resource1.name() + "]: resource is not locked", e.getMessage());
212 Future<Boolean> future1 = executor.submit(callable);
214 Future<Boolean> future2 = executor.submit(callable);
216 boolean future1Res = future1.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS);
217 boolean future2Res = future2.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS);
218 // one of the unlock calls should return true, the other one false as unlock is requested simultaneously from 2 threads by same owner
219 Assert.assertNotEquals(future1Res, future2Res);
221 future2.cancel(true);
224 future1.cancel(true);