@@ -146,6 +146,37 @@ public void retry() {
146146 Truth .assertThat (callable .call (1 )).isEqualTo (2 );
147147 }
148148
149+ @ Test
150+ public void retryOnStatusUnknown () {
151+ ImmutableSet <Status .Code > retryable = ImmutableSet .<Status .Code >of (Status .Code .UNKNOWN );
152+ Throwable t = Status .UNKNOWN .asException ();
153+ Mockito .when (callInt .futureCall ((CallContext <Integer >)Mockito .any ()))
154+ .thenReturn (Futures .<Integer >immediateFailedFuture (t ))
155+ .thenReturn (Futures .<Integer >immediateFailedFuture (t ))
156+ .thenReturn (Futures .<Integer >immediateFailedFuture (t ))
157+ .thenReturn (Futures .<Integer >immediateFuture (2 ));
158+ ApiCallable <Integer , Integer > callable =
159+ ApiCallable .<Integer , Integer >create (callInt )
160+ .retryableOn (retryable )
161+ .retrying (testRetryParams , EXECUTOR , new FakeNanoClock (System .nanoTime ()));
162+ Truth .assertThat (callable .call (1 )).isEqualTo (2 );
163+ }
164+
165+ @ Test
166+ public void retryOnUnexpectedException () {
167+ thrown .expect (UncheckedExecutionException .class );
168+ thrown .expectMessage ("foobar" );
169+ ImmutableSet <Status .Code > retryable = ImmutableSet .<Status .Code >of (Status .Code .UNKNOWN );
170+ Throwable t = new RuntimeException ("foobar" );
171+ Mockito .when (callInt .futureCall ((CallContext <Integer >)Mockito .any ()))
172+ .thenReturn (Futures .<Integer >immediateFailedFuture (t ));
173+ ApiCallable <Integer , Integer > callable =
174+ ApiCallable .<Integer , Integer >create (callInt )
175+ .retryableOn (retryable )
176+ .retrying (testRetryParams , EXECUTOR , new FakeNanoClock (System .nanoTime ()));
177+ callable .call (1 );
178+ }
179+
149180 @ Test
150181 public void retryNoRecover () {
151182 thrown .expect (UncheckedExecutionException .class );
@@ -396,4 +427,46 @@ public void bundlingException() throws Exception {
396427 bundlerFactory .close ();
397428 }
398429 }
430+
431+ // ApiException
432+ // ============
433+
434+ @ Test
435+ public void testKnownStatusCode () {
436+ ImmutableSet <Status .Code > retryable = ImmutableSet .<Status .Code >of (Status .Code .UNAVAILABLE );
437+ Mockito .when (callInt .futureCall ((CallContext <Integer >)Mockito .any ()))
438+ .thenReturn (
439+ Futures .<Integer >immediateFailedFuture (
440+ Status .FAILED_PRECONDITION .withDescription ("known" ).asException ()));
441+ ApiCallable <Integer , Integer > callable =
442+ ApiCallable .<Integer , Integer >create (callInt )
443+ .retryableOn (retryable );
444+ try {
445+ callable .call (1 );
446+ } catch (UncheckedExecutionException exception ) {
447+ ApiException apiException = (ApiException ) exception .getCause ();
448+ Truth .assertThat (apiException .getStatusCode ()).isEqualTo (Status .Code .FAILED_PRECONDITION );
449+ Truth .assertThat (apiException .getMessage ()).isEqualTo (
450+ "io.grpc.StatusException: FAILED_PRECONDITION: known" );
451+ }
452+ }
453+
454+ @ Test
455+ public void testUnknownStatusCode () {
456+ ImmutableSet <Status .Code > retryable = ImmutableSet .<Status .Code >of ();
457+ Mockito .when (callInt .futureCall ((CallContext <Integer >)Mockito .any ()))
458+ .thenReturn (
459+ Futures .<Integer >immediateFailedFuture (
460+ new RuntimeException ("unknown" )));
461+ ApiCallable <Integer , Integer > callable =
462+ ApiCallable .<Integer , Integer >create (callInt )
463+ .retryableOn (retryable );
464+ try {
465+ callable .call (1 );
466+ } catch (UncheckedExecutionException exception ) {
467+ ApiException apiException = (ApiException ) exception .getCause ();
468+ Truth .assertThat (apiException .getStatusCode ()).isEqualTo (Status .Code .UNKNOWN );
469+ Truth .assertThat (apiException .getMessage ()).isEqualTo ("java.lang.RuntimeException: unknown" );
470+ }
471+ }
399472}
0 commit comments