summaryrefslogtreecommitdiffstats
path: root/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/openecomp/appc/lockmanager/impl/sql/pessimistic/TestMySqlLockManager.java
blob: cf8781b133229e6206f516ffb92e25a2a840665e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : APP-C
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights
 * 						reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.appc.lockmanager.impl.sql.pessimistic;

import org.junit.Assert;
import org.junit.Test;
import org.openecomp.appc.lockmanager.api.LockException;
import org.openecomp.appc.lockmanager.api.LockRuntimeException;
import org.openecomp.appc.lockmanager.impl.sql.JdbcLockManager;
import org.openecomp.appc.lockmanager.impl.sql.MySqlLockManagerBaseTests;
import org.openecomp.appc.lockmanager.impl.sql.Synchronizer;
import org.openecomp.appc.lockmanager.impl.sql.pessimistic.MySqlLockManager;

import java.util.concurrent.*;

public class TestMySqlLockManager extends MySqlLockManagerBaseTests {

    private static int CRITICAL_SECTION_WAIT_TIMEOUT = 1; // in secs

	@Override
	protected JdbcLockManager createJdbcLockManager(boolean useReal) {
		return new MySqlLockManagerMock(useReal);
	}

	@Test
	public void testConcurrentLock() throws LockException, InterruptedException, ExecutionException, TimeoutException {
        try {
            callConcurrentTest(new Callable<Boolean>() {
                @Override
                public Boolean call() throws Exception {
                    try {
                        Assert.assertTrue(lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name()));
                        return true;
                    } catch(LockRuntimeException e) {
                        Assert.assertEquals("Cannot obtain critical section lock for resource [" + Resource.Resource1.name() + "].", e.getMessage());
                        return false;
                    }
                }
            });
        } finally {
            lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
		}
	}

	@Test
    public void testConcurrentUnlock() throws LockException, InterruptedException, ExecutionException, TimeoutException {
        lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
        callConcurrentTest(new Callable<Boolean>() {
            @Override
            public Boolean call() throws Exception {
                try {
                    lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
                    return true;
                } catch(LockRuntimeException e) {
                    Assert.assertEquals("Cannot obtain critical section lock for resource [" + Resource.Resource1.name() + "].", e.getMessage());
                    return false;
                }
            }
        });
    }

    private void callConcurrentTest(Callable<Boolean> callable) throws LockException, InterruptedException, ExecutionException, TimeoutException {
        final int participantsNo = 2;
        Synchronizer synchronizer = new Synchronizer(participantsNo) {

            @Override
            protected void waitForAllParticipants(Object waitObj, int totalParticipantsNo, int currentParticipantsNo) {
                waitOn(this, TimeUnit.MILLISECONDS.convert(1 + CRITICAL_SECTION_WAIT_TIMEOUT, TimeUnit.SECONDS)); // add 1 sec to make sure timeout occured
            }
        };
        if(!setSynchronizer(synchronizer)) {
            return;
        }
        ((MySqlLockManager)lockManager).setCriticalSectionWaitTimeoutSecs(CRITICAL_SECTION_WAIT_TIMEOUT);
        ExecutorService executor = Executors.newFixedThreadPool(participantsNo);
        Future<Boolean> future1 = executor.submit(callable);
        try {
            for(int i = 0; i < 10; i++) {
                Thread.sleep(100);
                if(synchronizer.getParticipantCount() > 0) {
                    break;
                }
            }
            // make sure 1st thread gets inside critical section
            if(synchronizer.getParticipantCount() < 1) {
                Assert.fail(getClass().getName() + " first thread failed to acquireLock()");
            }
            Future<Boolean> future2 = executor.submit(callable);
            try {
                // 1st thread should acquire the lock
                Assert.assertTrue(future1.get(3 + CRITICAL_SECTION_WAIT_TIMEOUT, TimeUnit.SECONDS));
                // 2nd thread should fail waiting for critical section
                Assert.assertFalse(future2.get(2 + CRITICAL_SECTION_WAIT_TIMEOUT, TimeUnit.SECONDS));
            } finally {
                future2.cancel(true);
            }
        } finally {
            future1.cancel(true);
            setSynchronizer(null);
        }
    }
}