Search in sources :

Example 1 with Membership

use of com.twitter.common.zookeeper.Group.Membership in project commons by twitter.

the class GroupTest method testJoinsAndWatchesSurviveExpiredSession.

@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);
    // Turn off expectations during ZK server shutdown.
    reset(onLoseMembership);
}
Also used : Membership(com.twitter.common.zookeeper.Group.Membership) EasyMockTest(com.twitter.common.testing.easymock.EasyMockTest) Test(org.junit.Test) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest)

Example 2 with Membership

use of com.twitter.common.zookeeper.Group.Membership in project commons by twitter.

the class GroupTest method testJoinsAndWatchesSurviveDisconnect.

@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();
    // and again for 2nd listener
    assertEmptyMembershipObserved();
    listener.assertEmpty();
    verify(onLoseMembership);
    // Turn off expectations during ZK server shutdown.
    reset(onLoseMembership);
}
Also used : Membership(com.twitter.common.zookeeper.Group.Membership) EasyMockTest(com.twitter.common.testing.easymock.EasyMockTest) Test(org.junit.Test) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest)

Example 3 with Membership

use of com.twitter.common.zookeeper.Group.Membership in project commons by twitter.

the class GroupTest method testUpdateMembershipData.

@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);
}
Also used : EasyMockTest(com.twitter.common.testing.easymock.EasyMockTest) Membership(com.twitter.common.zookeeper.Group.Membership) Supplier(com.twitter.common.base.Supplier) EasyMockTest(com.twitter.common.testing.easymock.EasyMockTest) Test(org.junit.Test) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest)

Example 4 with Membership

use of com.twitter.common.zookeeper.Group.Membership in project commons by twitter.

the class GroupTest method testNodeDeleteTriggersOnLoseMembership.

@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();
    // Will hang this test if onLoseMembership event is not propagated.
    lostMembership.await();
}
Also used : Command(com.twitter.common.base.Command) Membership(com.twitter.common.zookeeper.Group.Membership) CountDownLatch(java.util.concurrent.CountDownLatch) EasyMockTest(com.twitter.common.testing.easymock.EasyMockTest) Test(org.junit.Test) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest)

Example 5 with Membership

use of com.twitter.common.zookeeper.Group.Membership in project commons by twitter.

the class GroupTest method testStopWatching.

@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();
}
Also used : Membership(com.twitter.common.zookeeper.Group.Membership) EasyMockTest(com.twitter.common.testing.easymock.EasyMockTest) Test(org.junit.Test) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest)

Aggregations

Membership (com.twitter.common.zookeeper.Group.Membership)9 EasyMockTest (com.twitter.common.testing.easymock.EasyMockTest)7 BaseZooKeeperTest (com.twitter.common.zookeeper.testing.BaseZooKeeperTest)7 Test (org.junit.Test)7 Command (com.twitter.common.base.Command)3 Supplier (com.twitter.common.base.Supplier)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Supplier (com.google.common.base.Supplier)1 ExceptionalCommand (com.twitter.common.base.ExceptionalCommand)1 GroupChangeListener (com.twitter.common.zookeeper.Group.GroupChangeListener)1 JoinException (com.twitter.common.zookeeper.Group.JoinException)1 Status (com.twitter.thrift.Status)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1