### Question:
HealthStatusManager { public void setStatus(String service, ServingStatus status) { checkNotNull(status, "status"); healthService.setStatus(service, status); } HealthStatusManager(); BindableService getHealthService(); void setStatus(String service, ServingStatus status); void clearStatus(String service); static final String SERVICE_NAME_ALL_SERVICES; }### Answer:
@Test public void checkValidStatus() throws Exception { manager.setStatus(SERVICE1, ServingStatus.NOT_SERVING); manager.setStatus(SERVICE2, ServingStatus.SERVING); HealthCheckRequest request = HealthCheckRequest.newBuilder().setService(SERVICE1).build(); HealthCheckResponse response = blockingStub.withDeadlineAfter(5, TimeUnit.SECONDS).check(request); assertThat(response).isEqualTo( HealthCheckResponse.newBuilder().setStatus(ServingStatus.NOT_SERVING).build()); request = HealthCheckRequest.newBuilder().setService(SERVICE2).build(); response = blockingStub.withDeadlineAfter(5, TimeUnit.SECONDS).check(request); assertThat(response).isEqualTo( HealthCheckResponse.newBuilder().setStatus(ServingStatus.SERVING).build()); assertThat(service.numWatchersForTest(SERVICE1)).isEqualTo(0); assertThat(service.numWatchersForTest(SERVICE2)).isEqualTo(0); }
@Test public void checkStatusNotFound() throws Exception { manager.setStatus(SERVICE1, ServingStatus.SERVING); HealthCheckRequest request = HealthCheckRequest.newBuilder().setService(SERVICE2).build(); try { blockingStub.withDeadlineAfter(5, TimeUnit.SECONDS).check(request); fail("Should've failed"); } catch (StatusRuntimeException e) { assertThat(e.getStatus().getCode()).isEqualTo(Status.Code.NOT_FOUND); } assertThat(service.numWatchersForTest(SERVICE2)).isEqualTo(0); }
@Test public void watchRemovedWhenClientCloses() throws Exception { CancellableContext withCancellation = Context.current().withCancellation(); Context prevCtx = withCancellation.attach(); RespObserver respObs1 = new RespObserver(); try { assertThat(service.numWatchersForTest(SERVICE1)).isEqualTo(0); stub.watch(HealthCheckRequest.newBuilder().setService(SERVICE1).build(), respObs1); } finally { withCancellation.detach(prevCtx); } RespObserver respObs1b = new RespObserver(); stub.watch(HealthCheckRequest.newBuilder().setService(SERVICE1).build(), respObs1b); RespObserver respObs2 = new RespObserver(); stub.watch(HealthCheckRequest.newBuilder().setService(SERVICE2).build(), respObs2); assertThat(respObs1.responses.poll()).isEqualTo( HealthCheckResponse.newBuilder().setStatus(ServingStatus.SERVICE_UNKNOWN).build()); assertThat(respObs1b.responses.poll()).isEqualTo( HealthCheckResponse.newBuilder().setStatus(ServingStatus.SERVICE_UNKNOWN).build()); assertThat(respObs2.responses.poll()).isEqualTo( HealthCheckResponse.newBuilder().setStatus(ServingStatus.SERVICE_UNKNOWN).build()); assertThat(service.numWatchersForTest(SERVICE1)).isEqualTo(2); assertThat(service.numWatchersForTest(SERVICE2)).isEqualTo(1); assertThat(respObs1.responses).isEmpty(); assertThat(respObs1b.responses).isEmpty(); assertThat(respObs2.responses).isEmpty(); withCancellation.close(); assertThat(respObs1.responses.poll()).isInstanceOf(Throwable.class); assertThat(service.numWatchersForTest(SERVICE1)).isEqualTo(1); assertThat(service.numWatchersForTest(SERVICE2)).isEqualTo(1); assertThat(respObs1.responses).isEmpty(); assertThat(respObs1b.responses).isEmpty(); assertThat(respObs2.responses).isEmpty(); }