HealthServiceImpl extends HealthGrpc.HealthImplBase { @Override public void check(HealthCheckRequest request, StreamObserver<HealthCheckResponse> responseObserver) { try { if (healthCheck.check().get().isHealthy()) { responseObserver.onNext( HealthCheckResponse.newBuilder().setStatus(HealthCheckResponse.ServingStatus.SERVING).build() ); } else { responseObserver.onNext( HealthCheckResponse.newBuilder().setStatus(HealthCheckResponse.ServingStatus.NOT_SERVING).build() ); } } catch (Exception ex) { GRPC_HELPER.onError(responseObserver, ex); } finally { responseObserver.onCompleted(); } } @Inject  HealthServiceImpl(HealthCheckAggregator healthCheck); @Override void check(HealthCheckRequest request, StreamObserver<HealthCheckResponse> responseObserver);  }
@Test public void healthServing() throws Exception { String serverName = InProcessServerBuilder.generateName(); HealthCheckAggregator hca = mock(HealthCheckAggregator.class); CompletableFuture<HealthCheckStatus> hcsf = mock(CompletableFuture.class); HealthCheckStatus hcs = mock(HealthCheckStatus.class); when(hcs.isHealthy()).thenReturn(true); when(hcsf.get()).thenReturn(hcs); when(hca.check()).thenReturn(hcsf); HealthServiceImpl healthyService = new HealthServiceImpl(hca); addService(serverName, healthyService); HealthGrpc.HealthBlockingStub blockingStub = HealthGrpc.newBlockingStub( grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build())); HealthCheckResponse reply = blockingStub.check(HealthCheckRequest.newBuilder().build()); assertEquals(HealthCheckResponse.ServingStatus.SERVING, reply.getStatus()); }
@Test public void healthNotServing() throws Exception { String serverName = InProcessServerBuilder.generateName(); HealthCheckAggregator hca = mock(HealthCheckAggregator.class); CompletableFuture<HealthCheckStatus> hcsf = mock(CompletableFuture.class); HealthCheckStatus hcs = mock(HealthCheckStatus.class); when(hcs.isHealthy()).thenReturn(false); when(hcsf.get()).thenReturn(hcs); when(hca.check()).thenReturn(hcsf); HealthServiceImpl healthyService = new HealthServiceImpl(hca); addService(serverName, healthyService); HealthGrpc.HealthBlockingStub blockingStub = HealthGrpc.newBlockingStub( grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build())); HealthCheckResponse reply = blockingStub.check(HealthCheckRequest.newBuilder().build()); assertEquals(HealthCheckResponse.ServingStatus.NOT_SERVING, reply.getStatus()); }
@Test public void healthException() throws Exception { String serverName = InProcessServerBuilder.generateName(); HealthCheckAggregator hca = mock(HealthCheckAggregator.class); CompletableFuture<HealthCheckStatus> hcsf = mock(CompletableFuture.class); when(hcsf.get()).thenThrow(InterruptedException.class); when(hca.check()).thenReturn(hcsf); HealthServiceImpl healthyService = new HealthServiceImpl(hca); addService(serverName, healthyService); HealthGrpc.HealthBlockingStub blockingStub = HealthGrpc.newBlockingStub( grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build())); thrown.expect(StatusRuntimeException.class); thrown.expect(hasProperty("status", is(Status.INTERNAL))); blockingStub.check(HealthCheckRequest.newBuilder().build()); }