Second part of onap rename
[appc.git] / appc-dispatcher / appc-dispatcher-common / lock-manager-lib / lock-manager-impl / src / test / java / org / onap / appc / lockmanager / impl / sql / pessimistic / TestMySqlLockManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.lockmanager.impl.sql.pessimistic;
26
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;
35
36 import java.util.concurrent.*;
37
38 public class TestMySqlLockManager extends MySqlLockManagerBaseTests {
39
40     private static int CRITICAL_SECTION_WAIT_TIMEOUT = 1; // in secs
41
42         @Override
43         protected JdbcLockManager createJdbcLockManager(boolean useReal) {
44                 return new MySqlLockManagerMock(useReal);
45         }
46
47         @Test
48         public void testConcurrentLock() throws LockException, InterruptedException, ExecutionException, TimeoutException {
49         try {
50             callConcurrentTest(new Callable<Boolean>() {
51                 @Override
52                 public Boolean call() throws Exception {
53                     try {
54                         Assert.assertTrue(lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name()));
55                         return true;
56                     } catch(LockRuntimeException e) {
57                         Assert.assertEquals("Cannot obtain critical section lock for resource [" + Resource.Resource1.name() + "].", e.getMessage());
58                         return false;
59                     }
60                 }
61             });
62         } finally {
63             lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
64                 }
65         }
66
67         @Test
68     public void testConcurrentUnlock() throws LockException, InterruptedException, ExecutionException, TimeoutException {
69         lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
70         callConcurrentTest(new Callable<Boolean>() {
71             @Override
72             public Boolean call() throws Exception {
73                 try {
74                     lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
75                     return true;
76                 } catch(LockRuntimeException e) {
77                     Assert.assertEquals("Cannot obtain critical section lock for resource [" + Resource.Resource1.name() + "].", e.getMessage());
78                     return false;
79                 }
80             }
81         });
82     }
83
84     private void callConcurrentTest(Callable<Boolean> callable) throws LockException, InterruptedException, ExecutionException, TimeoutException {
85         final int participantsNo = 2;
86         Synchronizer synchronizer = new Synchronizer(participantsNo) {
87
88             @Override
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
91             }
92         };
93         if(!setSynchronizer(synchronizer)) {
94             return;
95         }
96         ((MySqlLockManager)lockManager).setCriticalSectionWaitTimeoutSecs(CRITICAL_SECTION_WAIT_TIMEOUT);
97         ExecutorService executor = Executors.newFixedThreadPool(participantsNo);
98         Future<Boolean> future1 = executor.submit(callable);
99         try {
100             for(int i = 0; i < 10; i++) {
101                 Thread.sleep(100);
102                 if(synchronizer.getParticipantCount() > 0) {
103                     break;
104                 }
105             }
106             // make sure 1st thread gets inside critical section
107             if(synchronizer.getParticipantCount() < 1) {
108                 Assert.fail(getClass().getName() + " first thread failed to acquireLock()");
109             }
110             Future<Boolean> future2 = executor.submit(callable);
111             try {
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));
116             } finally {
117                 future2.cancel(true);
118             }
119         } finally {
120             future1.cancel(true);
121             setSynchronizer(null);
122         }
123     }
124 }