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.genius.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 javax.inject.Inject;
21 import org.junit.Rule;
22 import org.junit.Test;
23 import org.junit.rules.MethodRule;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest;
26 import org.opendaylight.genius.lockmanager.LockManager;
27 import org.opendaylight.infrautils.inject.guice.testutils.GuiceRule;
28 import org.opendaylight.infrautils.testutils.LogCaptureRule;
29 import org.opendaylight.infrautils.testutils.LogRule;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.LockInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.LockInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.LockManagerService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.TimeUnits;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.TryLockInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.TryLockInputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.UnlockInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.UnlockInputBuilder;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
41 * Test for {@link LockManager}.
42 * @author Michael Vorburger.ch
44 public class LockManagerTest extends AbstractConcurrentDataBrokerTest {
46 public @Rule LogRule logRule = new LogRule();
47 public @Rule LogCaptureRule logCaptureRule = new LogCaptureRule();
48 public @Rule MethodRule guice = new GuiceRule(new LockManagerTestModule());
50 @Inject DataBroker dataBroker;
51 @Inject LockManagerService lockManager;
54 public void testLockAndUnLock() throws InterruptedException, ExecutionException, TimeoutException {
55 LockInput lockInput = new LockInputBuilder().setLockName("testLock").build();
56 assertSuccessfulFutureRpcResult(lockManager.lock(lockInput));
58 UnlockInput unlockInput = new UnlockInputBuilder().setLockName("testLock").build();
59 assertSuccessfulFutureRpcResult(lockManager.unlock(unlockInput));
63 public void testUnLockOfUnknownShouldNotFail() throws InterruptedException, ExecutionException, TimeoutException {
64 UnlockInput unlockInput = new UnlockInputBuilder().setLockName("unknownLock").build();
65 assertSuccessfulFutureRpcResult(lockManager.unlock(unlockInput));
69 // test re-lock of already locked key.
70 // lock() RPC will infinitely retry, and it will only come out when the key is unlocked
71 public void testLockAndReLockSameAgain() throws InterruptedException, ExecutionException, TimeoutException {
72 LockInput lockInput = new LockInputBuilder().setLockName("testLock").build();
73 assertSuccessfulFutureRpcResult(lockManager.lock(lockInput));
74 runUnlockTimerTask("testLock", 3000);
76 // This will retry infinitely since the other lock is not released!
77 // After 5 seconds, the parallel thread will unlock the key, and the below TC will pass
78 assertSuccessfulFutureRpcResult(lockManager.lock(lockInput));
82 // test re-lock of already locked key using tryLock() RPC.
83 // tryLock() RPC will retry only specific number of times, and it will only return after that
84 public void testTryLock() throws InterruptedException, ExecutionException, TimeoutException {
85 logCaptureRule.expectError("Failed to get lock testTryLock");
87 TryLockInput lockInput = new TryLockInputBuilder().setLockName("testTryLock").setTime(3L)
88 .setTimeUnit(TimeUnits.Seconds).build();
89 assertSuccessfulFutureRpcResult(lockManager.tryLock(lockInput));
91 // The second acquireLock request will retry for 3 seconds
92 // and since the first lock is not unlocked, the request will fail.
93 lockInput = new TryLockInputBuilder().setLockName("testTryLock").setTime(3000L)
94 .setTimeUnit(TimeUnits.Milliseconds).build();
95 assertFailedFutureRpcResult(lockManager.tryLock(lockInput));
97 // Try to unlock the key in a separate thread before retry expires, and see
98 // if lock gets acquired.
99 runUnlockTimerTask("testTryLock", 2000);
101 lockInput = new TryLockInputBuilder().setLockName("testTryLock").setTime(4000000L)
102 .setTimeUnit(TimeUnits.Microseconds).build();
103 assertSuccessfulFutureRpcResult(lockManager.tryLock(lockInput));
107 private void assertSuccessfulFutureRpcResult(Future<RpcResult<Void>> futureRpcResult)
108 throws InterruptedException, ExecutionException, TimeoutException {
109 assertThat(futureRpcResult.get(5, TimeUnit.SECONDS).isSuccessful()).isTrue();
110 assertThat(futureRpcResult.get(5, TimeUnit.SECONDS).getErrors()).isEmpty();
113 private void assertFailedFutureRpcResult(Future<RpcResult<Void>> futureRpcResult)
114 throws InterruptedException, ExecutionException, TimeoutException {
115 assertThat(futureRpcResult.get(5, TimeUnit.SECONDS).isSuccessful()).isFalse();
118 private void runUnlockTimerTask(String lockKey, long delay) {
119 Timer timer = new Timer();
120 timer.schedule(new TimerTask() {
123 UnlockInput unlockInput = new UnlockInputBuilder().setLockName(lockKey).build();
125 assertSuccessfulFutureRpcResult(lockManager.unlock(unlockInput));
126 } catch (InterruptedException | ExecutionException | TimeoutException e) {
127 throw new RuntimeException(e);