@Test public void testJoinsAndWatchesSurviveExpiredSession() throws Exception { onLoseMembership.execute(); replay(onLoseMembership); assertEmptyMembershipObserved(); Membership membership = joinGroup.join(onLoseMembership); String originalMemberId = membership.getMemberId(); assertMembershipObserved(originalMemberId); expireSession(zkClient); // We should have lost our group membership and then re-gained it with a new ephemeral node. // We may or may-not see the intermediate state change but we must see the final state Iterable<String> members = listener.take(); if (Iterables.isEmpty(members)) { members = listener.take(); } assertEquals(1, Iterables.size(members)); assertNotEqual(originalMemberId, Iterables.getOnlyElement(members)); assertNotEqual(originalMemberId, membership.getMemberId()); listener.assertEmpty(); verify(onLoseMembership); reset(onLoseMembership); // Turn off expectations during ZK server shutdown. }
@Test public void testJoinsAndWatchesSurviveDisconnect() throws Exception { replay(onLoseMembership); assertEmptyMembershipObserved(); Membership membership = joinGroup.join(); String originalMemberId = membership.getMemberId(); assertMembershipObserved(originalMemberId); shutdownNetwork(); restartNetwork(); // The member should still be present under existing ephemeral node since session did not // expire. watchGroup.watch(listener); assertMembershipObserved(originalMemberId); membership.cancel(); assertEmptyMembershipObserved(); assertEmptyMembershipObserved(); // and again for 2nd listener listener.assertEmpty(); verify(onLoseMembership); reset(onLoseMembership); // Turn off expectations during ZK server shutdown. }
@Test public void testStopWatching() throws Exception { replay(onLoseMembership); assertEmptyMembershipObserved(); Membership member1 = joinGroup.join(); String memberId1 = member1.getMemberId(); assertMembershipObserved(memberId1); Membership member2 = joinGroup.join(); String memberId2 = member2.getMemberId(); assertMembershipObserved(memberId1, memberId2); stopWatching.execute(); member1.cancel(); Membership member3 = joinGroup.join(); member2.cancel(); member3.cancel(); listener.assertEmpty(); }
@Test public void testJoinCustomNamingScheme() throws Exception { Group group = new Group( zkClient, ZooKeeperUtils.EVERYONE_READ_CREATOR_ALL, "/a/group", new CustomScheme()); listener = new RecordingListener(); group.watch(listener); assertEmptyMembershipObserved(); Membership membership = group.join(); String memberId = membership.getMemberId(); assertEquals("Wrong member ID.", CustomScheme.NODE_NAME, memberId); assertMembershipObserved(memberId); expireSession(zkClient); }
@Test public void testNodeDeleteTriggersOnLoseMembership() throws Exception { final CountDownLatch lostMembership = new CountDownLatch(1); Command onLoseMembership = new Command() { @Override public void execute() throws RuntimeException { lostMembership.countDown(); } }; assertEmptyMembershipObserved(); Membership membership = joinGroup.join(onLoseMembership); assertMembershipObserved(membership.getMemberId()); membership.cancel(); lostMembership.await(); // Will hang this test if onLoseMembership event is not propagated. }