@Test public void removeListener() throws InterruptedException, IOException { HazelcastClient hClient = getHazelcastClient(); final IMap<String, String> map = hClient.getMap("removeListener"); final CountDownLatch entryAddLatch = new CountDownLatch(5); final CountDownLatch entryUpdatedLatch = new CountDownLatch(5); final CountDownLatch entryRemovedLatch = new CountDownLatch(5); CountDownLatchEntryListener<String, String> listener1 = new CountDownLatchEntryListener<String, String>( entryAddLatch, entryUpdatedLatch, entryRemovedLatch); CountDownLatchEntryListener<String, String> listener2 = new CountDownLatchEntryListener<String, String>( entryAddLatch, entryUpdatedLatch, entryRemovedLatch); map.addEntryListener(listener1, true); map.put("hello", "world"); map.put("hello", "new world"); map.remove("hello"); Thread.sleep(100); assertEquals(4, entryAddLatch.getCount()); assertEquals(4, entryRemovedLatch.getCount()); assertEquals(4, entryUpdatedLatch.getCount()); map.removeEntryListener(listener1); map.put("hello", "world"); map.put("hello", "new world"); map.remove("hello"); Thread.sleep(100); assertEquals(4, entryAddLatch.getCount()); assertEquals(4, entryRemovedLatch.getCount()); assertEquals(4, entryUpdatedLatch.getCount()); }
@Test public void removeIfSame() { HazelcastClient hClient = getHazelcastClient(); IMap<String, String> map = hClient.getMap("remove"); String result = map.put("1", "CBDEF"); assertNull(result); assertFalse(map.remove("1", "CBD")); assertEquals("CBDEF", map.get("1")); assertTrue(map.remove("1", "CBDEF")); }
public void doFunctionalQueryTest(IMap imap) { Employee em = new Employee("joe", 33, false, 14.56); imap.put("1", new Employee("joe", 33, false, 14.56)); imap.put("2", new Employee("ali", 23, true, 15.00)); for (int i = 3; i < 103; i++) { imap.put( String.valueOf(i), new Employee("name" + i, i % 60, ((i % 2) == 1), Double.valueOf(i))); } Set<Map.Entry> entries = imap.entrySet(); assertEquals(102, entries.size()); int itCount = 0; for (Map.Entry entry : entries) { Employee c = (Employee) entry.getValue(); itCount++; } assertEquals(102, itCount); EntryObject e = new PredicateBuilder().getEntryObject(); Predicate predicate = e.is("active").and(e.get("age").equal(23)); entries = imap.entrySet(predicate); assertEquals(3, entries.size()); for (Map.Entry entry : entries) { Employee c = (Employee) entry.getValue(); assertEquals(c.getAge(), 23); assertTrue(c.isActive()); } imap.remove("2"); entries = imap.entrySet(predicate); assertEquals(2, entries.size()); for (Map.Entry entry : entries) { Employee c = (Employee) entry.getValue(); assertEquals(c.getAge(), 23); assertTrue(c.isActive()); } }
@Test public void getSize() { HazelcastClient hClient = getHazelcastClient(); IMap map = hClient.getMap("getSize"); assertEquals(0, map.size()); map.put("a", "b"); assertEquals(1, map.size()); for (int i = 0; i < 100; i++) { map.put(String.valueOf(i), String.valueOf(i)); } assertEquals(101, map.size()); map.remove("a"); assertEquals(100, map.size()); for (int i = 0; i < 50; i++) { map.remove(String.valueOf(i)); } assertEquals(50, map.size()); for (int i = 50; i < 100; i++) { map.remove(String.valueOf(i)); } assertEquals(0, map.size()); }
@Test public void testIssue508And513() throws Exception { HazelcastClient client = getHazelcastClient(); IMap<String, HashSet<byte[]>> callEventsMap = client.getMap("CALL_EVENTS"); IMap<String, Long> metaDataMap = client.getMap("CALL_META_DATA"); IMap<String, byte[]> callStartMap = client.getMap("CALL_START_EVENTS"); MultiMap<String, String> calls = client.getMultiMap("CALLS"); calls.lock("1"); calls.unlock("1"); byte[] bytes = new byte[10]; HashSet<byte[]> hashSet = new HashSet<byte[]>(); hashSet.add(bytes); String callId = "1"; callEventsMap.put(callId, hashSet); callStartMap.put(callId, bytes); metaDataMap.put(callId, 10L); Transaction txn = client.getTransaction(); txn.begin(); try { // remove the data callEventsMap.remove(callId); // remove meta data metaDataMap.remove(callId); // remove call start callStartMap.remove(callId); calls.put(callId, callId); txn.commit(); } catch (Exception e) { fail(); } assertNull(callEventsMap.get(callId)); assertNull(metaDataMap.get(callId)); assertNull(callStartMap.get(callId)); assertEquals(0, callEventsMap.size()); assertEquals(0, metaDataMap.size()); assertEquals(0, callStartMap.size()); }
/** * Test for the issue 537. Eviction event is fired for an object already removed * * @throws Exception */ @Test public void testEvictionAfterRemove() throws InterruptedException { Config cfg = new Config(); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(1); HazelcastInstance instance = factory.newHazelcastInstance(cfg); IMap<Object, Object> map = instance.getMap("map"); final AtomicInteger count = new AtomicInteger(0); map.addEntryListener( new EntryAdapter<Object, Object>() { @Override public void entryEvicted(EntryEvent<Object, Object> event) { count.incrementAndGet(); } }, true); map.put(1, 1, 1, TimeUnit.SECONDS); map.put(2, 2, 1, TimeUnit.SECONDS); map.remove(1); Thread.sleep(2000); assertEquals(1, count.get()); }
@Test public void addListenerForKey() throws InterruptedException, IOException { HazelcastClient hClient = getHazelcastClient(); final IMap<String, String> map = hClient.getMap("addListenerForKey"); map.clear(); assertEquals(0, map.size()); final CountDownLatch entryAddLatch = new CountDownLatch(1); final CountDownLatch entryUpdatedLatch = new CountDownLatch(1); final CountDownLatch entryRemovedLatch = new CountDownLatch(1); CountDownLatchEntryListener<String, String> listener = new CountDownLatchEntryListener<String, String>( entryAddLatch, entryUpdatedLatch, entryRemovedLatch); map.addEntryListener(listener, "hello", true); assertNull(map.get("hello")); map.put("hello", "world"); map.put("hello", "new world"); assertEquals("new world", map.get("hello")); map.remove("hello"); assertTrue(entryAddLatch.await(10, TimeUnit.SECONDS)); assertTrue(entryUpdatedLatch.await(10, TimeUnit.SECONDS)); assertTrue(entryRemovedLatch.await(10, TimeUnit.SECONDS)); }
@Test public void testPutWithTwoMember() throws Exception { Config config = new Config(); HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config); HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config); assertEquals(2, h1.getCluster().getMembers().size()); assertEquals(2, h2.getCluster().getMembers().size()); IMap imap1 = h1.getMap("default"); IMap imap2 = h2.getMap("default"); assertEquals(0, imap1.size()); assertEquals(0, imap2.size()); CMap cmap1 = getCMap(h1, "default"); CMap cmap2 = getCMap(h2, "default"); assertNotNull(cmap1); assertNotNull(cmap2); Object key = "1"; Object value = "value"; Data dKey = toData(key); Data dValue = toData(value); imap1.put(key, value, 5, TimeUnit.SECONDS); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals( 1, cmap1.getMapIndexService().getOwnedRecords().size() + cmap2.getMapIndexService().getOwnedRecords().size()); Record record1 = cmap1.getRecord(dKey); Record record2 = cmap2.getRecord(dKey); long now = System.currentTimeMillis(); long millisLeft1 = record1.getExpirationTime() - now; long millisLeft2 = record2.getExpirationTime() - now; assertTrue(millisLeft1 <= 5000 && millisLeft1 > 0); assertTrue(millisLeft2 <= 5000 && millisLeft2 > 0); assertTrue(record1.isActive()); assertTrue(record2.isActive()); assertEquals(1, record1.valueCount()); assertEquals(1, record2.valueCount()); assertEquals(dValue, record1.getValueData()); assertEquals(dValue, record2.getValueData()); imap1.set("2", "value2", 5, TimeUnit.SECONDS); assertEquals("value2", imap1.get("2")); assertEquals("value2", imap2.get("2")); Thread.sleep(6000); assertNull(imap1.get("2")); assertNull(imap2.get("2")); now = System.currentTimeMillis(); assertFalse(record1.isValid(now)); assertFalse(record2.isValid(now)); Thread.sleep(23000); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); assertEquals(0, cmap2.getMapIndexService().getOwnedRecords().size()); assertEquals(0, cmap1.mapRecords.size()); assertEquals(0, cmap2.mapRecords.size()); imap1.put(key, value, 10, TimeUnit.SECONDS); assertTrue(migrateKey(key, h1, h1, 0)); assertTrue(migrateKey(key, h1, h2, 1)); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals( 1, cmap1.getMapIndexService().getOwnedRecords().size() + cmap2.getMapIndexService().getOwnedRecords().size()); record1 = cmap1.getRecord(dKey); record2 = cmap2.getRecord(dKey); now = System.currentTimeMillis(); millisLeft1 = record1.getExpirationTime() - now; millisLeft2 = record2.getExpirationTime() - now; assertTrue(millisLeft1 <= 11000 && millisLeft1 > 0); assertTrue(millisLeft2 <= 11000 && millisLeft2 > 0); assertTrue(record1.isActive()); assertTrue(record2.isActive()); assertTrue(record1.isValid(now)); assertTrue(record2.isValid(now)); assertEquals(1, record1.valueCount()); assertEquals(1, record2.valueCount()); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals( 1, cmap1.getMapIndexService().getOwnedRecords().size() + cmap2.getMapIndexService().getOwnedRecords().size()); assertTrue(migrateKey(key, h1, h2, 0)); assertTrue(migrateKey(key, h1, h1, 1)); cmap1.startCleanup(true); cmap2.startCleanup(true); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals(1, cmap2.getMapIndexService().getOwnedRecords().size()); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); now = System.currentTimeMillis(); millisLeft1 = record1.getExpirationTime() - now; millisLeft2 = record2.getExpirationTime() - now; assertTrue(millisLeft1 <= 10000 && millisLeft1 > 0); assertTrue(millisLeft2 <= 10000 && millisLeft2 > 0); assertTrue(record1.isActive()); assertTrue(record2.isActive()); assertTrue(record1.isValid(now)); assertTrue(record2.isValid(now)); assertEquals(1, record1.valueCount()); assertEquals(1, record2.valueCount()); Thread.sleep(11000); now = System.currentTimeMillis(); assertFalse(record1.isValid(now)); assertFalse(record2.isValid(now)); Thread.sleep(20000); assertEquals(0, cmap1.mapRecords.size()); assertEquals(0, cmap2.mapRecords.size()); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); assertEquals(0, cmap2.getMapIndexService().getOwnedRecords().size()); imap1.put("1", "value1"); record1 = cmap1.getRecord(dKey); record2 = cmap2.getRecord(dKey); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); assertEquals(1, cmap2.getMapIndexService().getOwnedRecords().size()); now = System.currentTimeMillis(); assertEquals(Long.MAX_VALUE, record1.getExpirationTime()); assertEquals(Long.MAX_VALUE, record2.getExpirationTime()); assertTrue(record1.isActive()); assertTrue(record2.isActive()); assertTrue(record1.isValid(now)); assertTrue(record2.isValid(now)); assertEquals(1, record1.valueCount()); assertEquals(1, record2.valueCount()); imap1.remove("1"); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); assertEquals(0, cmap2.getMapIndexService().getOwnedRecords().size()); Thread.sleep(20000); assertEquals(0, cmap1.mapRecords.size()); assertEquals(0, cmap2.mapRecords.size()); assertEquals(0, cmap1.mapIndexService.size()); assertEquals(0, cmap2.mapIndexService.size()); }