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();
}
}
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();
}
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();
}
Aggregations