AggregatingHealthService implements HealthService { private ClientCall<HealthCheckResponse> check() { return (client, responseObserver) -> wrap(client).check(HealthCheckRequest.newBuilder().build(), responseObserver); } @Inject  AggregatingHealthService(AggregatingCellClient client, CallMetadataResolver callMetadataResolver, GrpcConfiguration grpcConfiguration); @Override Observable<HealthCheckResponse> check(HealthCheckRequest request);  }
@Test public void allCellsOK() { cellOne.getServiceRegistry().addService(new CellWithHealthStatus(ok("one"))); cellTwo.getServiceRegistry().addService(new CellWithHealthStatus(ok("two"))); AssertableSubscriber<HealthCheckResponse> subscriber = service.check(HealthCheckRequest.newBuilder().build()).test(); subscriber.awaitTerminalEvent(10, TimeUnit.SECONDS); subscriber.assertNoErrors(); subscriber.assertValueCount(1); HealthCheckResponse response = subscriber.getOnNextEvents().get(0); assertThat(response.getStatus()).isEqualTo(SERVING); assertThat(response.getDetailsCount()).isEqualTo(2); assertThat(response.getDetails(0).hasDetails()).isTrue(); assertThat(response.getDetails(1).hasDetails()).isTrue(); Set<String> cellsSeen = response.getDetailsList().stream() .map(s -> s.getDetails().getCell()) .collect(Collectors.toSet()); assertThat(cellsSeen).contains("one", "two"); }
@Test public void singleCell() { reset(connector); when(connector.getChannels()).thenReturn(Collections.singletonMap( new Cell("one", "1"), cellOne.getChannel() )); when(connector.getChannelForCell(any())).thenReturn(Optional.of(cellOne.getChannel())); HealthCheckResponse one = HealthCheckResponse.newBuilder() .setStatus(NOT_SERVING) .addDetails(ServerStatus.newBuilder() .setDetails(Details.newBuilder() .setCell("one") .setLeader(false) .setActive(true) .setUptime(Durations.fromMillis(clock.wallTime())) .build()) .build()) .build(); cellOne.getServiceRegistry().addService(new CellWithHealthStatus(one)); AssertableSubscriber<HealthCheckResponse> subscriber = service.check(HealthCheckRequest.newBuilder().build()).test(); subscriber.awaitTerminalEvent(10, TimeUnit.SECONDS); subscriber.assertNoErrors(); subscriber.assertValueCount(1); HealthCheckResponse response = subscriber.getOnNextEvents().get(0); assertThat(response.getStatus()).isEqualTo(NOT_SERVING); assertThat(response.getDetailsCount()).isEqualTo(1); assertThat(response.getDetails(0).hasDetails()).isTrue(); assertThat(response.getDetails(0).getDetails().getCell()).isEqualTo("one"); assertThat(response.getDetails(0).getDetails().getLeader()).isFalse(); }
@Test public void oneCellFailing() { cellOne.getServiceRegistry().addService(new CellWithHealthStatus(ok("one"))); cellTwo.getServiceRegistry().addService(new CellWithHealthStatus(failing("two"))); AssertableSubscriber<HealthCheckResponse> subscriber = service.check(HealthCheckRequest.newBuilder().build()).test(); subscriber.awaitTerminalEvent(10, TimeUnit.SECONDS); subscriber.assertNoErrors(); subscriber.assertValueCount(1); HealthCheckResponse response = subscriber.getOnNextEvents().get(0); assertThat(response.getStatus()).isEqualTo(NOT_SERVING); assertThat(response.getDetailsCount()).isEqualTo(2); assertThat(response.getDetails(0).hasDetails()).isTrue(); assertThat(response.getDetails(1).hasDetails()).isTrue(); Set<String> cellsSeen = response.getDetailsList().stream() .map(s -> s.getDetails().getCell()) .collect(Collectors.toSet()); assertThat(cellsSeen).contains("one", "two"); }
@Test public void grpcErrors() { cellOne.getServiceRegistry().addService(new CellWithHealthStatus(ok("one"))); cellTwo.getServiceRegistry().addService(new HealthGrpc.HealthImplBase() { @Override public void check(HealthCheckRequest request, StreamObserver<HealthCheckResponse> responseObserver) { responseObserver.onError(Status.DEADLINE_EXCEEDED.asRuntimeException()); } }); AssertableSubscriber<HealthCheckResponse> subscriber = service.check(HealthCheckRequest.newBuilder().build()).test(); subscriber.awaitTerminalEvent(10, TimeUnit.SECONDS); subscriber.assertNoErrors(); subscriber.assertValueCount(1); HealthCheckResponse response = subscriber.getOnNextEvents().get(0); assertThat(response.getStatus()).isEqualTo(NOT_SERVING); assertThat(response.getDetailsCount()).isEqualTo(2); List<ServerStatus> errors = response.getDetailsList().stream() .filter(ServerStatus::hasError) .collect(Collectors.toList()); assertThat(errors).hasSize(1); assertThat(errors.get(0).getError().getCell()).isEqualTo("two"); assertThat(errors.get(0).getError().getErrorCode()).isEqualTo(DEADLINE_EXCEEDED.toString()); }
@Test public void allCellsFailing() { cellOne.getServiceRegistry().addService(new CellWithHealthStatus(failing("one"))); cellTwo.getServiceRegistry().addService(new CellWithHealthStatus(failing("two"))); AssertableSubscriber<HealthCheckResponse> subscriber = service.check(HealthCheckRequest.newBuilder().build()).test(); subscriber.awaitTerminalEvent(10, TimeUnit.SECONDS); subscriber.assertNoErrors(); subscriber.assertValueCount(1); HealthCheckResponse response = subscriber.getOnNextEvents().get(0); assertThat(response.getStatus()).isEqualTo(NOT_SERVING); assertThat(response.getDetailsCount()).isEqualTo(2); assertThat(response.getDetails(0).hasDetails()).isTrue(); assertThat(response.getDetails(1).hasDetails()).isTrue(); Set<String> cellsSeen = response.getDetailsList().stream() .map(s -> s.getDetails().getCell()) .collect(Collectors.toSet()); assertThat(cellsSeen).contains("one", "two"); }