Search in sources :

Example 6 with MembershipListener

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

the class AbstractListenersOnReconnectTest method testListenersTerminateRandomNode.

private void testListenersTerminateRandomNode() {
    setupListener();
    terminateRandomNode();
    HazelcastClientInstanceImpl clientInstanceImpl = getHazelcastClientInstanceImpl(client);
    final CountDownLatch memberAddedLatch = new CountDownLatch(1);
    clientInstanceImpl.getClientClusterService().addMembershipListener(new MembershipListener() {

        @Override
        public void memberAdded(MembershipEvent membershipEvent) {
            memberAddedLatch.countDown();
        }

        @Override
        public void memberRemoved(MembershipEvent membershipEvent) {
        }

        @Override
        public void memberAttributeChanged(MemberAttributeEvent memberAttributeEvent) {
        }
    });
    factory.newHazelcastInstance();
    assertOpenEventually(memberAddedLatch);
    validateRegistrationsAndListenerFunctionality();
}
Also used : MemberAttributeEvent(com.hazelcast.core.MemberAttributeEvent) MembershipEvent(com.hazelcast.core.MembershipEvent) HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl) CountDownLatch(java.util.concurrent.CountDownLatch) MembershipListener(com.hazelcast.core.MembershipListener)

Example 7 with MembershipListener

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

the class ClusterMembershipTest method testAddMembershipListener_whenListenerRegisteredTwice.

@Test
public void testAddMembershipListener_whenListenerRegisteredTwice() {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance hz1 = factory.newHazelcastInstance();
    Cluster cluster = hz1.getCluster();
    final MembershipListener membershipListener = mock(MembershipListener.class);
    String id1 = cluster.addMembershipListener(membershipListener);
    String id2 = cluster.addMembershipListener(membershipListener);
    // first we check if the registration id's are different
    assertNotEquals(id1, id2);
    // an now we make sure that if a member joins the cluster, the same interface gets invoked twice.
    HazelcastInstance hz2 = factory.newHazelcastInstance();
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            //now we verify that the memberAdded method is called twice.
            verify(membershipListener, times(2)).memberAdded(any(MembershipEvent.class));
        }
    });
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Cluster(com.hazelcast.core.Cluster) AssertTask(com.hazelcast.test.AssertTask) InitialMembershipListener(com.hazelcast.core.InitialMembershipListener) MembershipListener(com.hazelcast.core.MembershipListener) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 8 with MembershipListener

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

the class MemberAttributeTest method testAddAttributes.

@Test(timeout = 120000)
public void testAddAttributes() throws Exception {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance h1 = factory.newHazelcastInstance();
    Member m1 = h1.getCluster().getLocalMember();
    m1.setIntAttribute("Test", 123);
    HazelcastInstance h2 = factory.newHazelcastInstance();
    assertEquals(2, h2.getCluster().getMembers().size());
    Member member = null;
    for (Member m : h2.getCluster().getMembers()) {
        if (m == h2.getCluster().getLocalMember())
            continue;
        member = m;
    }
    assertNotNull(member);
    assertEquals(m1, member);
    assertNotNull(member.getIntAttribute("Test"));
    assertEquals(123, (int) member.getIntAttribute("Test"));
    final CountDownLatch latch = new CountDownLatch(2);
    final MembershipListener listener = new LatchMembershipListener(latch);
    h2.getCluster().addMembershipListener(listener);
    h1.getCluster().addMembershipListener(listener);
    m1.setIntAttribute("Test2", 321);
    // Force sleep to distribute value
    assertOpenEventually(latch);
    assertNotNull(member.getIntAttribute("Test2"));
    assertEquals(321, (int) member.getIntAttribute("Test2"));
    h1.shutdown();
    h2.shutdown();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) CountDownLatch(java.util.concurrent.CountDownLatch) MembershipListener(com.hazelcast.core.MembershipListener) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) Member(com.hazelcast.core.Member) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 9 with MembershipListener

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

the class MemberAttributeTest method testRemoveAttributes.

