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
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.controller.cluster.access.client;
10
11 import java.util.concurrent.Executors;
12 import java.util.concurrent.ScheduledExecutorService;
13 import java.util.concurrent.TimeUnit;
14 import org.junit.After;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18
19 public class InversibleLockTest {
20
21     private InversibleLock lock;
22     private ScheduledExecutorService executor;
23
24     @Before
25     public void setUp() throws Exception {
26         lock = new InversibleLock();
27         executor = Executors.newScheduledThreadPool(1);
28     }
29
30     @After
31     public void tearDown() throws Exception {
32         executor.shutdownNow();
33     }
34
35     @Test(timeout = 2000)
36     public void testWriteLockUnlock() throws Exception {
37         final long stamp = lock.writeLock();
38         Assert.assertTrue(lock.validate(stamp));
39         executor.schedule(() -> lock.unlockWrite(stamp), 500, TimeUnit.MILLISECONDS);
40         try {
41             lock.optimisticRead();
42         } catch (final InversibleLockException e) {
43             e.awaitResolution();
44         }
45     }
46
47     @Test
48     public void testLockAfterRead() throws Exception {
49         final long readStamp = lock.optimisticRead();
50         lock.writeLock();
51         Assert.assertFalse(lock.validate(readStamp));
52     }
53 }