@Test public void addTwoListenerAndRemoveOne() throws InterruptedException { String topicName = "addTwoListenerAndRemoveOne" + generateRandomString(5); HazelcastInstance instance = createHazelcastInstance(); ITopic<String> topic = instance.getTopic(topicName); final CountDownLatch latch = new CountDownLatch(3); final CountDownLatch cp = new CountDownLatch(2); final AtomicInteger atomicInteger = new AtomicInteger(); final String message = "Hazelcast Rocks!"; MessageListener<String> messageListener1 = new MessageListener<String>() { public void onMessage(Message<String> msg) { atomicInteger.incrementAndGet(); latch.countDown(); cp.countDown(); } }; MessageListener<String> messageListener2 = new MessageListener<String>() { public void onMessage(Message<String> msg) { atomicInteger.incrementAndGet(); latch.countDown(); cp.countDown(); } }; String messageListenerId = topic.addMessageListener(messageListener1); topic.addMessageListener(messageListener2); topic.publish(message); assertOpenEventually(cp); topic.removeMessageListener(messageListenerId); topic.publish(message); assertOpenEventually(latch); assertEquals(3, atomicInteger.get()); }
@Test public void testNearCacheEvictionByUsingMapTTLEviction() throws InterruptedException { final int instanceCount = 3; final int ttl = 1; final int size = 1000; final Config cfg = new Config(); final String mapName = "_testNearCacheEvictionByUsingMapTTLEviction_"; final NearCacheConfig nearCacheConfig = new NearCacheConfig(); nearCacheConfig.setInvalidateOnChange(true); nearCacheConfig.setInMemoryFormat(InMemoryFormat.OBJECT); cfg.getMapConfig(mapName).setNearCacheConfig(nearCacheConfig); final MapConfig mapConfig = cfg.getMapConfig(mapName); mapConfig.setTimeToLiveSeconds(ttl); final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(instanceCount); final HazelcastInstance instance1 = factory.newHazelcastInstance(cfg); final HazelcastInstance instance2 = factory.newHazelcastInstance(cfg); final HazelcastInstance instance3 = factory.newHazelcastInstance(cfg); final IMap map1 = instance1.getMap(mapName); final IMap map2 = instance2.getMap(mapName); final IMap map3 = instance3.getMap(mapName); // observe eviction final CountDownLatch latch = new CountDownLatch(size); map1.addEntryListener( new EntryAdapter() { public void entryEvicted(EntryEvent event) { latch.countDown(); } }, false); // populate map for (int i = 0; i < size; i++) { // populate. map1.put(i, i); // bring near caches. -- here is a time window // that "i" already evicted. so a "get" brings // a NULL object to the near cache. map1.get(i); map2.get(i); map3.get(i); } // wait operations to complete assertOpenEventually(latch); // check map size after eviction. assertEquals(0, map1.size()); assertEquals(0, map2.size()); assertEquals(0, map3.size()); // near cache sizes should be zero after eviction. assertEquals(0, countNotNullValuesInNearCache(mapName, instance1)); assertEquals(0, countNotNullValuesInNearCache(mapName, instance2)); assertEquals(0, countNotNullValuesInNearCache(mapName, instance3)); }