@Test public void translateException_RuntimeException() throws Exception { Throwable originalException = new RuntimeException("stuff went wrong"); GrpcExceptionCallable<Integer, Integer> exceptionCallable = new GrpcExceptionCallable<>( failingCallable(originalException), Collections.<StatusCode.Code>emptySet()); try { exceptionCallable.call(0); fail("Expected exception to be thrown"); } catch (Throwable throwable) { assertInnerExceptionIsInstanceOf( throwable, UnknownException.class, NOT_RETRYABLE, originalException); } }
/** * Create a Unary callable object with minimal grpc-specific functionality. * * @param grpcCallSettings the gRPC call settings * @param callSettings the Unary call settings * @param clientContext {@link ClientContext} to use to connect to the service. */ public static <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createBaseUnaryCallable( GrpcCallSettings<RequestT, ResponseT> grpcCallSettings, UnaryCallSettings<?, ?> callSettings, ClientContext clientContext) { UnaryCallable<RequestT, ResponseT> callable = new GrpcDirectCallable<>(grpcCallSettings.getMethodDescriptor()); if (grpcCallSettings.getParamsExtractor() != null) { callable = new GrpcUnaryRequestParamCallable<>(callable, grpcCallSettings.getParamsExtractor()); } callable = new GrpcExceptionCallable<>(callable, callSettings.getRetryableCodes()); callable = Callables.retrying(callable, callSettings, clientContext); return callable; }
@Test public void translateException_cancelled() throws Exception { GrpcExceptionCallable<Integer, Integer> exceptionCallable = new GrpcExceptionCallable<>(inactiveCallable(), Collections.<StatusCode.Code>emptySet()); ApiFuture<Integer> result = exceptionCallable.futureCall(0); Truth.assertThat(result.isDone()).isFalse(); result.cancel(true); Truth.assertThat(result.isCancelled()).isTrue(); Truth.assertThat(result.isDone()).isTrue(); }
/** * Create a Unary callable object with minimal grpc-specific functionality. * * @param grpcCallSettings the gRPC call settings * @param callSettings the Unary call settings * @param clientContext {@link ClientContext} to use to connect to the service. */ public static <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createBaseUnaryCallable( GrpcCallSettings<RequestT, ResponseT> grpcCallSettings, UnaryCallSettings<?, ?> callSettings, ClientContext clientContext) { UnaryCallable<RequestT, ResponseT> callable = new GrpcDirectCallable<>(grpcCallSettings.getMethodDescriptor()); if (grpcCallSettings.getParamsExtractor() != null) { callable = new GrpcUnaryRequestParamCallable<>(callable, grpcCallSettings.getParamsExtractor()); } callable = new GrpcExceptionCallable<>(callable, callSettings.getRetryableCodes()); callable = Callables.retrying(callable, callSettings, clientContext); return callable; }
@Test public void translateException_StatusException_withRetry() throws Exception { Throwable originalException = new StatusException(Status.UNAVAILABLE); GrpcExceptionCallable<Integer, Integer> exceptionCallable = new GrpcExceptionCallable<>( failingCallable(originalException), Collections.singleton(Code.UNAVAILABLE)); try { exceptionCallable.call(0); fail("Expected exception to be thrown"); } catch (Throwable throwable) { assertInnerExceptionIsInstanceOf( throwable, UnavailableException.class, IS_RETRYABLE, originalException); } }
@Test public void translateException_StatusRuntimeException_noRetry() throws Exception { Throwable originalException = new StatusRuntimeException(Status.INVALID_ARGUMENT); GrpcExceptionCallable<Integer, Integer> exceptionCallable = new GrpcExceptionCallable<>( failingCallable(originalException), Collections.<StatusCode.Code>emptySet()); try { exceptionCallable.call(0); fail("Expected exception to be thrown"); } catch (Throwable throwable) { assertInnerExceptionIsInstanceOf( throwable, InvalidArgumentException.class, NOT_RETRYABLE, originalException); } }
@Test public void translateException_StatusException_noRetry() throws Exception { Throwable originalException = new StatusException(Status.INVALID_ARGUMENT); GrpcExceptionCallable<Integer, Integer> exceptionCallable = new GrpcExceptionCallable<>( failingCallable(originalException), Collections.<StatusCode.Code>emptySet()); try { exceptionCallable.call(0); fail("Expected exception to be thrown"); } catch (Throwable throwable) { assertInnerExceptionIsInstanceOf( throwable, InvalidArgumentException.class, NOT_RETRYABLE, originalException); } }
@Test public void translateException_ApiException() throws Exception { Throwable originalException = new RuntimeException("stuff went wrong"); Throwable apiException = new DataLossException( originalException, GrpcStatusCode.of(Status.Code.UNKNOWN), IS_RETRYABLE); GrpcExceptionCallable<Integer, Integer> exceptionCallable = new GrpcExceptionCallable<>( failingCallable(apiException), Collections.<StatusCode.Code>emptySet()); try { exceptionCallable.call(0); fail("Expected exception to be thrown"); } catch (Throwable throwable) { assertInnerExceptionIsInstanceOf( throwable, DataLossException.class, IS_RETRYABLE, originalException); } }
@Test public void translateException_StatusRuntimeException_withRetry() throws Exception { Throwable originalException = new StatusRuntimeException(Status.UNAVAILABLE); GrpcExceptionCallable<Integer, Integer> exceptionCallable = new GrpcExceptionCallable<>( failingCallable(originalException), Collections.singleton(Code.UNAVAILABLE)); try { exceptionCallable.call(0); fail("Expected exception to be thrown"); } catch (Throwable throwable) { assertInnerExceptionIsInstanceOf( throwable, UnavailableException.class, IS_RETRYABLE, originalException); } }