use of io.atomix.core.election.Leadership in project atomix by atomix.
the class LeaderElectorTest method testWithdraw.
@Test
public void testWithdraw() throws Throwable {
AsyncLeaderElector<NodeId> elector1 = atomix().<NodeId>leaderElectorBuilder("test-elector-withdraw", protocol()).build().async();
elector1.run("foo", node1).join();
AsyncLeaderElector<NodeId> elector2 = atomix().<NodeId>leaderElectorBuilder("test-elector-withdraw", protocol()).build().async();
elector2.run("foo", node2).join();
LeaderEventListener listener1 = new LeaderEventListener();
elector1.addListener("foo", listener1).join();
LeaderEventListener listener2 = new LeaderEventListener();
elector2.addListener("foo", listener2).join();
LeaderEventListener listener3 = new LeaderEventListener();
elector1.addListener("bar", listener3);
elector2.addListener("bar", listener3);
elector1.withdraw("foo", node1).join();
listener1.nextEvent().thenAccept(result -> {
assertEquals(node2, result.newLeadership().leader().id());
assertEquals(2, result.newLeadership().leader().term());
assertEquals(1, result.newLeadership().candidates().size());
assertEquals(node2, result.newLeadership().candidates().get(0));
}).join();
listener2.nextEvent().thenAccept(result -> {
assertEquals(node2, result.newLeadership().leader().id());
assertEquals(2, result.newLeadership().leader().term());
assertEquals(1, result.newLeadership().candidates().size());
assertEquals(node2, result.newLeadership().candidates().get(0));
}).join();
assertFalse(listener3.hasEvent());
Leadership leadership1 = elector1.getLeadership("foo").join();
assertEquals(node2, leadership1.leader().id());
assertEquals(1, leadership1.candidates().size());
Leadership leadership2 = elector2.getLeadership("foo").join();
assertEquals(node2, leadership2.leader().id());
assertEquals(1, leadership2.candidates().size());
}
Aggregations