Search in sources :

Example 31 with Member

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

the class ScheduledExecutorServiceBasicTest method capacity_onMember_whenPositiveLimit.

@Test
public void capacity_onMember_whenPositiveLimit() throws ExecutionException, InterruptedException {
    String schedulerName = "foobar";
    ScheduledExecutorConfig sec = new ScheduledExecutorConfig().setName(schedulerName).setDurability(1).setPoolSize(1).setCapacity(10);
    Config config = new Config().addScheduledExecutorConfig(sec);
    HazelcastInstance[] instances = createClusterWithCount(1, config);
    IScheduledExecutorService service = instances[0].getScheduledExecutorService(schedulerName);
    Member member = instances[0].getCluster().getLocalMember();
    for (int i = 0; i < 10; i++) {
        service.scheduleOnMember(new PlainCallableTask(), member, 0, TimeUnit.SECONDS);
    }
    try {
        service.scheduleOnMember(new PlainCallableTask(), member, 0, TimeUnit.SECONDS);
        fail("Should have been rejected.");
    } catch (RejectedExecutionException ex) {
        assertTrue("Got wrong RejectedExecutionException", ex.getMessage().equals("Maximum capacity of tasks reached."));
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ScheduledExecutorConfig(com.hazelcast.config.ScheduledExecutorConfig) Config(com.hazelcast.config.Config) ScheduledExecutorConfig(com.hazelcast.config.ScheduledExecutorConfig) Member(com.hazelcast.core.Member) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 32 with Member

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

the class TopicTest method testTopicGlobalOrder.

@Test
@SuppressWarnings("unchecked")
public void testTopicGlobalOrder() throws Exception {
    final int nodeCount = 5;
    final int count = 1000;
    final String randomTopicName = randomString();
    Config config = new Config();
    config.getTopicConfig(randomTopicName).setGlobalOrderingEnabled(true);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(nodeCount);
    final HazelcastInstance[] nodes = factory.newInstances(config);
    final List<TestMessage>[] messageListPerNode = new List[nodeCount];
    for (int i = 0; i < nodeCount; i++) {
        messageListPerNode[i] = new CopyOnWriteArrayList<TestMessage>();
    }
    final CountDownLatch messageLatch = new CountDownLatch(nodeCount * count);
    // add message listeners
    for (int i = 0; i < nodes.length; i++) {
        final int nodeIndex = i;
        ITopic<TestMessage> topic = nodes[i].getTopic(randomTopicName);
        topic.addMessageListener(new MessageListener<TestMessage>() {

            public void onMessage(Message<TestMessage> message) {
                messageListPerNode[nodeIndex].add(message.getMessageObject());
                messageLatch.countDown();
            }
        });
    }
    // publish messages
    for (HazelcastInstance node : nodes) {
        Member localMember = node.getCluster().getLocalMember();
        for (int j = 0; j < count; j++) {
            TestMessage message = new TestMessage(localMember, UUID.randomUUID().toString());
            ITopic<Object> topic = node.getTopic(randomTopicName);
            topic.publish(message);
        }
    }
    // all messages in nodes messageLists should be equal
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            int i = 0;
            do {
                assertEquals(messageListPerNode[i], messageListPerNode[i++]);
            } while (i < nodeCount);
        }
    });
}
Also used : ListenerConfig(com.hazelcast.config.ListenerConfig) Config(com.hazelcast.config.Config) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) HazelcastInstance(com.hazelcast.core.HazelcastInstance) AssertTask(com.hazelcast.test.AssertTask) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) Member(com.hazelcast.core.Member) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 33 with Member

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

the class TopicTest method testTopicMultiThreading.

