/** 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 testIssue321() throws Exception { HazelcastClient hClient = getHazelcastClient(); final IMap<Integer, Integer> imap = hClient.getMap("testIssue321_1"); final BlockingQueue<EntryEvent<Integer, Integer>> events1 = new LinkedBlockingQueue<EntryEvent<Integer, Integer>>(); final BlockingQueue<EntryEvent<Integer, Integer>> events2 = new LinkedBlockingQueue<EntryEvent<Integer, Integer>>(); imap.addEntryListener( new EntryAdapter<Integer, Integer>() { @Override public void entryAdded(EntryEvent event) { events2.add(event); } }, false); imap.addEntryListener( new EntryAdapter<Integer, Integer>() { @Override public void entryAdded(EntryEvent event) { events1.add(event); } }, true); imap.put(1, 1); final EntryEvent<Integer, Integer> event1 = events1.poll(10, TimeUnit.MILLISECONDS); final EntryEvent<Integer, Integer> event2 = events2.poll(10, TimeUnit.MILLISECONDS); assertNotNull(event1); assertNotNull(event2); assertNotNull(event1.getValue()); assertNull(event2.getValue()); }
@Test public void testMapRecordEviction() throws InterruptedException { int size = 100000; Config cfg = new Config(); MapConfig mc = cfg.getMapConfig("testMapRecordEviction"); mc.setTimeToLiveSeconds(1); final CountDownLatch latch = new CountDownLatch(size); mc.addEntryListenerConfig( new EntryListenerConfig() .setImplementation( new EntryAdapter() { public void entryEvicted(EntryEvent event) { latch.countDown(); } }) .setLocal(true)); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2); final HazelcastInstance[] instances = factory.newInstances(cfg); IMap map = instances[0].getMap("testMapRecordEviction"); for (int i = 0; i < size; i++) { map.put(i, i); } assertTrue(latch.await(5, TimeUnit.MINUTES)); assertEquals(0, map.size()); }
@Test public void testHazelcastInstances() { assertNotNull(map1); assertNotNull(map2); assertNotNull(multiMap); assertNotNull(queue); assertNotNull(topic); assertNotNull(set); assertNotNull(list); assertNotNull(executorService); assertNotNull(idGenerator); assertNotNull(atomicLong); assertNotNull(atomicReference); assertNotNull(countDownLatch); assertNotNull(semaphore); assertNotNull(lock); assertEquals("map1", map1.getName()); assertEquals("map2", map2.getName()); assertEquals("testMultimap", multiMap.getName()); assertEquals("testQ", queue.getName()); assertEquals("testTopic", topic.getName()); assertEquals("set", set.getName()); assertEquals("list", list.getName()); assertEquals("idGenerator", idGenerator.getName()); assertEquals("atomicLong", atomicLong.getName()); assertEquals("atomicReference", atomicReference.getName()); assertEquals("countDownLatch", countDownLatch.getName()); assertEquals("semaphore", semaphore.getName()); }
@Test public void testGetNullMapEntry() { HazelcastClient hClient = getHazelcastClient(); final IMap imap = hClient.getMap("testGetNullMapEntry"); String key = "key"; MapEntry mapEntry = imap.getMapEntry(key); assertNull(mapEntry); }
@Test public void testTwoMembersWithIndexes() { HazelcastClient hClient = getHazelcastClient(); final IMap imap = hClient.getMap("testTwoMembersWithIndexes"); imap.addIndex("name", false); imap.addIndex("age", true); imap.addIndex("active", false); doFunctionalQueryTest(imap); }
@Test public void putIfAbsent() { HazelcastClient hClient = getHazelcastClient(); IMap<String, String> map = hClient.getMap("putIfAbsent"); String result = map.put("1", "CBDEF"); assertNull(result); assertNull(map.putIfAbsent("2", "C")); assertEquals("C", map.putIfAbsent("2", "D")); }
@Test public void evictFromMap() { HazelcastClient hClient = getHazelcastClient(); IMap map = hClient.getMap("evictFromMap"); assertNull(map.put("a", "b")); assertEquals("b", map.get("a")); assertTrue(map.evict("a")); assertNull(map.get("a")); }
@Test public void tryPut() throws InterruptedException { HazelcastClient hClient = getHazelcastClient(); IMap<String, String> map = hClient.getMap("tryPut"); assertEquals(0, map.size()); Boolean result = map.tryPut("1", "CBDEF", 1, TimeUnit.SECONDS); assertTrue(result); assertEquals(1, map.size()); }
@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")); }
@Test public void putWithTTL() throws InterruptedException { HazelcastClient hClient = getHazelcastClient(); IMap<String, String> map = hClient.getMap("putWithTTL"); assertEquals(0, map.size()); map.put("1", "CBDEF", 100, TimeUnit.MILLISECONDS); assertEquals(1, map.size()); Thread.sleep(200); assertEquals(0, map.size()); }
@Test public void testGetAsync() throws Exception { HazelcastClient hClient = getHazelcastClient(); String key = "key"; String value1 = "value1"; IMap<String, String> map = hClient.getMap("map:test:getAsync"); map.put(key, value1); Future<String> f1 = map.getAsync(key); assertEquals(value1, f1.get()); }
@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 valuesToArray() { HazelcastClient hClient = getHazelcastClient(); IMap map = hClient.getMap("valuesToArray"); assertEquals(0, map.size()); map.put("a", "1"); map.put("b", "2"); map.put("c", "3"); assertEquals(3, map.size()); { final Object[] values = map.values().toArray(); Arrays.sort(values); assertArrayEquals(new Object[] {"1", "2", "3"}, values); } { final String[] values = (String[]) map.values().toArray(new String[3]); Arrays.sort(values); assertArrayEquals(new String[] {"1", "2", "3"}, values); } { final String[] values = (String[]) map.values().toArray(new String[2]); Arrays.sort(values); assertArrayEquals(new String[] {"1", "2", "3"}, values); } { final String[] values = (String[]) map.values().toArray(new String[5]); Arrays.sort(values, 0, 3); assertArrayEquals(new String[] {"1", "2", "3", null, null}, values); } }
@Test public void addIndex() { HazelcastClient hClient = getHazelcastClient(); IMap map = hClient.getMap("addIndex"); int size = 1000; for (int i = 0; i < size; i++) { map.put(String.valueOf(i), new Employee("name" + i, i, true, 0)); } EntryObject e = new PredicateBuilder().getEntryObject(); Predicate predicate = e.get("age").equal(23); long begin = Clock.currentTimeMillis(); Set<Entry<Object, Object>> set = map.entrySet(predicate); long timeWithoutIndex = Clock.currentTimeMillis() - begin; assertEquals(1, set.size()); assertEquals(size, map.size()); map.destroy(); map = hClient.getMap("addIndex"); map.addIndex("age", true); for (int i = 0; i < size; i++) { map.put(String.valueOf(i), new Employee("name" + i, i, true, 0)); } begin = Clock.currentTimeMillis(); set = map.entrySet(predicate); long timeWithIndex = Clock.currentTimeMillis() - begin; assertEquals(1, set.size()); assertEquals(size, map.size()); assertTrue(timeWithoutIndex > 2 * timeWithIndex); // map.addIndex("age", true); }
@Test public void isEmpty() { HazelcastClient hClient = getHazelcastClient(); IMap map = hClient.getMap("isEmpty"); int counter = 100; assertTrue(map.isEmpty()); for (int i = 0; i < counter; i++) { assertNull(map.put(i, i)); assertEquals(i, map.get(i)); } assertFalse(map.isEmpty()); }
@Test public void testMapRecordIdleEvictionOnMigration() throws InterruptedException { Config cfg = new Config(); final String name = "testMapRecordIdleEvictionOnMigration"; MapConfig mc = cfg.getMapConfig(name); int maxIdleSeconds = 10; int size = 100; final int nsize = size / 5; mc.setMaxIdleSeconds(maxIdleSeconds); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(3); HazelcastInstance instance1 = factory.newHazelcastInstance(cfg); final IMap map = instance1.getMap(name); final CountDownLatch latch = new CountDownLatch(size - nsize); map.addEntryListener( new EntryAdapter() { public void entryEvicted(EntryEvent event) { latch.countDown(); } }, false); for (int i = 0; i < size; i++) { map.put(i, i); } final Thread thread = new Thread( new Runnable() { public void run() { while (!Thread.currentThread().isInterrupted()) { try { for (int i = 0; i < nsize; i++) { map.get(i); } Thread.sleep(1000); } catch (HazelcastInstanceNotActiveException e) { return; } catch (InterruptedException e) { return; } } } }); thread.start(); HazelcastInstance instance2 = factory.newHazelcastInstance(cfg); HazelcastInstance instance3 = factory.newHazelcastInstance(cfg); assertTrue(latch.await(1, TimeUnit.MINUTES)); Assert.assertEquals(nsize, map.size()); thread.interrupt(); thread.join(5000); }
@Test public void clear() { HazelcastClient hClient = getHazelcastClient(); IMap map = hClient.getMap("clear"); for (int i = 0; i < 100; i++) { assertNull(map.put(i, i)); assertEquals(i, map.get(i)); } map.clear(); for (int i = 0; i < 100; i++) { assertNull(map.get(i)); } }
@Test public void containsValue() { HazelcastClient hClient = getHazelcastClient(); IMap map = hClient.getMap("containsValue"); int counter = 100; for (int i = 0; i < counter; i++) { assertNull(map.put(i, i)); assertEquals(i, map.get(i)); } for (int i = 0; i < counter; i++) { assertTrue(map.containsValue(i)); } }
/** * Test for issue 614 * * @throws InterruptedException */ @Test public void testContainsKeyShouldDelayEviction() throws InterruptedException { Config cfg = new Config(); String mapname = "testContainsKeyShouldDelayEviction"; cfg.getMapConfig(mapname).setMaxIdleSeconds(3); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(1); HazelcastInstance instance = factory.newHazelcastInstance(cfg); IMap<Object, Object> map = instance.getMap(mapname); map.put(1, 1); for (int i = 0; i < 20; i++) { assertTrue(map.containsKey(1)); Thread.sleep(500); } }
@Test public void iterateOverMapEntries() { HazelcastClient hClient = getHazelcastClient(); IMap<String, String> map = hClient.getMap("iterateOverMapEntries"); map.put("1", "A"); map.put("2", "B"); map.put("3", "C"); Set<Entry<String, String>> entrySet = map.entrySet(); assertEquals(3, entrySet.size()); Set<String> keySet = map.keySet(); for (Entry<String, String> entry : entrySet) { assertTrue(keySet.contains(entry.getKey())); assertEquals(entry.getValue(), map.get(entry.getKey())); } Iterator<Entry<String, String>> it = entrySet.iterator(); for (String key : keySet) { MapEntry mapEntry = map.getMapEntry(key); assertEquals(1, mapEntry.getHits()); } while (it.hasNext()) { it.next(); it.remove(); } assertTrue(map.isEmpty()); }
@Test public void testSqlPredicate() { HazelcastInstance h = getHazelcastInstance(); HazelcastClient hClient = getHazelcastClient(); IMap<Integer, Employee> map = hClient.getMap("testSqlPredicate"); for (int i = 0; i < 100; i++) { h.getMap("testSqlPredicate").put(i, new Employee("" + i, i, i % 2 == 0, i)); } Set<Entry<Integer, Employee>> set = map.entrySet(new SqlPredicate("active AND age < 30")); for (Entry<Integer, Employee> entry : set) { System.out.println(entry.getValue()); assertTrue(entry.getValue().age < 30); assertTrue(entry.getValue().active); } }
@Test public void putAllMany() { HazelcastClient hClient = getHazelcastClient(); IMap map = hClient.getMap("putAllMany"); int counter = 100; for (int j = 0; j < 4; j++, counter *= 10) { Map tempMap = new HashMap(); for (int i = 0; i < counter; i++) { tempMap.put(i, i); } map.putAll(tempMap); assertEquals(1, map.get(1)); } map.destroy(); }
/* github issue 585 */ @Test public void testIssue585ZeroTTLShouldPreventEvictionWithSet() throws InterruptedException { Config config = new Config(); config.getGroupConfig().setName("testIssue585ZeroTTLShouldPreventEvictionWithSet"); NearCacheConfig nearCacheConfig = new NearCacheConfig(); config.getMapConfig("default").setNearCacheConfig(nearCacheConfig); int n = 1; TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(n); HazelcastInstance h = factory.newHazelcastInstance(config); IMap<String, String> map = h.getMap("testIssue585ZeroTTLShouldPreventEvictionWithSet"); map.set("key", "value", 1, TimeUnit.SECONDS); map.set("key", "value2", 0, TimeUnit.SECONDS); Thread.sleep(2000); assertEquals("value2", map.get("key")); h.getLifecycleService().shutdown(); }
@Test public void getMapEntry() { HazelcastClient hClient = getHazelcastClient(); IMap map = hClient.getMap("getMapEntry"); assertNull(map.put("a", "b")); map.get("a"); map.get("a"); MapEntry<String, String> entry = map.getMapEntry("a"); assertEquals("a", entry.getKey()); assertEquals("b", entry.getValue()); assertEquals(2, entry.getHits()); assertEquals("b", entry.getValue()); assertEquals("b", entry.setValue("c")); assertEquals("c", map.get("a")); assertEquals("c", entry.getValue()); }
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 testMapPutTtlWithListener() throws InterruptedException { Config cfg = new Config(); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2); final HazelcastInstance[] instances = factory.newInstances(cfg); warmUpPartitions(instances); final int k = 10; final int putCount = 10000; final CountDownLatch latch = new CountDownLatch(k * putCount); final IMap map = instances[0].getMap("testMapEvictionTtlWithListener"); final AtomicBoolean error = new AtomicBoolean(false); final Set<Long> times = Collections.newSetFromMap(new ConcurrentHashMap<Long, Boolean>()); map.addEntryListener( new EntryAdapter() { public void entryEvicted(final EntryEvent event) { final Long expectedEvictionTime = (Long) (event.getOldValue()); long timeDifference = System.currentTimeMillis() - expectedEvictionTime; if (timeDifference > 5000) { error.set(true); times.add(timeDifference); } latch.countDown(); } }, true); for (int i = 0; i < k; i++) { final int threadId = i; int ttl = (int) (Math.random() * 5000 + 3000); for (int j = 0; j < putCount; j++) { final long expectedEvictionTime = ttl + System.currentTimeMillis(); map.put(j + putCount * threadId, expectedEvictionTime, ttl, TimeUnit.MILLISECONDS); } } assertTrue(latch.await(1, TimeUnit.MINUTES)); assertFalse("Some evictions took more than 3 seconds! -> " + times, error.get()); }
@Test public void testEvictionLRU() { final int k = 2; final int size = 10000; try { final String mapName = "testEvictionLRU"; Config cfg = new Config(); MapConfig mc = cfg.getMapConfig(mapName); mc.setEvictionPolicy(MapConfig.EvictionPolicy.LRU); mc.setEvictionPercentage(10); MaxSizeConfig msc = new MaxSizeConfig(); msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_NODE); msc.setSize(size); mc.setMaxSizeConfig(msc); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k); final HazelcastInstance[] instances = factory.newInstances(cfg); IMap<Object, Object> map = instances[0].getMap(mapName); Thread.sleep(1000); for (int i = size / 2; i < size; i++) { map.put(i, i); } Thread.sleep(2000); for (int i = 0; i < size / 2; i++) { map.put(i, i); } Thread.sleep(1000); int recentlyUsedEvicted = 0; for (int i = 0; i < size / 2; i++) { if (map.get(i) == null) { recentlyUsedEvicted++; } } Assert.assertTrue(recentlyUsedEvicted == 0); } catch (InterruptedException e) { e.printStackTrace(); } }
/* github issue 304 */ @Test public void testIssue304EvictionDespitePut() throws InterruptedException { Config c = new Config(); c.getGroupConfig().setName("testIssue304EvictionDespitePut"); final HashMap<String, MapConfig> mapConfigs = new HashMap<String, MapConfig>(); final MapConfig value = new MapConfig(); value.setMaxIdleSeconds(3); mapConfigs.put("default", value); c.setMapConfigs(mapConfigs); final Properties properties = new Properties(); properties.setProperty("hazelcast.map.cleanup.delay.seconds", "1"); // we need faster cleanups c.setProperties(properties); int n = 1; TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(n); final HazelcastInstance hazelcastInstance = factory.newHazelcastInstance(c); IMap<String, Long> map = hazelcastInstance.getMap("testIssue304EvictionDespitePutMap"); final AtomicInteger evictCount = new AtomicInteger(0); map.addEntryListener( new EntryListener<String, Long>() { public void entryAdded(EntryEvent<String, Long> event) {} public void entryRemoved(EntryEvent<String, Long> event) {} public void entryUpdated(EntryEvent<String, Long> event) {} public void entryEvicted(EntryEvent<String, Long> event) { evictCount.incrementAndGet(); } }, true); String key = "key"; for (int i = 0; i < 5; i++) { map.put(key, System.currentTimeMillis()); Thread.sleep(1000); } assertEquals(evictCount.get(), 0); assertNotNull(map.get(key)); hazelcastInstance.getLifecycleService().shutdown(); }
/** * 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()); }