Ejemplo n.º 1
0
  @Test
  public void testUpdateMembershipData() throws Exception {
    Supplier<byte[]> dataSupplier = new EasyMockTest.Clazz<Supplier<byte[]>>() {}.createMock();

    byte[] initial = "start".getBytes();
    expect(dataSupplier.get()).andReturn(initial);

    byte[] second = "update".getBytes();
    expect(dataSupplier.get()).andReturn(second);

    replay(dataSupplier);

    Membership membership = joinGroup.join(dataSupplier, onLoseMembership);
    assertArrayEquals(
        "Initial setting is incorrect.",
        initial,
        zkClient.get().getData(membership.getMemberPath(), false, null));

    assertArrayEquals(
        "Updating supplier should not change membership data",
        initial,
        zkClient.get().getData(membership.getMemberPath(), false, null));

    membership.updateMemberData();
    assertArrayEquals(
        "Updating membership should change data",
        second,
        zkClient.get().getData(membership.getMemberPath(), false, null));

    verify(dataSupplier);
  }
Ejemplo n.º 2
0
  @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.
  }
Ejemplo n.º 3
0
  @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.
  }
Ejemplo n.º 4
0
  @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);
  }
Ejemplo n.º 5
0
  @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.
  }
Ejemplo n.º 6
0
  @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();
  }