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.pessimistic;
26 import org.junit.Assert;
27 import org.junit.Test;
28 import org.onap.appc.lockmanager.api.LockException;
29 import org.onap.appc.lockmanager.api.LockRuntimeException;
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;
33 import org.onap.appc.lockmanager.impl.sql.pessimistic.MySqlLockManager;
35 import java.util.concurrent.*;
37 public class TestMySqlLockManager extends MySqlLockManagerBaseTests {
39 private static int CRITICAL_SECTION_WAIT_TIMEOUT = 1; // in secs
42 protected JdbcLockManager createJdbcLockManager(boolean useReal) {
43 return new MySqlLockManagerMock(useReal);
47 public void testConcurrentLock() throws LockException, InterruptedException, ExecutionException, TimeoutException {
49 callConcurrentTest(new Callable<Boolean>() {
51 public Boolean call() throws Exception {
53 Assert.assertTrue(lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name()));
55 } catch(LockRuntimeException e) {
56 Assert.assertEquals("Cannot obtain critical section lock for resource [" + Resource.Resource1.name() + "].", e.getMessage());
62 lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
67 public void testConcurrentUnlock() throws LockException, InterruptedException, ExecutionException, TimeoutException {
68 lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
69 callConcurrentTest(new Callable<Boolean>() {
71 public Boolean call() throws Exception {
73 lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
75 } catch(LockRuntimeException e) {
76 Assert.assertEquals("Cannot obtain critical section lock for resource [" + Resource.Resource1.name() + "].", e.getMessage());
83 private void callConcurrentTest(Callable<Boolean> callable) throws LockException, InterruptedException, ExecutionException, TimeoutException {
84 final int participantsNo = 2;
85 Synchronizer synchronizer = new Synchronizer(participantsNo) {
88 protected void waitForAllParticipants(Object waitObj, int totalParticipantsNo, int currentParticipantsNo) {
89 waitOn(this, TimeUnit.MILLISECONDS.convert(1 + CRITICAL_SECTION_WAIT_TIMEOUT, TimeUnit.SECONDS)); // add 1 sec to make sure timeout occured
92 if(!setSynchronizer(synchronizer)) {
95 ((MySqlLockManager)lockManager).setCriticalSectionWaitTimeoutSecs(CRITICAL_SECTION_WAIT_TIMEOUT);
96 ExecutorService executor = Executors.newFixedThreadPool(participantsNo);
97 Future<Boolean> future1 = executor.submit(callable);
99 for(int i = 0; i < 10; i++) {
101 if(synchronizer.getParticipantCount() > 0) {
105 // make sure 1st thread gets inside critical section
106 if(synchronizer.getParticipantCount() < 1) {
107 Assert.fail(getClass().getName() + " first thread failed to acquireLock()");
109 Future<Boolean> future2 = executor.submit(callable);
111 // 1st thread should acquire the lock
112 Assert.assertTrue(future1.get(3 + CRITICAL_SECTION_WAIT_TIMEOUT, TimeUnit.SECONDS));
113 // 2nd thread should fail waiting for critical section
114 Assert.assertFalse(future2.get(2 + CRITICAL_SECTION_WAIT_TIMEOUT, TimeUnit.SECONDS));
116 future2.cancel(true);
119 future1.cancel(true);
120 setSynchronizer(null);