LeaderElectionService { public Boolean isLeader() { String localHostIp = localHostService.getIp(); while (!hasLeader() && !serverService.getAvailableServers().isEmpty()) { log.info("Elastic job: leader node is electing, waiting for 100 ms at server '{}'", localHostIp); BlockUtils.waitingShortTime(); } return localHostIp.equals(jobNodeStorage.getJobNodeData(ElectionNode.LEADER_HOST)); } LeaderElectionService(final CoordinatorRegistryCenter coordinatorRegistryCenter, final JobConfiguration jobConfiguration); void leaderForceElection(); void leaderElection(); Boolean isLeader(); boolean hasLeader(); void removeLeader();  }
@Test public void assertIsLeader() { when(jobNodeStorage.isJobNodeExisted("leader/election/host")).thenReturn(false, true); when(serverService.getAvailableServers()).thenReturn(Collections.singletonList("mockedIP")); when(jobNodeStorage.getJobNodeData("leader/election/host")).thenReturn("mockedIP"); assertTrue(leaderElectionService.isLeader()); }
@Test public void assertIsNotLeader() { when(jobNodeStorage.isJobNodeExisted("leader/election/host")).thenReturn(false); when(serverService.getAvailableServers()).thenReturn(Collections.<String>emptyList()); assertFalse(leaderElectionService.isLeader()); }