use of com.hazelcast.core.MembershipListener in project hazelcast by hazelcast.
the class ClusterMembershipTest method testRemoveMembershipListener.
@Test
public void testRemoveMembershipListener() {
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
HazelcastInstance hz1 = factory.newHazelcastInstance();
Cluster cluster = hz1.getCluster();
MembershipListener membershipListener = mock(MembershipListener.class);
String id = cluster.addMembershipListener(membershipListener);
boolean removed = cluster.removeMembershipListener(id);
assertTrue(removed);
// now we add a member
HazelcastInstance hz2 = factory.newHazelcastInstance();
// and verify that the listener isn't called.
verify(membershipListener, never()).memberAdded(any(MembershipEvent.class));
}
use of com.hazelcast.core.MembershipListener in project hazelcast by hazelcast.
the class SplitBrainHandlerTest method testMergeAfterSplitBrain.
private void testMergeAfterSplitBrain(boolean multicast) throws InterruptedException {
String groupName = generateRandomString(10);
Config config = new Config();
config.setProperty(GroupProperty.MERGE_FIRST_RUN_DELAY_SECONDS.getName(), "5");
config.setProperty(GroupProperty.MERGE_NEXT_RUN_DELAY_SECONDS.getName(), "3");
config.getGroupConfig().setName(groupName);
NetworkConfig networkConfig = config.getNetworkConfig();
JoinConfig join = networkConfig.getJoin();
join.getMulticastConfig().setEnabled(multicast);
join.getTcpIpConfig().setEnabled(!multicast);
join.getTcpIpConfig().addMember("127.0.0.1");
HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config);
HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config);
HazelcastInstance h3 = Hazelcast.newHazelcastInstance(config);
final CountDownLatch splitLatch = new CountDownLatch(2);
h3.getCluster().addMembershipListener(new MembershipListener() {
@Override
public void memberAdded(MembershipEvent membershipEvent) {
}
@Override
public void memberRemoved(MembershipEvent membershipEvent) {
splitLatch.countDown();
}
@Override
public void memberAttributeChanged(MemberAttributeEvent memberAttributeEvent) {
}
});
final CountDownLatch mergeLatch = new CountDownLatch(1);
h3.getLifecycleService().addLifecycleListener(new MergedEventLifeCycleListener(mergeLatch));
closeConnectionBetween(h1, h3);
closeConnectionBetween(h2, h3);
assertTrue(splitLatch.await(10, TimeUnit.SECONDS));
assertEquals(2, h1.getCluster().getMembers().size());
assertEquals(2, h2.getCluster().getMembers().size());
assertEquals(1, h3.getCluster().getMembers().size());
assertTrue(mergeLatch.await(30, TimeUnit.SECONDS));
assertEquals(3, h1.getCluster().getMembers().size());
assertEquals(3, h2.getCluster().getMembers().size());
assertEquals(3, h3.getCluster().getMembers().size());
assertEquals(ACTIVE, h1.getCluster().getClusterState());
assertEquals(ACTIVE, h2.getCluster().getClusterState());
assertEquals(ACTIVE, h3.getCluster().getClusterState());
}
use of com.hazelcast.core.MembershipListener in project hazelcast by hazelcast.
the class QueueAdvancedTest method testDeadTaker.
/**
* Test for issue 730 (Google).
*/
@Test
public void testDeadTaker() throws Exception {
Config config = new Config();
final CountDownLatch shutdownLatch = new CountDownLatch(1);
config.addListenerConfig(new ListenerConfig().setImplementation(new MembershipListener() {
@Override
public void memberAdded(MembershipEvent membershipEvent) {
}
@Override
public void memberRemoved(MembershipEvent membershipEvent) {
shutdownLatch.countDown();
}
@Override
public void memberAttributeChanged(MemberAttributeEvent memberAttributeEvent) {
}
}));
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
HazelcastInstance[] instances = factory.newInstances(config);
final HazelcastInstance h1 = instances[0];
final HazelcastInstance h2 = instances[1];
warmUpPartitions(h1, h2);
final IQueue<String> q1 = h1.getQueue("default");
final IQueue<String> q2 = h2.getQueue("default");
final CountDownLatch startLatch = new CountDownLatch(1);
new Thread(new Runnable() {
@Override
public void run() {
try {
assertTrue("Expected startLatch.await() to succeed within 10 seconds", startLatch.await(10, SECONDS));
Thread.sleep(5000);
h2.getLifecycleService().terminate();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
startLatch.countDown();
String value = q2.take();
fail("Should not be able to take value from queue, but got: " + value);
} catch (HazelcastInstanceNotActiveException e) {
EmptyStatement.ignore(e);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
assertTrue("Expected shutdownLatch.await() to succeed within 1 minute", shutdownLatch.await(1, MINUTES));
q1.offer("item");
assertEquals(1, q1.size());
assertEquals("item", q1.poll());
}
use of com.hazelcast.core.MembershipListener in project hazelcast by hazelcast.
the class ClientMemberAttributeTest method testRemoveAttributes.
@Test(timeout = 120000)
public void testRemoveAttributes() throws Exception {
Config c = new Config();
JoinConfig join = c.getNetworkConfig().getJoin();
join.getTcpIpConfig().addMember("127.0.0.1").setEnabled(true);
join.getMulticastConfig().setEnabled(false);
HazelcastInstance h1 = hazelcastFactory.newHazelcastInstance(c);
Member m1 = h1.getCluster().getLocalMember();
m1.setIntAttribute("Test", 123);
HazelcastInstance h2 = hazelcastFactory.newHazelcastInstance(c);
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"));
HazelcastInstance client = hazelcastFactory.newHazelcastClient();
final CountDownLatch latch = new CountDownLatch(3);
final MembershipListener listener = new LatchMembershipListener(latch);
h2.getCluster().addMembershipListener(listener);
h1.getCluster().addMembershipListener(listener);
client.getCluster().addMembershipListener(listener);
m1.removeAttribute("Test");
// Force sleep to distribute value
assertOpenEventually(latch);
assertNull(member.getIntAttribute("Test"));
boolean found = false;
Collection<Member> members = client.getCluster().getMembers();
for (Member m : members) {
if (m.equals(m1)) {
assertNull(m.getIntAttribute("Test"));
found = true;
}
}
assertTrue(found);
}
use of com.hazelcast.core.MembershipListener in project hazelcast by hazelcast.
the class ClientMemberAttributeTest method testChangeAttributes.
@Test(timeout = 120000)
public void testChangeAttributes() throws Exception {
Config c = new Config();
JoinConfig join = c.getNetworkConfig().getJoin();
join.getTcpIpConfig().addMember("127.0.0.1").setEnabled(true);
join.getMulticastConfig().setEnabled(false);
HazelcastInstance h1 = hazelcastFactory.newHazelcastInstance(c);
Member m1 = h1.getCluster().getLocalMember();
m1.setIntAttribute("Test", 123);
HazelcastInstance h2 = hazelcastFactory.newHazelcastInstance(c);
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"));
HazelcastInstance client = hazelcastFactory.newHazelcastClient();
final CountDownLatch latch = new CountDownLatch(3);
final MembershipListener listener = new LatchMembershipListener(latch);
h2.getCluster().addMembershipListener(listener);
h1.getCluster().addMembershipListener(listener);
client.getCluster().addMembershipListener(listener);
m1.setIntAttribute("Test", 321);
// Force sleep to distribute value
assertOpenEventually(latch);
assertNotNull(member.getIntAttribute("Test"));
assertEquals(321, (int) member.getIntAttribute("Test"));
boolean found = false;
Collection<Member> members = client.getCluster().getMembers();
for (Member m : members) {
if (m.equals(m1)) {
assertEquals(321, (int) m.getIntAttribute("Test"));
found = true;
}
}
assertTrue(found);
}
Aggregations