@Test(timeout = 120000) public void testOneMemberWriteThroughWithIndex() throws Exception { TestMapStore testMapStore = new TestMapStore(1, 1, 1); testMapStore.setLoadAllKeys(false); Config config = newConfig(testMapStore, 0); TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(3); HazelcastInstance h1 = nodeFactory.newHazelcastInstance(config); testMapStore.insert("1", "value1"); IMap map = h1.getMap("default"); assertEquals(0, map.size()); assertTrue(map.tryLock("1", 1, TimeUnit.SECONDS)); assertEquals("value1", map.get("1")); map.unlock("1"); assertEquals("value1", map.put("1", "value2")); assertEquals("value2", map.get("1")); assertEquals("value2", testMapStore.getStore().get("1")); assertEquals(1, map.size()); assertTrue(map.evict("1")); assertEquals(0, map.size()); assertEquals(1, testMapStore.getStore().size()); assertEquals("value2", map.get("1")); assertEquals(1, map.size()); map.remove("1"); assertEquals(0, map.size()); assertEquals(0, testMapStore.getStore().size()); testMapStore.assertAwait(1); assertEquals(1, testMapStore.getInitCount()); assertEquals("default", testMapStore.getMapName()); assertEquals(TestUtil.getNode(h1), TestUtil.getNode(testMapStore.getHazelcastInstance())); }
/** 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 testMigrationOfTTLAndLock() throws Exception { Config config = new Config(); final HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config); final HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config); ConcurrentMapManager cmm1 = getConcurrentMapManager(h1); ConcurrentMapManager cmm2 = getConcurrentMapManager(h2); final IMap imap1 = h1.getMap("default"); final IMap imap2 = h2.getMap("default"); final Data dKey = toData("1"); assertTrue(migrateKey("1", h1, h1, 0)); assertTrue(migrateKey("1", h1, h2, 1)); imap1.put("1", "value1", 60, TimeUnit.SECONDS); imap1.lock("1"); Future put2 = imap2.putAsync("1", "value2"); imap2.addEntryListener( new EntryAdapter() { @Override public void entryUpdated(EntryEvent entryEvent) { System.out.println(entryEvent); } }, "1", true); if (put2 == null) fail(); Thread.sleep(1000); CMap cmap1 = getCMap(h1, "default"); CMap cmap2 = getCMap(h2, "default"); Record record1 = cmap1.getRecord(dKey); assertEquals(1, record1.getScheduledActionCount()); for (ScheduledAction scheduledAction : record1.getScheduledActions()) { assertTrue(scheduledAction.isValid()); } assertNotNull(record1.getListeners()); assertEquals(1, record1.getListeners().size()); DistributedLock lock = cmap1.getRecord(dKey).getLock(); assertTrue(cmm1.thisAddress.equals(lock.getLockAddress())); assertTrue(lock.getLockThreadId() != -1); assertEquals(1, lock.getLockCount()); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals(0, cmap2.getMapIndexService().getOwnedRecords().size()); assertEquals(1, cmap1.getMapIndexService().getOwnedRecords().size()); assertTrue(migrateKey("1", h1, h2, 0)); assertTrue(migrateKey("1", h1, h1, 1)); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); assertEquals(1, cmap2.getMapIndexService().getOwnedRecords().size()); assertTrue(cmap1.getRecord(dKey).getRemainingTTL() < 60000); assertTrue(cmap2.getRecord(dKey).getRemainingTTL() < 60000); Record record2 = cmap2.getRecord(dKey); lock = record2.getLock(); assertTrue(cmm1.thisAddress.equals(lock.getLockAddress())); assertTrue(lock.getLockThreadId() != -1); assertEquals(1, lock.getLockCount()); lock = cmap1.getRecord(dKey).getLock(); assertTrue(cmm1.thisAddress.equals(lock.getLockAddress())); assertTrue(lock.getLockThreadId() != -1); assertEquals(1, lock.getLockCount()); imap1.unlock("1"); put2.get(10, TimeUnit.SECONDS); assertEquals("value2", imap1.get("1")); assertEquals("value2", imap2.get("1")); assertNotNull(record2.getListeners()); assertEquals(1, record2.getListeners().size()); }