@Test
@Category(NightlyTest.class)
@SuppressWarnings("unchecked")
public void testTopicMultiThreading() throws Exception {
    final int nodeCount = 5;
    final int count = 1000;
    final String randomTopicName = randomString();
    Config config = new Config();
    config.getTopicConfig(randomTopicName).setGlobalOrderingEnabled(false);
    config.getTopicConfig(randomTopicName).setMultiThreadingEnabled(true);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(nodeCount);
    final HazelcastInstance[] instances = factory.newInstances(config);
    final Set<String>[] threads = new Set[nodeCount];
    for (int i = 0; i < nodeCount; i++) {
        threads[i] = new HashSet<String>();
    }
    final CountDownLatch startLatch = new CountDownLatch(nodeCount);
    final CountDownLatch messageLatch = new CountDownLatch(nodeCount * nodeCount * count);
    final CountDownLatch publishLatch = new CountDownLatch(nodeCount * count);
    ExecutorService ex = Executors.newFixedThreadPool(nodeCount);
    for (int i = 0; i < nodeCount; i++) {
        final int finalI = i;
        ex.execute(new Runnable() {

            public void run() {
                final Set<String> thNames = threads[finalI];
                HazelcastInstance hz = instances[finalI];
                ITopic<TestMessage> topic = hz.getTopic(randomTopicName);
                topic.addMessageListener(new MessageListener<TestMessage>() {

                    public void onMessage(Message<TestMessage> message) {
                        thNames.add(Thread.currentThread().getName());
                        messageLatch.countDown();
                    }
                });
                startLatch.countDown();
                try {
                    startLatch.await(1, TimeUnit.MINUTES);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    return;
                }
                Member localMember = hz.getCluster().getLocalMember();
                for (int j = 0; j < count; j++) {
                    topic.publish(new TestMessage(localMember, UuidUtil.newUnsecureUuidString()));
                    publishLatch.countDown();
                }
            }
        });
    }
    try {
        assertTrue(publishLatch.await(2, TimeUnit.MINUTES));
        assertTrue(messageLatch.await(5, TimeUnit.MINUTES));
        boolean passed = false;
        for (int i = 0; i < nodeCount; i++) {
            if (threads[i].size() > 1) {
                passed = true;
            }
        }
        assertTrue("All listeners received messages in single thread. Expecting more threads involved", passed);
    } finally {
        ex.shutdownNow();
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ITopic(com.hazelcast.core.ITopic) Message(com.hazelcast.core.Message) ListenerConfig(com.hazelcast.config.ListenerConfig) Config(com.hazelcast.config.Config) MessageListener(com.hazelcast.core.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ExecutorService(java.util.concurrent.ExecutorService) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) Member(com.hazelcast.core.Member) Category(org.junit.experimental.categories.Category) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 34 with Member

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

the class TopicTest method testTopicLocalOrder.

@Test
@SuppressWarnings("unchecked")
public void testTopicLocalOrder() throws Exception {
    final int nodeCount = 5;
    final int count = 1000;
    final String randomTopicName = randomString();
    Config config = new Config();
    config.getTopicConfig(randomTopicName).setGlobalOrderingEnabled(false);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(nodeCount);
    final HazelcastInstance[] instances = factory.newInstances(config);
    final List<TestMessage>[] messageLists = new List[nodeCount];
    for (int i = 0; i < nodeCount; i++) {
        messageLists[i] = new CopyOnWriteArrayList<TestMessage>();
    }
    final CountDownLatch startLatch = new CountDownLatch(nodeCount);
    final CountDownLatch messageLatch = new CountDownLatch(nodeCount * nodeCount * count);
    final CountDownLatch publishLatch = new CountDownLatch(nodeCount * count);
    ExecutorService ex = Executors.newFixedThreadPool(nodeCount);
    for (int i = 0; i < nodeCount; i++) {
        final int finalI = i;
        ex.execute(new Runnable() {

            public void run() {
                final List<TestMessage> messages = messageLists[finalI];
                HazelcastInstance hz = instances[finalI];
                ITopic<TestMessage> topic = hz.getTopic(randomTopicName);
                topic.addMessageListener(new MessageListener<TestMessage>() {

                    public void onMessage(Message<TestMessage> message) {
                        messages.add(message.getMessageObject());
                        messageLatch.countDown();
                    }
                });
                startLatch.countDown();
                try {
                    startLatch.await(1, TimeUnit.MINUTES);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    return;
                }
                Member localMember = hz.getCluster().getLocalMember();
                for (int j = 0; j < count; j++) {
                    topic.publish(new TestMessage(localMember, UuidUtil.newUnsecureUuidString()));
                    publishLatch.countDown();
                }
            }
        });
    }
    try {
        assertTrue(publishLatch.await(2, TimeUnit.MINUTES));
        assertTrue(messageLatch.await(5, TimeUnit.MINUTES));
        TestMessage[] ref = new TestMessage[messageLists[0].size()];
        messageLists[0].toArray(ref);
        Comparator<TestMessage> comparator = new Comparator<TestMessage>() {

            public int compare(TestMessage m1, TestMessage m2) {
                // sort only publisher blocks. if publishers are the same, leave them as they are
                return m1.publisher.getUuid().compareTo(m2.publisher.getUuid());
            }
        };
        Arrays.sort(ref, comparator);
        for (int i = 1; i < nodeCount; i++) {
            TestMessage[] messages = new TestMessage[messageLists[i].size()];
            messageLists[i].toArray(messages);
            Arrays.sort(messages, comparator);
            assertArrayEquals(ref, messages);
        }
    } finally {
        ex.shutdownNow();
    }
}
Also used : ITopic(com.hazelcast.core.ITopic) Message(com.hazelcast.core.Message) ListenerConfig(com.hazelcast.config.ListenerConfig) Config(com.hazelcast.config.Config) MessageListener(com.hazelcast.core.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) Comparator(java.util.Comparator) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ExecutorService(java.util.concurrent.ExecutorService) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) Member(com.hazelcast.core.Member) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 35 with Member

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

the class HazelcastTestSupport method generateKeyOwnedBy.

/**
     * Generates a key according to given reference instance by checking partition ownership for it.
     *
     * @param instance         reference instance for key generation.
     * @param generateOwnedKey {@code true} if we want a key which is owned by the given instance, otherwise
     *                         set to {@code false} which means generated key will not be owned by the given instance.
     * @return generated string.
     */
public static String generateKeyOwnedBy(HazelcastInstance instance, boolean generateOwnedKey) {
    Cluster cluster = instance.getCluster();
    checkMemberCount(generateOwnedKey, cluster);
    checkPartitionCountGreaterOrEqualMemberCount(instance);
    Member localMember = cluster.getLocalMember();
    PartitionService partitionService = instance.getPartitionService();
    while (true) {
        String id = randomString();
        Partition partition = partitionService.getPartition(id);
        if (comparePartitionOwnership(generateOwnedKey, localMember, partition)) {
            return id;
        }
    }
}
Also used : Partition(com.hazelcast.core.Partition) IPartition(com.hazelcast.spi.partition.IPartition) Cluster(com.hazelcast.core.Cluster) PartitionService(com.hazelcast.core.PartitionService) InternalPartitionService(com.hazelcast.internal.partition.InternalPartitionService) Member(com.hazelcast.core.Member)

Aggregations

Member (com.hazelcast.core.Member)330 Test (org.junit.Test)134 QuickTest (com.hazelcast.test.annotation.QuickTest)124 ParallelTest (com.hazelcast.test.annotation.ParallelTest)113 HazelcastInstance (com.hazelcast.core.HazelcastInstance)99 Address (com.hazelcast.nio.Address)60 IExecutorService (com.hazelcast.core.IExecutorService)57 Future (java.util.concurrent.Future)47 ArrayList (java.util.ArrayList)45 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)41 CountDownLatch (java.util.concurrent.CountDownLatch)37 Config (com.hazelcast.config.Config)35 HazelcastTestSupport.randomString (com.hazelcast.test.HazelcastTestSupport.randomString)26 HashMap (java.util.HashMap)26 IMap (com.hazelcast.core.IMap)24 HashSet (java.util.HashSet)21 LinkedList (java.util.LinkedList)20 OperationService (com.hazelcast.spi.OperationService)19 AssertTask (com.hazelcast.test.AssertTask)19 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)18