@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. }
@Before public void mySetUp() throws Exception { onLoseMembership = createMock(Command.class); zkClient = createZkClient("group", "test"); joinGroup = new Group(zkClient, ZooKeeperUtils.EVERYONE_READ_CREATOR_ALL, "/a/group"); watchGroup = new Group(zkClient, ZooKeeperUtils.EVERYONE_READ_CREATOR_ALL, "/a/group"); listener = new RecordingListener(); stopWatching = watchGroup.watch(listener); }
@Test public void testAcls() throws Exception { Group securedMembership = new Group( createZkClient("secured", "group"), ZooKeeperUtils.EVERYONE_READ_CREATOR_ALL, "/secured/group/membership"); String memberId = securedMembership.join().getMemberId(); Group unauthenticatedObserver = new Group( createZkClient(Credentials.NONE), Ids.READ_ACL_UNSAFE, "/secured/group/membership"); RecordingListener unauthenticatedListener = new RecordingListener(); unauthenticatedObserver.watch(unauthenticatedListener); assertMembershipObserved(unauthenticatedListener, memberId); try { unauthenticatedObserver.join(); fail("Expected join exception for unauthenticated observer"); } catch (JoinException e) { // expected } Group unauthorizedObserver = new Group( createZkClient("joe", "schmoe"), Ids.READ_ACL_UNSAFE, "/secured/group/membership"); RecordingListener unauthorizedListener = new RecordingListener(); unauthorizedObserver.watch(unauthorizedListener); assertMembershipObserved(unauthorizedListener, memberId); try { unauthorizedObserver.join(); fail("Expected join exception for unauthorized observer"); } catch (JoinException e) { // expected } }
@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); }