@Test public void checkValidStatus() throws Exception { //setup manager.setStatus("", status); HealthCheckRequest request = HealthCheckRequest.newBuilder().setService("").build(); @SuppressWarnings("unchecked") StreamObserver<HealthCheckResponse> observer = mock(StreamObserver.class); //test health.check(request, observer); //verify InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onNext(any(HealthCheckResponse.class)); inOrder.verify(observer, times(1)).onCompleted(); verify(observer, never()).onError(any(Throwable.class)); }
@Test public void checkStatusNotFound() throws Exception { //setup manager.setStatus("", status); HealthCheckRequest request = HealthCheckRequest.newBuilder().setService("invalid").build(); @SuppressWarnings("unchecked") StreamObserver<HealthCheckResponse> observer = mock(StreamObserver.class); //test health.check(request, observer); //verify ArgumentCaptor<StatusException> exception = ArgumentCaptor.forClass(StatusException.class); verify(observer, times(1)).onError(exception.capture()); assertEquals(Status.Code.NOT_FOUND, exception.getValue().getStatus().getCode()); verify(observer, never()).onCompleted(); }
@Test public void notFoundForClearedStatus() throws Exception { //setup manager.setStatus("", status); manager.clearStatus(""); HealthCheckRequest request = HealthCheckRequest.newBuilder().setService("").build(); @SuppressWarnings("unchecked") StreamObserver<HealthCheckResponse> observer = mock(StreamObserver.class); //test health.check(request, observer); //verify ArgumentCaptor<StatusException> exception = ArgumentCaptor.forClass(StatusException.class); verify(observer, times(1)).onError(exception.capture()); assertEquals(Status.Code.NOT_FOUND, exception.getValue().getStatus().getCode()); verify(observer, never()).onCompleted(); }
@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 void run(String... args) throws Exception { log.info("Starting gRPC Server ..."); Collection<ServerInterceptor> globalInterceptors = getBeanNamesByTypeWithAnnotation(GRpcGlobalInterceptor.class, ServerInterceptor.class) .map(name -> applicationContext.getBeanFactory().getBean(name, ServerInterceptor.class)) .collect(Collectors.toList()); // Adding health service serverBuilder.addService(healthStatusManager.getHealthService()); // find and register all GRpcService-enabled beans getBeanNamesByTypeWithAnnotation(GRpcService.class, BindableService.class) .forEach(name -> { BindableService srv = applicationContext.getBeanFactory().getBean(name, BindableService.class); ServerServiceDefinition serviceDefinition = srv.bindService(); GRpcService gRpcServiceAnn = applicationContext.findAnnotationOnBean(name, GRpcService.class); serviceDefinition = bindInterceptors(serviceDefinition, gRpcServiceAnn, globalInterceptors); serverBuilder.addService(serviceDefinition); String serviceName = serviceDefinition.getServiceDescriptor().getName(); healthStatusManager.setStatus(serviceName, HealthCheckResponse.ServingStatus.SERVING); log.info("'{}' service has been registered.", srv.getClass().getName()); }); configurer.configure(serverBuilder); server = serverBuilder.build().start(); applicationContext.publishEvent(new GRpcServerInitializedEvent(server)); log.info("gRPC Server started, listening on port {}.", server.getPort()); startDaemonAwaitThread(); }
@Override public void check(HealthCheckRequest request, StreamObserver<HealthCheckResponse> responseObserver) { ServingStatus status = getStatus(request.getService()); if (status == null) { responseObserver.onError(new StatusException( Status.NOT_FOUND.withDescription("unknown service " + request.getService()))); } else { HealthCheckResponse response = HealthCheckResponse.newBuilder().setStatus(status).build(); responseObserver.onNext(response); responseObserver.onCompleted(); } }