@Test public void healthServing() throws Exception { // Generate a unique in-process server name. 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( // Create a client channel and register for automatic graceful shutdown. grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build())); HealthCheckResponse reply = blockingStub.check(HealthCheckRequest.newBuilder().build()); assertEquals(HealthCheckResponse.ServingStatus.SERVING, reply.getStatus()); }
@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(); } } }
@Test public void healthNotServing() throws Exception { // Generate a unique in-process server name. 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( // Create a client channel and register for automatic graceful shutdown. grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build())); HealthCheckResponse reply = blockingStub.check(HealthCheckRequest.newBuilder().build()); assertEquals(HealthCheckResponse.ServingStatus.NOT_SERVING, reply.getStatus()); }
@Test public void testHealthCheck() throws ExecutionException, InterruptedException { final HealthCheckRequest healthCheckRequest = HealthCheckRequest.newBuilder().setService(GreeterGrpc.getServiceDescriptor().getName()).build(); final HealthGrpc.HealthFutureStub healthFutureStub = HealthGrpc.newFutureStub(channel); final HealthCheckResponse.ServingStatus servingStatus = healthFutureStub.check(healthCheckRequest).get().getStatus(); assertNotNull(servingStatus); assertEquals(servingStatus, HealthCheckResponse.ServingStatus.SERVING); } }
@Test public void testHealthCheck() throws ExecutionException, InterruptedException { final HealthCheckRequest healthCheckRequest = HealthCheckRequest.newBuilder().setService(GreeterGrpc.getServiceDescriptor().getName()).build(); final HealthGrpc.HealthFutureStub healthFutureStub = HealthGrpc.newFutureStub(channel); final HealthCheckResponse.ServingStatus servingStatus = healthFutureStub.check(healthCheckRequest).get().getStatus(); assertNotNull(servingStatus); assertEquals(servingStatus, HealthCheckResponse.ServingStatus.SERVING); } }
@Override public CompletableFuture<Void> healthCheck() { ensureNotClosed(); CompletableFuture<HealthCheckResponse> completableFuture = new CompletableFuture<>(); StreamObserver<HealthCheckResponse> observer = createUnaryCallStreamObserverForCompletableFuture( completableFuture); tiller.getHealthStub().check(HealthCheckRequest.getDefaultInstance(), observer); return completableFuture.thenAccept(response -> { if (response.getStatus() != ServingStatus.SERVING) { throw new TillerClientException(String.format(HEALTH_CHECK_STATUS_MESSAGE_FORMAT, response.getStatus().toString())); } }); }
private HealthStub createHealthStubMock(ServingStatus servingStatus) { HealthStub healthStubMock = Mockito.mock(HealthStub.class); HealthCheckResponse responseMock = mock(HealthCheckResponse.class); when(responseMock.getStatus()).thenReturn(servingStatus); @SuppressWarnings("unchecked") ArgumentCaptor<StreamObserver<HealthCheckResponse>> captor = ArgumentCaptor .forClass(StreamObserver.class); Mockito.doAnswer(invocation -> { StreamObserver<HealthCheckResponse> streamObserver = captor.getValue(); streamObserver.onNext(responseMock); streamObserver.onCompleted(); return null; }).when(healthStubMock).check(any(HealthCheckRequest.class), captor.capture()); return healthStubMock; } }