FailoverService { public void failoverIfNecessary() { if (!needFailover()) { return; } jobNodeStorage.executeInLeader(FailoverNode.LATCH, new FailoverLeaderExecutionCallback()); } FailoverService(final CoordinatorRegistryCenter coordinatorRegistryCenter, final JobConfiguration jobConfiguration); void setCrashedFailoverFlag(final int item); void failoverIfNecessary(); void updateFailoverComplete(final List<Integer> items); List<Integer> getLocalHostFailoverItems(); List<Integer> getLocalHostTakeOffItems(); void removeFailoverInfo();  }
@Test public void assertFailoverIfUnnecessaryWhenItemsRootNodeNotExisted() { when(jobNodeStorage.isJobNodeExisted("leader/failover/items")).thenReturn(false); failoverService.failoverIfNecessary(); verify(jobNodeStorage).isJobNodeExisted("leader/failover/items"); verify(jobNodeStorage, times(0)).executeInLeader(eq("leader/failover/latch"), Matchers.<FailoverLeaderExecutionCallback>any()); }
@Test public void assertFailoverIfUnnecessaryWhenItemsRootNodeIsEmpty() { when(jobNodeStorage.isJobNodeExisted("leader/failover/items")).thenReturn(true); when(jobNodeStorage.getJobNodeChildrenKeys("leader/failover/items")).thenReturn(Collections.<String>emptyList()); failoverService.failoverIfNecessary(); verify(jobNodeStorage).isJobNodeExisted("leader/failover/items"); verify(jobNodeStorage).getJobNodeChildrenKeys("leader/failover/items"); verify(jobNodeStorage, times(0)).executeInLeader(eq("leader/failover/latch"), Matchers.<FailoverLeaderExecutionCallback>any()); }
@Test public void assertFailoverIfUnnecessaryWhenServerIsNotReady() { when(jobNodeStorage.isJobNodeExisted("leader/failover/items")).thenReturn(true); when(jobNodeStorage.getJobNodeChildrenKeys("leader/failover/items")).thenReturn(Arrays.asList("0", "1", "2")); when(serverService.isLocalhostServerReady()).thenReturn(false); failoverService.failoverIfNecessary(); verify(jobNodeStorage).isJobNodeExisted("leader/failover/items"); verify(jobNodeStorage).getJobNodeChildrenKeys("leader/failover/items"); verify(serverService).isLocalhostServerReady(); verify(jobNodeStorage, times(0)).executeInLeader(eq("leader/failover/latch"), Matchers.<FailoverLeaderExecutionCallback>any()); }
@Test public void assertFailoverIfNecessary() { when(jobNodeStorage.isJobNodeExisted("leader/failover/items")).thenReturn(true); when(jobNodeStorage.getJobNodeChildrenKeys("leader/failover/items")).thenReturn(Arrays.asList("0", "1", "2")); when(serverService.isLocalhostServerReady()).thenReturn(true); failoverService.failoverIfNecessary(); verify(jobNodeStorage).isJobNodeExisted("leader/failover/items"); verify(jobNodeStorage).getJobNodeChildrenKeys("leader/failover/items"); verify(serverService).isLocalhostServerReady(); verify(jobNodeStorage).executeInLeader(eq("leader/failover/latch"), Matchers.<FailoverLeaderExecutionCallback>any()); }