@Test public void testDestroyTopicRemovesStatistics() { String randomTopicName = randomString(); HazelcastInstance instance = createHazelcastInstance(); final ITopic<String> topic = instance.getTopic(randomTopicName); topic.publish("foobar"); // we need to give the message the chance to be processed, else the topic statistics are // recreated // so in theory the destroy for the topic is broken sleepSeconds(1); topic.destroy(); final TopicService topicService = getNode(instance).nodeEngine.getService(TopicService.SERVICE_NAME); assertTrueEventually( new AssertTask() { @Override public void run() { boolean containsStats = topicService.getStatsMap().containsKey(topic.getName()); assertFalse(containsStats); } }); }
@Test public void testTopicPublishingMember() { final int nodeCount = 3; final String randomName = "testTopicPublishingMember" + generateRandomString(5); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(nodeCount); HazelcastInstance[] instances = factory.newInstances(); final CountDownLatch mainLatch = new CountDownLatch(nodeCount); final AtomicInteger count1 = new AtomicInteger(0); final AtomicInteger count2 = new AtomicInteger(0); final AtomicInteger count3 = new AtomicInteger(0); for (int i = 0; i < nodeCount; i++) { final HazelcastInstance instance = instances[i]; new Thread( new Runnable() { public void run() { ITopic<Long> topic = instance.getTopic(randomName); topic.addMessageListener( new MessageListener<Long>() { public void onMessage(Message<Long> message) { Member publishingMember = message.getPublishingMember(); if (publishingMember.equals(instance.getCluster().getLocalMember())) count1.incrementAndGet(); if (publishingMember.equals(message.getMessageObject())) count2.incrementAndGet(); if (publishingMember.localMember()) count3.incrementAndGet(); } }); mainLatch.countDown(); } }) .start(); } try { mainLatch.await(1, TimeUnit.MINUTES); } catch (InterruptedException e) { fail(); } for (int i = 0; i < nodeCount; i++) { HazelcastInstance instance = instances[i]; instance.getTopic(randomName).publish(instance.getCluster().getLocalMember()); } assertTrueEventually( new AssertTask() { @Override public void run() { assertEquals(nodeCount, count1.get()); assertEquals(nodeCount * nodeCount, count2.get()); assertEquals(nodeCount, count3.get()); } }); }
@Test @Category(ProblematicTest.class) public void testNearCacheInvalidationByUsingMapPutAll() { int n = 3; String mapName = "test"; Config config = new Config(); config .getMapConfig(mapName) .setNearCacheConfig(new NearCacheConfig().setInvalidateOnChange(true)); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(n); HazelcastInstance[] instances = factory.newInstances(config); IMap<Object, Object> map = instances[0].getMap(mapName); int count = 5000; for (int i = 0; i < count; i++) { map.put(i, i); } // populate the near cache for (int i = 0; i < count; i++) { map.get(i); } final NearCache nearCache = getNearCache(mapName, instances[0]); assertTrue( nearCache.size() > (count / n - count * 0.1)); // more-or-less (count / no_of_nodes) should be in the near cache now Map<Object, Object> invalidationMap = new HashMap<Object, Object>(count); for (int i = 0; i < count; i++) { invalidationMap.put(i, i); } map.putAll(invalidationMap); // this should invalidate the near cache assertTrueEventually( new AssertTask() { @Override public void run() { assertEquals("Invalidation is not working on putAll()", 0, nearCache.size()); } }); }
@Test public void testNodeStartup() { TestHazelcastFactory factory = new TestHazelcastFactory(); Config config = new Config(); config.getNetworkConfig().setPort(50001); InterfacesConfig interfaces = config.getNetworkConfig().getInterfaces(); interfaces.clear(); interfaces.setEnabled(true); interfaces.addInterface("127.0.0.1"); final HazelcastInstance hazelcastInstance1 = factory.newHazelcastInstance(config); final HazelcastInstance hazelcastInstance2 = factory.newHazelcastInstance(config); final HazelcastInstance hazelcastInstance3 = factory.newHazelcastInstance(config); try { String xmlFileName = "hazelcast-client-discovery-spi-test.xml"; InputStream xmlResource = ClientDiscoverySpiTest.class.getClassLoader().getResourceAsStream(xmlFileName); ClientConfig clientConfig = new XmlClientConfigBuilder(xmlResource).build(); final HazelcastInstance client = factory.newHazelcastClient(clientConfig); assertNotNull(hazelcastInstance1); assertNotNull(hazelcastInstance2); assertNotNull(hazelcastInstance3); assertNotNull(client); assertTrueEventually( new AssertTask() { @Override public void run() throws Exception { assertEquals(3, hazelcastInstance1.getCluster().getMembers().size()); assertEquals(3, hazelcastInstance2.getCluster().getMembers().size()); assertEquals(3, hazelcastInstance3.getCluster().getMembers().size()); assertEquals(3, client.getCluster().getMembers().size()); } }); } finally { factory.shutdownAll(); } }
@Test public void removeMessageListener() throws InterruptedException { String topicName = "removeMessageListener" + generateRandomString(5); try { HazelcastInstance instance = createHazelcastInstance(); ITopic<String> topic = instance.getTopic(topicName); final CountDownLatch latch = new CountDownLatch(2); final CountDownLatch cp = new CountDownLatch(1); MessageListener<String> messageListener = new MessageListener<String>() { public void onMessage(Message<String> msg) { latch.countDown(); cp.countDown(); } }; final String message = "message_" + messageListener.hashCode() + "_"; final String id = topic.addMessageListener(messageListener); topic.publish(message + "1"); cp.await(); topic.removeMessageListener(id); topic.publish(message + "2"); assertTrueEventually( new AssertTask() { @Override public void run() { assertEquals(1, latch.getCount()); } }); } finally { shutdownNodeFactory(); } }
@Test public void testSimpleUsage() throws InterruptedException { final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(1); final Config config = new Config(); final HazelcastInstance instance = nodeFactory.newHazelcastInstance(config); final AtomicInteger atomicInteger = new AtomicInteger(0); final ILock lock = instance.getLock("testSimpleUsage"); Assert.assertEquals("testSimpleUsage", lock.getName()); final Runnable tryLockRunnable = new Runnable() { public void run() { if (lock.tryLock()) atomicInteger.incrementAndGet(); } }; final Runnable lockRunnable = new Runnable() { public void run() { lock.lock(); } }; Assert.assertEquals(false, lock.isLocked()); lock.lock(); Assert.assertEquals(true, lock.isLocked()); Assert.assertEquals(true, lock.tryLock()); lock.unlock(); Thread thread1 = new Thread(tryLockRunnable); thread1.start(); thread1.join(); Assert.assertEquals(0, atomicInteger.get()); lock.unlock(); Thread thread2 = new Thread(tryLockRunnable); thread2.start(); thread2.join(); Assert.assertEquals(1, atomicInteger.get()); Assert.assertEquals(true, lock.isLocked()); lock.forceUnlock(); Thread thread3 = new Thread(lockRunnable); thread3.start(); thread3.join(); Assert.assertEquals(true, lock.isLocked()); Assert.assertEquals(false, lock.tryLock(2, TimeUnit.SECONDS)); Thread thread4 = new Thread(lockRunnable); thread4.start(); assertTrueEventually( new AssertTask() { @Override public void run() { Assert.assertEquals(true, lock.isLocked()); } }); lock.forceUnlock(); thread4.join(); }