Search in sources :

Example 11 with Message

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

the class ClientReliableTopicTest method testMessageFieldSetCorrectly.

@Test
public void testMessageFieldSetCorrectly() {
    ITopic topic = client.getReliableTopic(randomString());
    final ReliableMessageListenerMock listener = new ReliableMessageListenerMock();
    topic.addMessageListener(listener);
    final long beforePublishTime = Clock.currentTimeMillis();
    final String messageStr = randomString();
    topic.publish(messageStr);
    final long afterPublishTime = Clock.currentTimeMillis();
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(1, listener.messages.size());
            Message<String> message = listener.messages.get(0);
            assertEquals(messageStr, message.getMessageObject());
            assertEquals(null, message.getPublishingMember());
            long actualPublishTime = message.getPublishTime();
            assertTrue(actualPublishTime >= beforePublishTime);
            assertTrue(actualPublishTime <= afterPublishTime);
        }
    });
}
Also used : ITopic(com.hazelcast.core.ITopic) Message(com.hazelcast.core.Message) ReliableMessageListenerMock(com.hazelcast.topic.impl.reliable.ReliableMessageListenerMock) AssertTask(com.hazelcast.test.AssertTask) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 12 with Message

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

the class ClientReliableTopicTest method testListener.

@Test
public void testListener() throws InterruptedException {
    ITopic topic = client.getReliableTopic(randomString());
    int messageCount = 10;
    final CountDownLatch latch = new CountDownLatch(messageCount);
    MessageListener listener = new MessageListener() {

        public void onMessage(Message message) {
            latch.countDown();
        }
    };
    topic.addMessageListener(listener);
    for (int i = 0; i < messageCount; i++) {
        topic.publish(i);
    }
    assertTrue(latch.await(20, TimeUnit.SECONDS));
}
Also used : ITopic(com.hazelcast.core.ITopic) Message(com.hazelcast.core.Message) MessageListener(com.hazelcast.core.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 13 with Message

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

the class ClientTopicTest method testListener.

@Test
public void testListener() throws InterruptedException {
    ITopic topic = client.getTopic(randomString());
    final CountDownLatch latch = new CountDownLatch(10);
    MessageListener listener = new MessageListener() {

        public void onMessage(Message message) {
            latch.countDown();
        }
    };
    topic.addMessageListener(listener);
    for (int i = 0; i < 10; i++) {
        topic.publish(i);
    }
    assertTrue(latch.await(20, TimeUnit.SECONDS));
}
Also used : ITopic(com.hazelcast.core.ITopic) Message(com.hazelcast.core.Message) MessageListener(com.hazelcast.core.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 14 with Message

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

the class ClientTopicTest method testRemoveListener.

@Test
public void testRemoveListener() {
    ITopic topic = client.getTopic(randomString());
    MessageListener listener = new MessageListener() {

        public void onMessage(Message message) {
        }
    };
    String id = topic.addMessageListener(listener);
    assertTrue(topic.removeMessageListener(id));
}
Also used : ITopic(com.hazelcast.core.ITopic) Message(com.hazelcast.core.Message) MessageListener(com.hazelcast.core.MessageListener) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 15 with Message

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

the class TopicTest method removeMessageListener.

@Test
@Repeat(10)
public void removeMessageListener() throws InterruptedException {
    String topicName = "removeMessageListener" + generateRandomString(5);
    try {
        HazelcastInstance instance = createHazelcastInstance();
        ITopic<String> topic = instance.getTopic(topicName);
        final AtomicInteger onMessageCount = new AtomicInteger(0);
        final CountDownLatch onMessageInvoked = new CountDownLatch(1);
        MessageListener<String> messageListener = new MessageListener<String>() {

            public void onMessage(Message<String> msg) {
                onMessageCount.incrementAndGet();
                onMessageInvoked.countDown();
            }
        };
        final String message = "message_" + messageListener.hashCode() + "_";
        final String id = topic.addMessageListener(messageListener);
        topic.publish(message + "1");
        onMessageInvoked.await();
        assertTrue(topic.removeMessageListener(id));
        topic.publish(message + "2");
        assertTrueEventually(new AssertTask() {

            @Override
            public void run() {
                assertEquals(1, onMessageCount.get());
            }
        });
    } finally {
        shutdownNodeFactory();
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Message(com.hazelcast.core.Message) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageListener(com.hazelcast.core.MessageListener) AssertTask(com.hazelcast.test.AssertTask) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest) Repeat(com.hazelcast.test.annotation.Repeat)

Aggregations

Message (com.hazelcast.core.Message)18 Test (org.junit.Test)15 ParallelTest (com.hazelcast.test.annotation.ParallelTest)12 QuickTest (com.hazelcast.test.annotation.QuickTest)12 MessageListener (com.hazelcast.core.MessageListener)11 CountDownLatch (java.util.concurrent.CountDownLatch)9 ITopic (com.hazelcast.core.ITopic)8 HazelcastInstance (com.hazelcast.core.HazelcastInstance)7 NightlyTest (com.hazelcast.test.annotation.NightlyTest)7 Config (com.hazelcast.config.Config)4 ListenerConfig (com.hazelcast.config.ListenerConfig)4 Member (com.hazelcast.core.Member)3 AssertTask (com.hazelcast.test.AssertTask)3 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)3 MemberImpl (com.hazelcast.instance.MemberImpl)2 ExecutorService (java.util.concurrent.ExecutorService)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)2 ClusterService (com.hazelcast.internal.cluster.ClusterService)1 LocalTopicStatsImpl (com.hazelcast.monitor.impl.LocalTopicStatsImpl)1