ZkLeaderElector implements LeaderElector { @Override public boolean amILeader() { return isLeader.get(); } ZkLeaderElector(String processorIdStr, ZkUtils zkUtils); @VisibleForTesting  ZkLeaderElector(String processorIdStr,
                         ZkUtils zkUtils,
                         IZkDataListener previousProcessorChangeListener); @Override void setLeaderElectorListener(LeaderElectorListener listener); @Override void tryBecomeLeader(); @Override void resignLeadership(); @Override boolean amILeader(); static final Logger LOG; }
@Test public void testAmILeader() { BooleanResult isLeader1 = new BooleanResult(); BooleanResult isLeader2 = new BooleanResult(); ZkUtils zkUtils1 = getZkUtilsWithNewClient(); ZkLeaderElector leaderElector1 = new ZkLeaderElector("1", zkUtils1, null); leaderElector1.setLeaderElectorListener(() -> isLeader1.res = true); ZkUtils zkUtils2 = getZkUtilsWithNewClient(); ZkLeaderElector leaderElector2 = new ZkLeaderElector("2", zkUtils2, null); leaderElector2.setLeaderElectorListener(() -> isLeader2.res = true); Assert.assertFalse(leaderElector1.amILeader()); Assert.assertFalse(leaderElector2.amILeader()); leaderElector1.tryBecomeLeader(); leaderElector2.tryBecomeLeader(); Assert.assertTrue(leaderElector1.amILeader()); Assert.assertFalse(leaderElector2.amILeader()); zkUtils1.close(); zkUtils2.close(); }