/** Overly basic test for ZooKeeper locks. */
@Test
public void testZooKeeperLock() throws Exception {
final File lockDir = new File("/lock");
final ZooKeeperClient zkClient = ZooKeeperClient.getZooKeeperClient(getZKAddress());
try {
final CyclicBarrier barrier = new CyclicBarrier(2);
final ZooKeeperLock lock1 = new ZooKeeperLock(zkClient, lockDir);
final ZooKeeperLock lock2 = new ZooKeeperLock(zkClient, lockDir);
lock1.lock();
final Thread thread = new Thread() {
/** {@inheritDoc} */
@Override
public void run() {
try {
assertFalse(lock2.lock(0.1));
barrier.await();
lock2.lock();
lock2.unlock();
lock2.lock();
lock2.unlock();
lock2.close();
barrier.await();
} catch (Exception e) {
LOG.warn("Exception caught in locking thread: {}", e.getMessage());
}
}
};
thread.start();
barrier.await(5, TimeUnit.SECONDS); // Eventually fail
lock1.unlock();
lock1.close();
barrier.await(5, TimeUnit.SECONDS); // Eventually fail
} finally {
zkClient.release();
}
}