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);
}
});
}
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));
}
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));
}
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));
}
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();
}
}
Aggregations