### Question:
DeterministicShuffleShardSyncLeaderDecider implements LeaderDecider { @Override public synchronized Boolean isLeader(String workerId) { if (executeConditionCheckWithReadLock(() -> CollectionUtils.isNullOrEmpty(leaders))) { electLeaders(); leaderElectionThreadPool.scheduleWithFixedDelay(this::electLeaders, ELECTION_INITIAL_DELAY_MILLIS, ELECTION_SCHEDULING_INTERVAL_MILLIS, TimeUnit.MILLISECONDS); } return executeConditionCheckWithReadLock(() -> isWorkerLeaderForShardSync(workerId)); } DeterministicShuffleShardSyncLeaderDecider(LeaseRefresher leaseRefresher, ScheduledExecutorService leaderElectionThreadPool,
            int numPeriodicShardSyncWorkers); @Override synchronized Boolean isLeader(String workerId); @Override synchronized void shutdown();  }### Answer:
@Test public void testLeaderElectionWithNullLeases() { boolean isLeader = leaderDecider.isLeader(WORKER_ID); assertTrue("IsLeader should return true if leaders is null", isLeader); }
@Test public void testLeaderElectionWithEmptyLeases() throws Exception { when(leaseRefresher.listLeases()).thenReturn(new ArrayList<>()); boolean isLeader = leaderDecider.isLeader(WORKER_ID); assertTrue("IsLeader should return true if no leases are returned", isLeader); }
@Test public void testleaderElectionWithEmptyOwnerLeases() throws Exception { List<Lease> leases = getLeases(5, true, true, true); when(leaseRefresher.listLeases()).thenReturn(leases); boolean isLeader = leaderDecider.isLeader(WORKER_ID); assertTrue("IsLeader should return true if leases have no owner", isLeader); }
@Test public void testElectedLeadersAsPerExpectedShufflingOrder() throws Exception { List<Lease> leases = getLeases(5, false ,false , true ); when(leaseRefresher.listLeases()).thenReturn(leases); Set<String> expectedLeaders = getExpectedLeaders(leases); for (String leader : expectedLeaders) { assertTrue(leaderDecider.isLeader(leader)); } for (Lease lease : leases) { if (!expectedLeaders.contains(lease.leaseOwner())) { assertFalse(leaderDecider.isLeader(lease.leaseOwner())); } } }
@Test public void testElectedLeadersAsPerExpectedShufflingOrderWhenUniqueWorkersLessThanMaxLeaders() { this.numShardSyncWorkers = 5; leaderDecider = new DeterministicShuffleShardSyncLeaderDecider(leaseRefresher, scheduledExecutorService, numShardSyncWorkers); List<Lease> leases = getLeases(3, false , false , true ); Set<String> expectedLeaders = getExpectedLeaders(leases); for (Lease lease : leases) { assertTrue(leaderDecider.isLeader(lease.leaseOwner())); assertTrue(expectedLeaders.contains(lease.leaseOwner())); } }