@Test(timeout = 120000)
public void testRemoveAttributes() throws Exception {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance h1 = factory.newHazelcastInstance();
    Member m1 = h1.getCluster().getLocalMember();
    m1.setIntAttribute("Test", 123);
    HazelcastInstance h2 = factory.newHazelcastInstance();
    assertEquals(2, h2.getCluster().getMembers().size());
    Member member = null;
    for (Member m : h2.getCluster().getMembers()) {
        if (m == h2.getCluster().getLocalMember())
            continue;
        member = m;
    }
    assertNotNull(member);
    assertEquals(m1, member);
    assertNotNull(member.getIntAttribute("Test"));
    assertEquals(123, (int) member.getIntAttribute("Test"));
    final CountDownLatch latch = new CountDownLatch(2);
    final MembershipListener listener = new LatchMembershipListener(latch);
    h2.getCluster().addMembershipListener(listener);
    h1.getCluster().addMembershipListener(listener);
    m1.removeAttribute("Test");
    // Force sleep to distribute value
    assertOpenEventually(latch);
    assertNull(member.getIntAttribute("Test"));
    h1.shutdown();
    h2.shutdown();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) CountDownLatch(java.util.concurrent.CountDownLatch) MembershipListener(com.hazelcast.core.MembershipListener) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) Member(com.hazelcast.core.Member) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 10 with MembershipListener

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

the class MemberAttributeTest method testChangeAttributes.

@Test(timeout = 120000)
public void testChangeAttributes() throws Exception {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance h1 = factory.newHazelcastInstance();
    Member m1 = h1.getCluster().getLocalMember();
    m1.setIntAttribute("Test", 123);
    HazelcastInstance h2 = factory.newHazelcastInstance();
    assertEquals(2, h2.getCluster().getMembers().size());
    Member member = null;
    for (Member m : h2.getCluster().getMembers()) {
        if (m == h2.getCluster().getLocalMember())
            continue;
        member = m;
    }
    assertNotNull(member);
    assertEquals(m1, member);
    assertNotNull(member.getIntAttribute("Test"));
    assertEquals(123, (int) member.getIntAttribute("Test"));
    final CountDownLatch latch = new CountDownLatch(2);
    final MembershipListener listener = new LatchMembershipListener(latch);
    h2.getCluster().addMembershipListener(listener);
    h1.getCluster().addMembershipListener(listener);
    m1.setIntAttribute("Test", 321);
    // Force sleep to distribute value
    assertOpenEventually(latch);
    assertNotNull(member.getIntAttribute("Test"));
    assertEquals(321, (int) member.getIntAttribute("Test"));
    h1.shutdown();
    h2.shutdown();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) CountDownLatch(java.util.concurrent.CountDownLatch) MembershipListener(com.hazelcast.core.MembershipListener) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) Member(com.hazelcast.core.Member) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

MembershipListener (com.hazelcast.core.MembershipListener)12 HazelcastInstance (com.hazelcast.core.HazelcastInstance)10 ParallelTest (com.hazelcast.test.annotation.ParallelTest)9 QuickTest (com.hazelcast.test.annotation.QuickTest)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 Test (org.junit.Test)9 ListenerConfig (com.hazelcast.config.ListenerConfig)6 Member (com.hazelcast.core.Member)6 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)6 Config (com.hazelcast.config.Config)5 JoinConfig (com.hazelcast.config.JoinConfig)4 MembershipEvent (com.hazelcast.core.MembershipEvent)4 ClientConfig (com.hazelcast.client.config.ClientConfig)3 MemberAttributeConfig (com.hazelcast.config.MemberAttributeConfig)3 MemberAttributeEvent (com.hazelcast.core.MemberAttributeEvent)3 Cluster (com.hazelcast.core.Cluster)2 InitialMembershipListener (com.hazelcast.core.InitialMembershipListener)2 HazelcastClientInstanceImpl (com.hazelcast.client.impl.HazelcastClientInstanceImpl)1 NetworkConfig (com.hazelcast.config.NetworkConfig)1 ClientListener (com.hazelcast.core.ClientListener)1