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.pessimistic;
27 import org.junit.Assert;
28 import org.junit.Test;
29 import org.onap.appc.lockmanager.api.LockException;
30 import org.onap.appc.lockmanager.api.LockRuntimeException;
31 import org.onap.appc.lockmanager.impl.sql.JdbcLockManager;
32 import org.onap.appc.lockmanager.impl.sql.MySqlLockManagerBaseTests;
33 import org.onap.appc.lockmanager.impl.sql.Synchronizer;
34 import org.onap.appc.lockmanager.impl.sql.pessimistic.MySqlLockManager;
36 import java.util.concurrent.*;
38 public class TestMySqlLockManager extends MySqlLockManagerBaseTests {
40 private static int CRITICAL_SECTION_WAIT_TIMEOUT = 1; // in secs
43 protected JdbcLockManager createJdbcLockManager(boolean useReal) {
44 return new MySqlLockManagerMock(useReal);
48 public void testConcurrentLock() throws LockException, InterruptedException, ExecutionException, TimeoutException {
50 callConcurrentTest(new Callable<Boolean>() {
52 public Boolean call() throws Exception {
54 Assert.assertTrue(lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name()));
56 } catch(LockRuntimeException e) {
57 Assert.assertEquals("Cannot obtain critical section lock for resource [" + Resource.Resource1.name() + "].", e.getMessage());
63 lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
68 public void testConcurrentUnlock() throws LockException, InterruptedException, ExecutionException, TimeoutException {
69 lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
70 callConcurrentTest(new Callable<Boolean>() {
72 public Boolean call() throws Exception {
74 lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
76 } catch(LockRuntimeException e) {
77 Assert.assertEquals("Cannot obtain critical section lock for resource [" + Resource.Resource1.name() + "].", e.getMessage());
84 private void callConcurrentTest(Callable<Boolean> callable) throws LockException, InterruptedException, ExecutionException, TimeoutException {
85 final int participantsNo = 2;
86 Synchronizer synchronizer = new Synchronizer(participantsNo) {
89 protected void waitForAllParticipants(Object waitObj, int totalParticipantsNo, int currentParticipantsNo) {
90 waitOn(this, TimeUnit.MILLISECONDS.convert(1 + CRITICAL_SECTION_WAIT_TIMEOUT, TimeUnit.SECONDS)); // add 1 sec to make sure timeout occured
93 if(!setSynchronizer(synchronizer)) {
96 ((MySqlLockManager)lockManager).setCriticalSectionWaitTimeoutSecs(CRITICAL_SECTION_WAIT_TIMEOUT);
97 ExecutorService executor = Executors.newFixedThreadPool(participantsNo);
98 Future<Boolean> future1 = executor.submit(callable);
100 for(int i = 0; i < 10; i++) {
102 if(synchronizer.getParticipantCount() > 0) {
106 // make sure 1st thread gets inside critical section
107 if(synchronizer.getParticipantCount() < 1) {
108 Assert.fail(getClass().getName() + " first thread failed to acquireLock()");
110 Future<Boolean> future2 = executor.submit(callable);
112 // 1st thread should acquire the lock
113 Assert.assertTrue(future1.get(3 + CRITICAL_SECTION_WAIT_TIMEOUT, TimeUnit.SECONDS));
114 // 2nd thread should fail waiting for critical section
115 Assert.assertFalse(future2.get(2 + CRITICAL_SECTION_WAIT_TIMEOUT, TimeUnit.SECONDS));
117 future2.cancel(true);
120 future1.cancel(true);
121 setSynchronizer(null);