Rename class LockManager to LockManagerServiceImpl and move package
[genius.git] / lockmanager / lockmanager-impl / src / test / java / org / opendaylight / genius / lockmanager / tests / LockManagerTest.java
index ceef5e3d7b6e8483b405f72640d763a42594a21b..abf97a9ed944dc2a9016c16f34221deea5e1f722 100644 (file)
@@ -23,8 +23,12 @@ import org.junit.Test;
 import org.junit.rules.MethodRule;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest;
-import org.opendaylight.genius.lockmanager.LockManager;
+import org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException;
+import org.opendaylight.genius.datastoreutils.testutils.DataBrokerFailures;
+import org.opendaylight.genius.datastoreutils.testutils.DataBrokerFailuresModule;
+import org.opendaylight.genius.lockmanager.impl.LockManagerServiceImpl;
 import org.opendaylight.infrautils.inject.guice.testutils.GuiceRule;
+import org.opendaylight.infrautils.testutils.LogCaptureRule;
 import org.opendaylight.infrautils.testutils.LogRule;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.LockInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.LockInputBuilder;
@@ -35,17 +39,23 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev16041
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.UnlockInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.UnlockInputBuilder;
 import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
- * Test for {@link LockManager}.
+ * Test for {@link LockManagerServiceImpl}.
  * @author Michael Vorburger.ch
  */
 public class LockManagerTest extends AbstractConcurrentDataBrokerTest {
 
+    private static final Logger LOG = LoggerFactory.getLogger(LockManagerTest.class);
+
     public @Rule LogRule logRule = new LogRule();
-    public @Rule MethodRule guice = new GuiceRule(new LockManagerTestModule());
+    public @Rule LogCaptureRule logCaptureRule = new LogCaptureRule();
+    public @Rule MethodRule guice = new GuiceRule(LockManagerTestModule.class, DataBrokerFailuresModule.class);
 
     @Inject DataBroker dataBroker;
+    @Inject DataBrokerFailures dbFailureSimulator;
     @Inject LockManagerService lockManager;
 
     @Test
@@ -80,6 +90,8 @@ public class LockManagerTest extends AbstractConcurrentDataBrokerTest {
     // test re-lock of already locked key using tryLock() RPC.
     // tryLock() RPC will retry only specific number of times, and it will only return after that
     public void testTryLock() throws InterruptedException, ExecutionException, TimeoutException {
+        logCaptureRule.expectError("Failed to get lock testTryLock after 3 retries");
+
         TryLockInput lockInput = new TryLockInputBuilder().setLockName("testTryLock").setTime(3L)
             .setTimeUnit(TimeUnits.Seconds).build();
         assertSuccessfulFutureRpcResult(lockManager.tryLock(lockInput));
@@ -99,6 +111,13 @@ public class LockManagerTest extends AbstractConcurrentDataBrokerTest {
         assertSuccessfulFutureRpcResult(lockManager.tryLock(lockInput));
     }
 
+    @Test
+    public void testOptimisticLockFailedException() throws InterruptedException, ExecutionException, TimeoutException {
+        dbFailureSimulator.failSubmits(new OptimisticLockFailedException("bada boum bam!"));
+        LockInput lockInput = new LockInputBuilder().setLockName("testLock").build();
+        runUnfailSubmitsTimerTask(3000); // see other tests above
+        assertSuccessfulFutureRpcResult(lockManager.lock(lockInput));
+    }
 
     private void assertSuccessfulFutureRpcResult(Future<RpcResult<Void>> futureRpcResult)
             throws InterruptedException, ExecutionException, TimeoutException {
@@ -120,9 +139,21 @@ public class LockManagerTest extends AbstractConcurrentDataBrokerTest {
                 try {
                     assertSuccessfulFutureRpcResult(lockManager.unlock(unlockInput));
                 } catch (InterruptedException | ExecutionException | TimeoutException e) {
-                    throw new RuntimeException(e);
+                    LOG.error("runUnlockTimerTask() failed", e);
+                    // throw new RuntimeException(e) is useless here, as this in a BG Thread, and it would go nowhere
                 }
             }
         }, delay);
     }
+
+    private void runUnfailSubmitsTimerTask(long delay) {
+        Timer timer = new Timer();
+        timer.schedule(new TimerTask() {
+            @Override
+            public void run() {
+                dbFailureSimulator.unfailSubmits();
+            }
+        }, delay);
+    }
+
 }