2 * Copyright (c) 2017 Red Hat, Inc. and others. All rights reserved.
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
8 package org.opendaylight.lockmanager.tests;
10 import static com.google.common.truth.Truth.assertThat;
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;
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;
36 * Test for {@link LockManager}.
37 * @author Michael Vorburger.ch
39 public class LockManagerTest extends AbstractConcurrentDataBrokerTest {
41 public final @Rule LogRule logRule = new LogRule();
42 private LockManagerService lockManager;
46 lockManager = new LockManager(getDataBroker());
50 public void testLockAndUnLock() throws InterruptedException, ExecutionException, TimeoutException {
51 LockInput lockInput = new LockInputBuilder().setLockName("testLock").build();
52 assertSuccessfulFutureRpcResult(lockManager.lock(lockInput));
54 UnlockInput unlockInput = new UnlockInputBuilder().setLockName("testLock").build();
55 assertSuccessfulFutureRpcResult(lockManager.unlock(unlockInput));
59 public void testUnLockOfUnknownShouldNotFail() throws InterruptedException, ExecutionException, TimeoutException {
60 UnlockInput unlockInput = new UnlockInputBuilder().setLockName("unknownLock").build();
61 assertSuccessfulFutureRpcResult(lockManager.unlock(unlockInput));
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);
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));
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));
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));
91 // Try to unlock the key in a separate thread before retry expires, and see
92 // if lock gets acquired.
93 runUnlockTimerTask("testTryLock", 2000);
95 lockInput = new TryLockInputBuilder().setLockName("testTryLock").setTime(4000000L)
96 .setTimeUnit(TimeUnits.Microseconds).build();
97 assertSuccessfulFutureRpcResult(lockManager.tryLock(lockInput));
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();
107 private void assertFailedFutureRpcResult(Future<RpcResult<Void>> futureRpcResult)
108 throws InterruptedException, ExecutionException, TimeoutException {
109 assertThat(futureRpcResult.get(5, TimeUnit.SECONDS).isSuccessful()).isFalse();
112 private void runUnlockTimerTask(String lockKey, long delay) {
113 Timer timer = new Timer();
114 timer.schedule(new TimerTask() {
117 UnlockInput unlockInput = new UnlockInputBuilder().setLockName(lockKey).build();
119 assertSuccessfulFutureRpcResult(lockManager.unlock(unlockInput));
120 } catch (InterruptedException | ExecutionException | TimeoutException e) {
121 throw new RuntimeException(e);