Add cds-access-client unit tests
[controller.git] / opendaylight / md-sal / cds-access-client / src / test / java / org / opendaylight / controller / cluster / access / client / InversibleLockTest.java
diff --git a/opendaylight/md-sal/cds-access-client/src/test/java/org/opendaylight/controller/cluster/access/client/InversibleLockTest.java b/opendaylight/md-sal/cds-access-client/src/test/java/org/opendaylight/controller/cluster/access/client/InversibleLockTest.java
new file mode 100644 (file)
index 0000000..56b3f54
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.controller.cluster.access.client;
+
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class InversibleLockTest {
+
+    private InversibleLock lock;
+    private ScheduledExecutorService executor;
+
+    @Before
+    public void setUp() throws Exception {
+        lock = new InversibleLock();
+        executor = Executors.newScheduledThreadPool(1);
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        executor.shutdownNow();
+    }
+
+    @Test(timeout = 2000)
+    public void testWriteLockUnlock() throws Exception {
+        final long stamp = lock.writeLock();
+        Assert.assertTrue(lock.validate(stamp));
+        executor.schedule(() -> lock.unlockWrite(stamp), 500, TimeUnit.MILLISECONDS);
+        try {
+            lock.optimisticRead();
+        } catch (final InversibleLockException e) {
+            e.awaitResolution();
+        }
+    }
+
+    @Test
+    public void testLockAfterRead() throws Exception {
+        final long readStamp = lock.optimisticRead();
+        lock.writeLock();
+        Assert.assertFalse(lock.validate(readStamp));
+    }
+}
\ No newline at end of file