Search in sources :

Example 1 with DistributedObjectEvent

use of com.hazelcast.core.DistributedObjectEvent in project hazelcast by hazelcast.

the class QueueEvictionTest method testQueueEviction_whenTtlIsZero_thenListenersAreNeverthelessExecuted.

@Test
public void testQueueEviction_whenTtlIsZero_thenListenersAreNeverthelessExecuted() throws Exception {
    String queueName = randomString();
    Config config = smallInstanceConfig();
    config.getQueueConfig("default").setPriorityComparatorClassName(comparatorClassName);
    config.getQueueConfig(queueName).setEmptyQueueTtl(0);
    HazelcastInstance hz = createHazelcastInstance(config);
    final CountDownLatch latch = new CountDownLatch(2);
    hz.addDistributedObjectListener(new DistributedObjectListener() {

        public void distributedObjectCreated(DistributedObjectEvent event) {
            latch.countDown();
        }

        public void distributedObjectDestroyed(DistributedObjectEvent event) {
            latch.countDown();
        }
    });
    IQueue<VersionedObject<String>> queue = hz.getQueue(queueName);
    assertTrue(queue.offer(new VersionedObject<>("item")));
    assertEquals(new VersionedObject<>("item"), queue.poll());
    assertTrue(latch.await(10, TimeUnit.SECONDS));
}
Also used : VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) HazelcastInstance(com.hazelcast.core.HazelcastInstance) DistributedObjectEvent(com.hazelcast.core.DistributedObjectEvent) Config(com.hazelcast.config.Config) CountDownLatch(java.util.concurrent.CountDownLatch) DistributedObjectListener(com.hazelcast.core.DistributedObjectListener) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 2 with DistributedObjectEvent

use of com.hazelcast.core.DistributedObjectEvent in project hazelcast by hazelcast.

the class TestCacheManager method testCacheNames.

@Test
public void testCacheNames() {
    // create a test instance, to reproduce the behavior described in the GitHub issue
    // https://github.com/hazelcast/hazelcast/issues/492
    final String testMap = "test-map";
    final CountDownLatch distributionSignal = new CountDownLatch(1);
    instance.addDistributedObjectListener(new DistributedObjectListener() {

        @Override
        public void distributedObjectCreated(DistributedObjectEvent event) {
            DistributedObject distributedObject = event.getDistributedObject();
            if (distributedObject instanceof IMap) {
                IMap<?, ?> map = (IMap) distributedObject;
                if (testMap.equals(map.getName())) {
                    distributionSignal.countDown();
                }
            }
        }

        @Override
        public void distributedObjectDestroyed(DistributedObjectEvent event) {
        }
    });
    HazelcastInstance testInstance = Hazelcast.newHazelcastInstance(extractConfig());
    testInstance.getMap(testMap);
    // be sure that test-map is distributed
    HazelcastTestSupport.assertOpenEventually(distributionSignal);
    Collection<String> test = cacheManager.getCacheNames();
    assertContains(test, testMap);
    testInstance.shutdown();
}
Also used : DistributedObject(com.hazelcast.core.DistributedObject) IMap(com.hazelcast.map.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) DistributedObjectEvent(com.hazelcast.core.DistributedObjectEvent) CountDownLatch(java.util.concurrent.CountDownLatch) DistributedObjectListener(com.hazelcast.core.DistributedObjectListener) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with DistributedObjectEvent

use of com.hazelcast.core.DistributedObjectEvent in project hazelcast by hazelcast.

the class DistributedObjectListenerTest method destroyedNotReceivedOnClient.

@Test
public void destroyedNotReceivedOnClient() throws Exception {
    HazelcastInstance instance = hazelcastFactory.newHazelcastInstance();
    final HazelcastInstance client = hazelcastFactory.newHazelcastClient();
    final CountDownLatch createdLatch = new CountDownLatch(1);
    final CountDownLatch destroyedLatch = new CountDownLatch(1);
    client.addDistributedObjectListener(new DistributedObjectListener() {

        @Override
        public void distributedObjectCreated(DistributedObjectEvent event) {
            createdLatch.countDown();
        }

        @Override
        public void distributedObjectDestroyed(DistributedObjectEvent event) {
            destroyedLatch.countDown();
        }
    });
    final String name = randomString();
    final ITopic<Object> topic = instance.getTopic(name);
    assertOpenEventually(createdLatch, 10);
    assertEquals(1, client.getDistributedObjects().size());
    topic.destroy();
    assertOpenEventually(destroyedLatch, 10);
    assertTrueAllTheTime(new AssertTask() {

        @Override
        public void run() throws Exception {
            Collection<DistributedObject> distributedObjects = client.getDistributedObjects();
            assertTrue(distributedObjects.isEmpty());
        }
    }, 5);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) DistributedObjectEvent(com.hazelcast.core.DistributedObjectEvent) AssertTask(com.hazelcast.test.AssertTask) Collection(java.util.Collection) DistributedObject(com.hazelcast.core.DistributedObject) CountDownLatch(java.util.concurrent.CountDownLatch) DistributedObjectListener(com.hazelcast.core.DistributedObjectListener) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 4 with DistributedObjectEvent

use of com.hazelcast.core.DistributedObjectEvent in project hazelcast by hazelcast.

the class QueueListenerTest method testListener_withEvictionViaTTL.

@Test
public void testListener_withEvictionViaTTL() throws Exception {
    Config config = getConfig();
    config.getQueueConfig("queueWithTTL").setEmptyQueueTtl(0);
    HazelcastInstance hz = createHazelcastInstance(config);
    final CountDownLatch latch = new CountDownLatch(2);
    hz.addDistributedObjectListener(new DistributedObjectListener() {

        @Override
        public void distributedObjectCreated(DistributedObjectEvent event) {
            latch.countDown();
        }

        @Override
        public void distributedObjectDestroyed(DistributedObjectEvent event) {
            latch.countDown();
        }
    });
    IQueue<VersionedObject<String>> queue = hz.getQueue("queueWithTTL");
    queue.offer(new VersionedObject<>("item"));
    queue.poll();
    assertTrue(latch.await(10, TimeUnit.SECONDS));
}
Also used : VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) HazelcastInstance(com.hazelcast.core.HazelcastInstance) DistributedObjectEvent(com.hazelcast.core.DistributedObjectEvent) QueueConfig(com.hazelcast.config.QueueConfig) Config(com.hazelcast.config.Config) ItemListenerConfig(com.hazelcast.config.ItemListenerConfig) CountDownLatch(java.util.concurrent.CountDownLatch) DistributedObjectListener(com.hazelcast.core.DistributedObjectListener) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

DistributedObjectEvent (com.hazelcast.core.DistributedObjectEvent)4 DistributedObjectListener (com.hazelcast.core.DistributedObjectListener)4 HazelcastInstance (com.hazelcast.core.HazelcastInstance)4 QuickTest (com.hazelcast.test.annotation.QuickTest)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Test (org.junit.Test)4 VersionedObject (com.hazelcast.collection.impl.queue.model.VersionedObject)2 Config (com.hazelcast.config.Config)2 DistributedObject (com.hazelcast.core.DistributedObject)2 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)2 ItemListenerConfig (com.hazelcast.config.ItemListenerConfig)1 QueueConfig (com.hazelcast.config.QueueConfig)1 IMap (com.hazelcast.map.IMap)1 AssertTask (com.hazelcast.test.AssertTask)1 ParallelTest (com.hazelcast.test.annotation.ParallelTest)1 Collection (java.util.Collection)1