@Test public void testEmptyMembership() throws Exception { ZooKeeperClient zkClient1 = createZkClient(TIMEOUT); final CandidateImpl candidate1 = new CandidateImpl(createGroup(zkClient1)); Reign candidate1Reign = new Reign("1", candidate1); candidate1.offerLeadership(candidate1Reign); assertSame(candidate1, candidateBuffer.takeLast()); candidate1Reign.abdicate(); assertFalse(candidate1.getLeaderData().isPresent()); }
@Test public void testOfferLeadership() throws Exception { ZooKeeperClient zkClient1 = createZkClient(TIMEOUT); final CandidateImpl candidate1 = new CandidateImpl(createGroup(zkClient1)) { @Override public String toString() { return "Leader1"; } }; ZooKeeperClient zkClient2 = createZkClient(TIMEOUT); final CandidateImpl candidate2 = new CandidateImpl(createGroup(zkClient2)) { @Override public String toString() { return "Leader2"; } }; ZooKeeperClient zkClient3 = createZkClient(TIMEOUT); final CandidateImpl candidate3 = new CandidateImpl(createGroup(zkClient3)) { @Override public String toString() { return "Leader3"; } }; Reign candidate1Reign = new Reign("1", candidate1); Reign candidate2Reign = new Reign("2", candidate2); Reign candidate3Reign = new Reign("3", candidate3); Supplier<Boolean> candidate1Leader = candidate1.offerLeadership(candidate1Reign); Supplier<Boolean> candidate2Leader = candidate2.offerLeadership(candidate2Reign); Supplier<Boolean> candidate3Leader = candidate3.offerLeadership(candidate3Reign); assertTrue( "Since initial group join is synchronous, candidate 1 should be the first leader", candidate1Leader.get()); shutdownNetwork(); restartNetwork(); assertTrue( "A re-connect without a session expiration should leave the leader elected", candidate1Leader.get()); candidate1Reign.abdicate(); assertSame(candidate1, candidateBuffer.takeLast()); assertFalse(candidate1Leader.get()); // Active abdication should trigger defeat. candidate1Reign.expectDefeated(); CandidateImpl secondCandidate = candidateBuffer.takeLast(); assertTrue( "exactly 1 remaining candidate should now be leader: " + secondCandidate + " " + candidateBuffer, candidate2Leader.get() ^ candidate3Leader.get()); if (secondCandidate == candidate2) { expireSession(zkClient2); assertSame(candidate3, candidateBuffer.takeLast()); assertTrue(candidate3Leader.get()); // Passive expiration should trigger defeat. candidate2Reign.expectDefeated(); } else { expireSession(zkClient3); assertSame(candidate2, candidateBuffer.takeLast()); assertTrue(candidate2Leader.get()); // Passive expiration should trigger defeat. candidate3Reign.expectDefeated(); } }