Search in sources :

Example 1 with LeaderControl

use of com.twitter.common.zookeeper.SingletonService.LeaderControl in project commons by twitter.

the class SingletonServiceTest method testLeadMulti.

@Test
public void testLeadMulti() throws Exception {
    List<Capture<Leader>> leaderCaptures = Lists.newArrayList();
    List<Capture<LeaderControl>> leaderControlCaptures = Lists.newArrayList();
    for (int i = 0; i < 5; i++) {
        Capture<Leader> leaderCapture = new Capture<Leader>();
        leaderCaptures.add(leaderCapture);
        Capture<LeaderControl> controlCapture = createCapture();
        leaderControlCaptures.add(controlCapture);
        expect(candidate.offerLeadership(capture(leaderCapture))).andReturn(null);
        listener.onLeading(capture(controlCapture));
        InetSocketAddress primary = InetSocketAddress.createUnresolved("foo" + i, PORT_A);
        Map<String, InetSocketAddress> aux = ImmutableMap.of("http-admin", InetSocketAddress.createUnresolved("foo" + i, PORT_B));
        expect(serverSet.join(primary, aux)).andReturn(endpointStatus);
        endpointStatus.leave();
        abdicate.execute();
    }
    control.replay();
    for (int i = 0; i < 5; i++) {
        final String leaderName = "foo" + i;
        newLeader(leaderName, leaderCaptures.get(i));
        leaderControlCaptures.get(i).getValue().advertise();
        leaderControlCaptures.get(i).getValue().leave();
    }
}
Also used : DefeatOnDisconnectLeader(com.twitter.common.zookeeper.SingletonService.DefeatOnDisconnectLeader) Leader(com.twitter.common.zookeeper.Candidate.Leader) InetSocketAddress(java.net.InetSocketAddress) LeaderControl(com.twitter.common.zookeeper.SingletonService.LeaderControl) Capture(org.easymock.Capture) EasyMockTest.createCapture(com.twitter.common.testing.easymock.EasyMockTest.createCapture) Test(org.junit.Test) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest)

Example 2 with LeaderControl

use of com.twitter.common.zookeeper.SingletonService.LeaderControl in project commons by twitter.

the class SingletonServiceTest method testLeadJoinFailure.

@Test
public void testLeadJoinFailure() throws Exception {
    Capture<Leader> leaderCapture = new Capture<Leader>();
    expect(candidate.offerLeadership(capture(leaderCapture))).andReturn(null);
    Capture<LeaderControl> controlCapture = createCapture();
    listener.onLeading(capture(controlCapture));
    expectJoin().andThrow(new Group.JoinException("Injected join failure.", new Exception()));
    abdicate.execute();
    control.replay();
    newLeader("foo", leaderCapture);
    try {
        controlCapture.getValue().advertise();
        fail("Join should have failed.");
    } catch (JoinException e) {
    // Expected.
    }
    controlCapture.getValue().leave();
}
Also used : JoinException(com.twitter.common.zookeeper.Group.JoinException) JoinException(com.twitter.common.zookeeper.Group.JoinException) DefeatOnDisconnectLeader(com.twitter.common.zookeeper.SingletonService.DefeatOnDisconnectLeader) Leader(com.twitter.common.zookeeper.Candidate.Leader) LeaderControl(com.twitter.common.zookeeper.SingletonService.LeaderControl) Capture(org.easymock.Capture) EasyMockTest.createCapture(com.twitter.common.testing.easymock.EasyMockTest.createCapture) JoinException(com.twitter.common.zookeeper.Group.JoinException) IOException(java.io.IOException) Test(org.junit.Test) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest)

Example 3 with LeaderControl

use of com.twitter.common.zookeeper.SingletonService.LeaderControl in project commons by twitter.

the class SingletonServiceTest method testLeaderDisconnect.

@Test
public void testLeaderDisconnect() throws Exception {
    Capture<LeaderControl> controlCapture = createCapture();
    CountDownLatch leading = new CountDownLatch(1);
    listener.onLeading(capture(controlCapture));
    expectLastCall().andAnswer(countDownAnswer(leading));
    CountDownLatch defeated = new CountDownLatch(1);
    listener.onDefeated(null);
    expectLastCall().andAnswer(countDownAnswer(defeated));
    control.replay();
    ZooKeeperClient zkClient = createZkClient();
    serverSet = new ServerSetImpl(zkClient, "/fake/path");
    candidate = new CandidateImpl(new Group(zkClient, ZooKeeperUtils.OPEN_ACL_UNSAFE, "/fake/path"));
    DefeatOnDisconnectLeader leader = new DefeatOnDisconnectLeader(zkClient, listener);
    service = new SingletonService(serverSet, candidate);
    service.lead(InetSocketAddress.createUnresolved("foo", PORT_A), ImmutableMap.of("http-admin", InetSocketAddress.createUnresolved("foo", PORT_B)), leader);
    leading.await();
    shutdownNetwork();
    defeated.await();
}
Also used : DefeatOnDisconnectLeader(com.twitter.common.zookeeper.SingletonService.DefeatOnDisconnectLeader) CountDownLatch(java.util.concurrent.CountDownLatch) LeaderControl(com.twitter.common.zookeeper.SingletonService.LeaderControl) Test(org.junit.Test) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest)

Aggregations

DefeatOnDisconnectLeader (com.twitter.common.zookeeper.SingletonService.DefeatOnDisconnectLeader)3 LeaderControl (com.twitter.common.zookeeper.SingletonService.LeaderControl)3 BaseZooKeeperTest (com.twitter.common.zookeeper.testing.BaseZooKeeperTest)3 Test (org.junit.Test)3 EasyMockTest.createCapture (com.twitter.common.testing.easymock.EasyMockTest.createCapture)2 Leader (com.twitter.common.zookeeper.Candidate.Leader)2 Capture (org.easymock.Capture)2 JoinException (com.twitter.common.zookeeper.Group.JoinException)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 CountDownLatch (java.util.concurrent.CountDownLatch)1