Fix 1 Checkstyle problem (red in Eclipse, missed in build)
[genius.git] / lockmanager / lockmanager-impl / src / test / java / org / opendaylight / lockmanager / tests / LockManagerTest.java
1 /*
2  * Copyright (c) 2017 Red Hat, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.lockmanager.tests;
9
10 import static com.google.common.truth.Truth.assertThat;
11
12 import java.util.Timer;
13 import java.util.TimerTask;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.Future;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.TimeoutException;
18
19 import org.junit.Before;
20 import org.junit.Rule;
21 import org.junit.Test;
22 import org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest;
23 import org.opendaylight.infrautils.testutils.LogRule;
24 import org.opendaylight.lockmanager.LockManager;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.LockInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.LockInputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.LockManagerService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.TimeUnits;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.TryLockInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.TryLockInputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.UnlockInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.UnlockInputBuilder;
33 import org.opendaylight.yangtools.yang.common.RpcResult;
34
35 /**
36  * Test for {@link LockManager}.
37  * @author Michael Vorburger.ch
38  */
39 public class LockManagerTest extends AbstractConcurrentDataBrokerTest {
40
41     public final @Rule LogRule logRule = new LogRule();
42     private LockManagerService lockManager;
43
44     @Before
45     public void setUp() {
46         lockManager = new LockManager(getDataBroker());
47     }
48
49     @Test
50     public void testLockAndUnLock() throws InterruptedException, ExecutionException, TimeoutException {
51         LockInput lockInput = new LockInputBuilder().setLockName("testLock").build();
52         assertSuccessfulFutureRpcResult(lockManager.lock(lockInput));
53
54         UnlockInput unlockInput = new UnlockInputBuilder().setLockName("testLock").build();
55         assertSuccessfulFutureRpcResult(lockManager.unlock(unlockInput));
56     }
57
58     @Test
59     public void testUnLockOfUnknownShouldNotFail() throws InterruptedException, ExecutionException, TimeoutException {
60         UnlockInput unlockInput = new UnlockInputBuilder().setLockName("unknownLock").build();
61         assertSuccessfulFutureRpcResult(lockManager.unlock(unlockInput));
62     }
63
64     @Test
65     // test re-lock of already locked key.
66     // lock() RPC will infinitely retry, and it will only come out when the key is unlocked
67     public void testLockAndReLockSameAgain() throws InterruptedException, ExecutionException, TimeoutException {
68         LockInput lockInput = new LockInputBuilder().setLockName("testLock").build();
69         assertSuccessfulFutureRpcResult(lockManager.lock(lockInput));
70         runUnlockTimerTask("testLock", 3000);
71
72         // This will retry infinitely since the other lock is not released!
73         // After 5 seconds, the parallel thread will unlock the key, and the below TC will pass
74         assertSuccessfulFutureRpcResult(lockManager.lock(lockInput));
75     }
76
77     @Test
78     // test re-lock of already locked key using tryLock() RPC.
79     // tryLock() RPC will retry only specific number of times, and it will only return after that
80     public void testTryLock() throws InterruptedException, ExecutionException, TimeoutException {
81         TryLockInput lockInput = new TryLockInputBuilder().setLockName("testTryLock").setTime(3L)
82             .setTimeUnit(TimeUnits.Seconds).build();
83         assertSuccessfulFutureRpcResult(lockManager.tryLock(lockInput));
84
85         // The second acquireLock request will retry for 3 seconds
86         // and since the first lock is not unlocked, the request will fail.
87         lockInput = new TryLockInputBuilder().setLockName("testTryLock").setTime(3000L)
88             .setTimeUnit(TimeUnits.Milliseconds).build();
89         assertFailedFutureRpcResult(lockManager.tryLock(lockInput));
90
91         // Try to unlock the key in a separate thread before retry expires, and see
92         // if lock gets acquired.
93         runUnlockTimerTask("testTryLock", 2000);
94
95         lockInput = new TryLockInputBuilder().setLockName("testTryLock").setTime(4000000L)
96             .setTimeUnit(TimeUnits.Microseconds).build();
97         assertSuccessfulFutureRpcResult(lockManager.tryLock(lockInput));
98     }
99
100
101     private void assertSuccessfulFutureRpcResult(Future<RpcResult<Void>> futureRpcResult)
102             throws InterruptedException, ExecutionException, TimeoutException {
103         assertThat(futureRpcResult.get(5, TimeUnit.SECONDS).isSuccessful()).isTrue();
104         assertThat(futureRpcResult.get(5, TimeUnit.SECONDS).getErrors()).isEmpty();
105     }
106
107     private void assertFailedFutureRpcResult(Future<RpcResult<Void>> futureRpcResult)
108             throws InterruptedException, ExecutionException, TimeoutException {
109         assertThat(futureRpcResult.get(5, TimeUnit.SECONDS).isSuccessful()).isFalse();
110     }
111
112     private void runUnlockTimerTask(String lockKey, long delay) {
113         Timer timer = new Timer();
114         timer.schedule(new TimerTask() {
115             @Override
116             public void run() {
117                 UnlockInput unlockInput = new UnlockInputBuilder().setLockName(lockKey).build();
118                 try {
119                     assertSuccessfulFutureRpcResult(lockManager.unlock(unlockInput));
120                 } catch (InterruptedException | ExecutionException | TimeoutException e) {
121                     throw new RuntimeException(e);
122                 }
123             }
124         }, delay);
125     }
126 }