Search in sources :

Example 11 with ITopic

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

the class ReliableTopicCreateTest method testConfiguredListenerClassAndHazelcastInstanceAware.

@Test
public void testConfiguredListenerClassAndHazelcastInstanceAware() {
    Config config = new Config();
    config.addReliableTopicConfig(new ReliableTopicConfig("foo*").addMessageListenerConfig(new ListenerConfig(InstanceAwareReliableMessageListenerMock.class.getName())));
    HazelcastInstance hz = createHazelcastInstance(config);
    ITopic topic = hz.getReliableTopic("foo");
    ReliableTopicProxy proxy = assertInstanceOf(ReliableTopicProxy.class, topic);
    // check there is one listener.
    assertEquals(1, proxy.runnersMap.size());
    // check that the listener is of the right class.
    MessageRunner runner = (MessageRunner) proxy.runnersMap.values().iterator().next();
    InstanceAwareReliableMessageListenerMock mock = assertInstanceOf(InstanceAwareReliableMessageListenerMock.class, runner.listener);
    assertNotNull(mock.hz);
}
Also used : ListenerConfig(com.hazelcast.config.ListenerConfig) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ITopic(com.hazelcast.topic.ITopic) ReliableTopicConfig(com.hazelcast.config.ReliableTopicConfig) ReliableTopicConfig(com.hazelcast.config.ReliableTopicConfig) ListenerConfig(com.hazelcast.config.ListenerConfig) RingbufferConfig(com.hazelcast.config.RingbufferConfig) Config(com.hazelcast.config.Config) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 12 with ITopic

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

the class ClientReliableTopicOnClusterRestartTest method shouldContinue_OnClusterRestart_afterInvocationTimeout.

@Test
public void shouldContinue_OnClusterRestart_afterInvocationTimeout() throws InterruptedException {
    HazelcastInstance member = hazelcastFactory.newHazelcastInstance(smallInstanceConfig());
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.getConnectionStrategyConfig().getConnectionRetryConfig().setClusterConnectTimeoutMillis(Long.MAX_VALUE);
    int invocationTimeoutSeconds = 2;
    clientConfig.setProperty(ClientProperty.INVOCATION_TIMEOUT_SECONDS.getName(), String.valueOf(invocationTimeoutSeconds));
    HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
    final CountDownLatch messageArrived = new CountDownLatch(1);
    String topicName = "topic";
    ITopic<String> topic = client.getReliableTopic(topicName);
    UUID registrationId = topic.addMessageListener(createListener(true, m -> messageArrived.countDown()));
    member.shutdown();
    // wait for the topic operation to timeout
    Thread.sleep(TimeUnit.SECONDS.toMillis(invocationTimeoutSeconds));
    member = hazelcastFactory.newHazelcastInstance(smallInstanceConfig());
    member.getReliableTopic(topicName).publish("message");
    assertOpenEventually(messageArrived);
    ClientReliableTopicProxy<?> proxy = (ClientReliableTopicProxy<?>) topic;
    assertFalse(proxy.isListenerCancelled(registrationId));
}
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) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ClientReliableTopicProxy(com.hazelcast.client.impl.proxy.ClientReliableTopicProxy) ClientConfig(com.hazelcast.client.config.ClientConfig) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 13 with ITopic

use of com.hazelcast.topic.ITopic 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 14 with ITopic

use of com.hazelcast.topic.ITopic 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 15 with ITopic

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

the class ClientReliableTopicTest method testAlwaysStartAfterTail.

// makes sure that when a listener is register, we don't see any messages being published before
// it got registered. We'll only see the messages after it got registered.
@Test
public void testAlwaysStartAfterTail() {
    final ITopic topic = client.getReliableTopic(randomString());
    topic.publish("1");
    topic.publish("2");
    topic.publish("3");
    spawn(new Runnable() {

        @Override
        public void run() {
            sleepSeconds(5);
            topic.publish("4");
            topic.publish("5");
            topic.publish("6");
        }
    });
    final ReliableMessageListenerMock listener = new ReliableMessageListenerMock();
    topic.addMessageListener(listener);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(asList("4", "5", "6"), Arrays.asList(listener.objects.toArray()));
        }
    });
}
Also used : ITopic(com.hazelcast.topic.ITopic) 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)

Aggregations

ITopic (com.hazelcast.topic.ITopic)20 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)19 QuickTest (com.hazelcast.test.annotation.QuickTest)19 Test (org.junit.Test)19 DurableSubscriptionTest (com.hazelcast.topic.impl.reliable.DurableSubscriptionTest)12 UUID (java.util.UUID)8 ReliableMessageListenerMock (com.hazelcast.topic.impl.reliable.ReliableMessageListenerMock)7 AssertTask (com.hazelcast.test.AssertTask)6 Message (com.hazelcast.topic.Message)6 Config (com.hazelcast.config.Config)5 HazelcastInstance (com.hazelcast.core.HazelcastInstance)5 MessageListener (com.hazelcast.topic.MessageListener)5 ClientConfig (com.hazelcast.client.config.ClientConfig)3 ClientProperty (com.hazelcast.client.properties.ClientProperty)3 TestHazelcastFactory (com.hazelcast.client.test.TestHazelcastFactory)3 ListenerConfig (com.hazelcast.config.ListenerConfig)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ClientReliableTopicProxy (com.hazelcast.client.impl.proxy.ClientReliableTopicProxy)2 ReliableTopicConfig (com.hazelcast.config.ReliableTopicConfig)2 RingbufferConfig (com.hazelcast.config.RingbufferConfig)2