DistributedStoreCheck extends HealthCheck { @Override protected Result check() { try { ListenableFuture<Void> nopFuture = distributedStore.nop(); nopFuture.get(TIMEOUT, TIMEOUT_TIME_UNIT); return Result.healthy("server is leader"); } catch (TimeoutException e) { return Result.unhealthy(String.format("timed out after attempting to reach cluster for %d %s", TIMEOUT, TIMEOUT_TIME_UNIT)); } catch (ExecutionException e) { if (e.getCause() instanceof NotLeaderException) { NotLeaderException leaderException = (NotLeaderException) e.getCause(); String leader = leaderException.getLeader(); if (leader != null) { return Result.healthy(String.format("server not leader, but cluster has leader: %s", leader)); } else { return Result.unhealthy("cluster does not have a leader"); } } else { return getDefaultUnhealthyResult(e.getCause()); } } catch (Exception e) { return getDefaultUnhealthyResult(e); } } DistributedStoreCheck(DistributedStore distributedStore);   }
@Test public void shouldReturnOKResultIfServerIsLeader() { when(distributedStore.nop()).thenReturn(Futures.<Void>immediateFuture(null)); assertThat(check.check().isHealthy(), equalTo(true)); }
@Test public void shouldReturnOKResultIfServerIsPartOfClusterAndLeaderIsKnown() { when(distributedStore.nop()).thenReturn(Futures.<Void, NotLeaderException>immediateFailedCheckedFuture(new NotLeaderException("SELF", "LEADER"))); assertThat(check.check().isHealthy(), equalTo(true)); }
@Test public void shouldReturnUnhealthyResultIfServerIsPartOfClusterAndLeaderIsUnknown() { NotLeaderException notLeaderException = new NotLeaderException("SELF", null); when(distributedStore.nop()).thenReturn(Futures.<Void>immediateFailedFuture(notLeaderException)); assertThat(check.check().isHealthy(), equalTo(false)); assertThat(check.check().getError(), nullValue()); }
@Test public void shouldReturnUnhealthyResultIfCallToDistributedStoreThrowsException0() { IllegalStateException failureCause = new IllegalStateException("failed"); when(distributedStore.nop()).thenThrow(failureCause); assertThat(check.check().isHealthy(), equalTo(false)); assertThat(check.check().getError(), Matchers.<Throwable>sameInstance(failureCause)); }
@Test public void shouldReturnUnhealthyResultIfCallToDistributedStoreThrowsException1() { IllegalStateException failureCause = new IllegalStateException("failed"); when(distributedStore.nop()).thenReturn(Futures.<Void>immediateFailedFuture(failureCause)); assertThat(check.check().isHealthy(), equalTo(false)); assertThat(check.check().getError(), Matchers.<Throwable>sameInstance(failureCause)); }
@Test public void shouldReturnUnhealthyResultIfCallToDistributedStoreTimesOut() { when(distributedStore.nop()).thenReturn(SettableFuture.<Void>create()); assertThat(check.check().isHealthy(), equalTo(false)); }