2 * ============LICENSE_START=======================================================
3 * feature-distributed-locking
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.distributed.locking.test;
23 import org.junit.AfterClass;
24 import org.junit.Before;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.onap.policy.distributed.locking.DistributedLockingFeature;
28 import org.onap.policy.drools.persistence.SystemPersistence;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import static org.junit.Assert.assertFalse;
33 import static org.junit.Assert.assertTrue;
35 import java.sql.Connection;
36 import java.sql.DriverManager;
37 import java.sql.PreparedStatement;
38 import java.sql.SQLException;
39 import java.util.concurrent.CountDownLatch;
40 import java.util.concurrent.ExecutionException;
41 import java.util.concurrent.TimeUnit;
43 public class TargetLockTest {
44 private static final Logger logger = LoggerFactory.getLogger(TargetLockTest.class);
45 private static final String DB_CONNECTION = "jdbc:h2:mem:pooling;INIT=CREATE SCHEMA IF NOT EXISTS pooling\\;SET SCHEMA pooling";
46 private static final String DB_USER = "user";
47 private static final String DB_PASSWORD = "password";
48 private static Connection conn = null;
49 private static DistributedLockingFeature distLockFeat;
52 public static void setup() {
55 SystemPersistence.manager.setConfigurationDir("src/test/resources");
56 distLockFeat = new DistributedLockingFeature();
57 distLockFeat.afterStart(null);
62 public static void cleanUp() {
63 distLockFeat.beforeShutdown(null);
66 } catch (SQLException e) {
67 logger.error("Error in TargetLockTest.cleanUp()", e);
72 public void wipeDb() {
74 try (PreparedStatement lockDelete = conn.prepareStatement("DELETE FROM pooling.locks");){
75 lockDelete.executeUpdate();
76 } catch (SQLException e) {
77 logger.error("Error in TargetLockTest.wipeDb()", e);
78 throw new RuntimeException(e);
84 public void testGrabLockSuccess() throws InterruptedException, ExecutionException {
85 assertTrue(distLockFeat.beforeLock("resource1", "owner1", null).get());
87 //attempt to grab expiredLock
88 try (PreparedStatement updateStatement = conn.prepareStatement("UPDATE pooling.locks SET expirationTime = ? WHERE resourceId = ?");)
90 updateStatement.setLong(1, System.currentTimeMillis() - 1000);
91 updateStatement.setString(2, "resource1");
92 updateStatement.executeUpdate();
94 } catch (SQLException e) {
95 logger.error("Error in TargetLockTest.testGrabLockSuccess()", e);
96 throw new RuntimeException(e);
99 assertTrue(distLockFeat.beforeLock("resource1", "owner1", null).get());
103 public void testExpiredLocks() throws InterruptedException, ExecutionException {
104 CountDownLatch latch = new CountDownLatch(1);
106 distLockFeat.beforeLock("resource1", "owner1", null);
109 latch.await(1000, TimeUnit.MILLISECONDS);
110 } catch (InterruptedException e) {
111 logger.error("Error in testExpiredLocks", e);
114 //Heartbeat should keep it active
115 assertFalse(distLockFeat.beforeLock("resource1", "owner1", null).get());
119 public void testGrabLockFail() throws InterruptedException, ExecutionException {
120 CountDownLatch latch = new CountDownLatch(1);
122 distLockFeat.beforeLock("resource1", "owner1", null);
125 latch.await(10, TimeUnit.MILLISECONDS);
126 } catch (InterruptedException e) {
127 logger.error("Error in testExpiredLocks", e);
129 assertFalse(distLockFeat.beforeLock("resource1", "owner1", null).get());
135 public void testUnlock() throws InterruptedException, ExecutionException {
136 distLockFeat.beforeLock("resource1", "owner1", null);
138 assertTrue(distLockFeat.beforeUnlock("resource1", "owner1"));
139 assertTrue(distLockFeat.beforeLock("resource1", "owner1", null).get());
143 public void testIsActive() {
144 assertFalse(distLockFeat.beforeIsLockedBy("resource1", "owner1"));
145 distLockFeat.beforeLock("resource1", "owner1", null);
146 assertTrue(distLockFeat.beforeIsLockedBy("resource1", "owner1"));
147 assertFalse(distLockFeat.beforeIsLockedBy("resource1", "owner2"));
149 // isActive on expiredLock
150 try (PreparedStatement updateStatement = conn
151 .prepareStatement("UPDATE pooling.locks SET expirationTime = ? WHERE resourceId = ?");) {
152 updateStatement.setLong(1, System.currentTimeMillis() - 5000);
153 updateStatement.setString(2, "resource1");
154 updateStatement.executeUpdate();
156 } catch (SQLException e) {
157 logger.error("Error in TargetLockTest.testIsActive()", e);
158 throw new RuntimeException(e);
161 assertFalse(distLockFeat.beforeIsLockedBy("resource1", "owner1"));
163 distLockFeat.beforeLock("resource1", "owner1", null);
164 //Unlock record, next isActive attempt should fail
165 distLockFeat.beforeUnlock("resource1", "owner1");
166 assertFalse(distLockFeat.beforeIsLockedBy("resource1", "owner1"));
171 public void testHeartbeat() {
172 CountDownLatch latch = new CountDownLatch(1);
174 distLockFeat.beforeLock("resource1", "owner1", null);
176 latch.await(1000, TimeUnit.MILLISECONDS);
177 } catch (InterruptedException e) {
178 logger.error("Error in testExpiredLocks", e);
181 // This test always returns true.
182 assertTrue(distLockFeat.beforeIsLocked("resource1"));
186 public void unlockBeforeLock() {
187 assertFalse(distLockFeat.beforeUnlock("resource1", "owner1"));
188 distLockFeat.beforeLock("resource1", "owner1", null);
189 assertTrue(distLockFeat.beforeUnlock("resource1", "owner1"));
190 assertFalse(distLockFeat.beforeUnlock("resource1", "owner1"));
194 public void testIsLocked() {
195 assertFalse(distLockFeat.beforeIsLocked("resource1"));
196 distLockFeat.beforeLock("resource1", "owner1", null);
197 assertTrue(distLockFeat.beforeIsLocked("resource1"));
201 private static void getDBConnection() {
203 conn = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
204 } catch (SQLException e) {
205 logger.error("Error in TargetLockTest.getDBConnection()", e);
209 private static void createTable() {
210 String createString = "create table if not exists pooling.locks (resourceId VARCHAR(128), host VARCHAR(128), owner VARCHAR(128), expirationTime BIGINT, PRIMARY KEY (resourceId))";
211 try (PreparedStatement createStmt = conn.prepareStatement(createString);) {
212 createStmt.executeUpdate();
214 } catch (SQLException e) {
215 logger.error("Error in TargetLockTest.createTable()", e);