Search in sources :

Example 6 with Message

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

the class ClientReliableTopicOnClusterRestartTest method shouldFail_OnClusterRestart_whenDataLoss_notLossTolerant.

@Test
public void shouldFail_OnClusterRestart_whenDataLoss_notLossTolerant() {
    Config config = smallInstanceConfig();
    String topicName = "topic";
    config.getRingbufferConfig(topicName).setCapacity(10);
    HazelcastInstance member = hazelcastFactory.newHazelcastInstance(config);
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.getConnectionStrategyConfig().getConnectionRetryConfig().setClusterConnectTimeoutMillis(Long.MAX_VALUE);
    HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
    AtomicLong messageCount = new AtomicLong();
    // initialises listener seq up to 10000
    for (int i = 0; i < 10_000; i++) {
        member.getReliableTopic(topicName).publish("message");
    }
    ITopic<String> topic = client.getReliableTopic(topicName);
    UUID registrationId = topic.addMessageListener(createListener(false, m -> messageCount.incrementAndGet()));
    member.shutdown();
    member = hazelcastFactory.newHazelcastInstance(config);
    HazelcastInstance finalMember = member;
    assertTrueEventually(() -> {
        // we require at least one new message to detect that the ringbuffer was recreated
        finalMember.getReliableTopic(topicName).publish("message");
        ClientReliableTopicProxy<?> proxy = (ClientReliableTopicProxy<?>) topic;
        assertTrue(proxy.isListenerCancelled(registrationId));
    });
    assertEquals(0, messageCount.get());
}
Also used : ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) Message(com.hazelcast.topic.Message) QuickTest(com.hazelcast.test.annotation.QuickTest) RunWith(org.junit.runner.RunWith) ClientProperty(com.hazelcast.client.properties.ClientProperty) HazelcastTestSupport.smallInstanceConfig(com.hazelcast.test.HazelcastTestSupport.smallInstanceConfig) ClientReliableTopicProxy(com.hazelcast.client.impl.proxy.ClientReliableTopicProxy) DurableMessageListener(com.hazelcast.topic.impl.reliable.DurableSubscriptionTest.DurableMessageListener) After(org.junit.After) HazelcastTestSupport.assertOpenEventually(com.hazelcast.test.HazelcastTestSupport.assertOpenEventually) ClientConfig(com.hazelcast.client.config.ClientConfig) TestHazelcastFactory(com.hazelcast.client.test.TestHazelcastFactory) ITopic(com.hazelcast.topic.ITopic) Config(com.hazelcast.config.Config) HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastTestSupport.assertTrueEventually(com.hazelcast.test.HazelcastTestSupport.assertTrueEventually) Test(org.junit.Test) UUID(java.util.UUID) Category(org.junit.experimental.categories.Category) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) TestCase.assertTrue(junit.framework.TestCase.assertTrue) Assert.assertFalse(org.junit.Assert.assertFalse) HazelcastParallelClassRunner(com.hazelcast.test.HazelcastParallelClassRunner) TestCase.assertEquals(junit.framework.TestCase.assertEquals) AtomicLong(java.util.concurrent.atomic.AtomicLong) HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastTestSupport.smallInstanceConfig(com.hazelcast.test.HazelcastTestSupport.smallInstanceConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) ClientReliableTopicProxy(com.hazelcast.client.impl.proxy.ClientReliableTopicProxy) ClientConfig(com.hazelcast.client.config.ClientConfig) UUID(java.util.UUID) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 7 with Message

use of com.hazelcast.topic.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.topic.ITopic) Message(com.hazelcast.topic.Message) MessageListener(com.hazelcast.topic.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) DurableSubscriptionTest(com.hazelcast.topic.impl.reliable.DurableSubscriptionTest) Test(org.junit.Test)

Example 8 with Message

use of com.hazelcast.topic.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) {
        }
    };
    UUID id = topic.addMessageListener(listener);
    assertTrue(topic.removeMessageListener(id));
}
Also used : ITopic(com.hazelcast.topic.ITopic) Message(com.hazelcast.topic.Message) MessageListener(com.hazelcast.topic.MessageListener) UUID(java.util.UUID) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 9 with Message

use of com.hazelcast.topic.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());
            assertNull(message.getPublishingMember());
            long actualPublishTime = message.getPublishTime();
            assertTrue(actualPublishTime >= beforePublishTime);
            assertTrue(actualPublishTime <= afterPublishTime);
        }
    });
}
Also used : ITopic(com.hazelcast.topic.ITopic) Message(com.hazelcast.topic.Message) ReliableMessageListenerMock(com.hazelcast.topic.impl.reliable.ReliableMessageListenerMock) AssertTask(com.hazelcast.test.AssertTask) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) DurableSubscriptionTest(com.hazelcast.topic.impl.reliable.DurableSubscriptionTest) Test(org.junit.Test)

Example 10 with Message

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

the class AllTest method loadTopicOperations.

private List<Runnable> loadTopicOperations() {
    ITopic topic = hazelcast.getTopic("myTopic");
    topic.addMessageListener(new MessageListener() {

        public void onMessage(Message message) {
            messagesReceived.incrementAndGet();
        }
    });
    List<Runnable> operations = new ArrayList<Runnable>();
    addOperation(operations, new Runnable() {

        public void run() {
            ITopic topic = hazelcast.getTopic("myTopic");
            topic.publish(String.valueOf(random.nextInt(100000000)));
            messagesSend.incrementAndGet();
        }
    }, 10);
    return operations;
}
Also used : ITopic(com.hazelcast.topic.ITopic) Message(com.hazelcast.topic.Message) MessageListener(com.hazelcast.topic.MessageListener) ArrayList(java.util.ArrayList)

Aggregations

Message (com.hazelcast.topic.Message)10 ITopic (com.hazelcast.topic.ITopic)8 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)7 QuickTest (com.hazelcast.test.annotation.QuickTest)7 Test (org.junit.Test)7 MessageListener (com.hazelcast.topic.MessageListener)5 UUID (java.util.UUID)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 ClientConfig (com.hazelcast.client.config.ClientConfig)3 ClientReliableTopicProxy (com.hazelcast.client.impl.proxy.ClientReliableTopicProxy)3 ClientProperty (com.hazelcast.client.properties.ClientProperty)3 TestHazelcastFactory (com.hazelcast.client.test.TestHazelcastFactory)3 Config (com.hazelcast.config.Config)3 HazelcastInstance (com.hazelcast.core.HazelcastInstance)3 HazelcastParallelClassRunner (com.hazelcast.test.HazelcastParallelClassRunner)3 HazelcastTestSupport.assertOpenEventually (com.hazelcast.test.HazelcastTestSupport.assertOpenEventually)3 HazelcastTestSupport.assertTrueEventually (com.hazelcast.test.HazelcastTestSupport.assertTrueEventually)3 HazelcastTestSupport.smallInstanceConfig (com.hazelcast.test.HazelcastTestSupport.smallInstanceConfig)3 DurableSubscriptionTest (com.hazelcast.topic.impl.reliable.DurableSubscriptionTest)3 DurableMessageListener (com.hazelcast.topic.impl.reliable.DurableSubscriptionTest.DurableMessageListener)3