/** Test for issue #39 */ @Test public void testIsMapKeyLocked() throws InterruptedException { HazelcastClient hClient = getHazelcastClient(); final IMap map = hClient.getMap("testIsMapKeyLocked"); assertFalse(map.isLocked("key")); map.lock("key"); assertTrue(map.isLocked("key")); final CountDownLatch latch = new CountDownLatch(1); Thread thread = new Thread( new Runnable() { public void run() { assertTrue(map.isLocked("key")); try { while (map.isLocked("key")) { Thread.sleep(100); } } catch (InterruptedException e) { throw new RuntimeException(e); } latch.countDown(); } }); thread.start(); Thread.sleep(100); map.unlock("key"); assertTrue(latch.await(3, TimeUnit.SECONDS)); }
@Test public void lockMapKey() throws InterruptedException { HazelcastClient hClient = getHazelcastClient(); final IMap<String, String> map = hClient.getMap("lockMapKey"); final CountDownLatch latch = new CountDownLatch(1); map.put("a", "b"); Thread.sleep(10); map.lock("a"); new Thread( new Runnable() { public void run() { map.lock("a"); latch.countDown(); } }) .start(); Thread.sleep(10); assertEquals(1, latch.getCount()); map.unlock("a"); assertTrue(latch.await(10, TimeUnit.SECONDS)); }
@Test public void tryLock() throws InterruptedException { HazelcastClient hClient = getHazelcastClient(); final IMap<String, String> map = hClient.getMap("tryLock"); final CountDownLatch latch = new CountDownLatch(3); map.put("1", "A"); map.lock("1"); new Thread( new Runnable() { public void run() { if (!map.tryLock("1", 100, TimeUnit.MILLISECONDS)) { latch.countDown(); } if (!map.tryLock("1")) { latch.countDown(); } if (map.tryLock("2")) { latch.countDown(); } } }) .start(); assertTrue(latch.await(10, TimeUnit.SECONDS)); }