Search in sources :

Example 1 with FakeStatusCode

use of com.google.api.gax.rpc.testing.FakeStatusCode in project gax-java by googleapis.

the class ApiResultRetryAlgorithmTest method testShouldRetryWithContextWithRetryableCodes.

@Test
public void testShouldRetryWithContextWithRetryableCodes() {
    ApiCallContext context = mock(ApiCallContext.class);
    when(context.getRetryableCodes()).thenReturn(Sets.newHashSet(StatusCode.Code.DEADLINE_EXCEEDED, StatusCode.Code.UNAVAILABLE));
    StatusCode unavailable = mock(StatusCode.class);
    when(unavailable.getCode()).thenReturn(Code.UNAVAILABLE);
    StatusCode dataLoss = mock(StatusCode.class);
    when(dataLoss.getCode()).thenReturn(Code.DATA_LOSS);
    // The return value of isRetryable() will be ignored, as UNAVAILABLE has been added as a
    // retryable code to the call context.
    ApiException unavailableException = new ApiException(null, new FakeStatusCode(Code.UNAVAILABLE), /* retryable = */
    false);
    ApiException dataLossException = new ApiException(null, new FakeStatusCode(Code.DATA_LOSS), /* retryable = */
    true);
    ApiResultRetryAlgorithm<String> algorithm = new ApiResultRetryAlgorithm<>();
    assertTrue(algorithm.shouldRetry(context, unavailableException, null));
    assertFalse(algorithm.shouldRetry(context, dataLossException, null));
}
Also used : FakeStatusCode(com.google.api.gax.rpc.testing.FakeStatusCode) FakeStatusCode(com.google.api.gax.rpc.testing.FakeStatusCode) Test(org.junit.Test)

Example 2 with FakeStatusCode

use of com.google.api.gax.rpc.testing.FakeStatusCode in project gax-java by googleapis.

the class ApiResultRetryAlgorithmTest method testShouldRetryNoContext.

@Test
public void testShouldRetryNoContext() {
    ApiException nonRetryable = new ApiException(null, new FakeStatusCode(Code.INTERNAL), /* retryable = */
    false);
    ApiException retryable = new ApiException(null, new FakeStatusCode(Code.UNAVAILABLE), /* retryable = */
    true);
    ApiResultRetryAlgorithm<String> algorithm = new ApiResultRetryAlgorithm<>();
    assertFalse(algorithm.shouldRetry(nonRetryable, null));
    assertTrue(algorithm.shouldRetry(retryable, null));
}
Also used : FakeStatusCode(com.google.api.gax.rpc.testing.FakeStatusCode) Test(org.junit.Test)

Example 3 with FakeStatusCode

use of com.google.api.gax.rpc.testing.FakeStatusCode in project gax-java by googleapis.

the class ApiResultRetryAlgorithmTest method testShouldRetryWithContextWithEmptyRetryableCodes.

@Test
public void testShouldRetryWithContextWithEmptyRetryableCodes() {
    ApiCallContext context = mock(ApiCallContext.class);
    // This will effectively make the RPC non-retryable.
    when(context.getRetryableCodes()).thenReturn(Collections.<Code>emptySet());
    ApiException unavailableException = new ApiException(null, new FakeStatusCode(Code.UNAVAILABLE), /* retryable = */
    true);
    ApiResultRetryAlgorithm<String> algorithm = new ApiResultRetryAlgorithm<>();
    assertFalse(algorithm.shouldRetry(context, unavailableException, null));
}
Also used : FakeStatusCode(com.google.api.gax.rpc.testing.FakeStatusCode) Test(org.junit.Test)

Example 4 with FakeStatusCode

use of com.google.api.gax.rpc.testing.FakeStatusCode in project gax-java by googleapis.

the class ApiResultRetryAlgorithmTest method testShouldRetryWithContextWithoutRetryableCodes.

@Test
public void testShouldRetryWithContextWithoutRetryableCodes() {
    ApiCallContext context = mock(ApiCallContext.class);
    // No retryable codes in the call context, means that the retry algorithm should fall back to
    // its default implementation.
    when(context.getRetryableCodes()).thenReturn(null);
    ApiException nonRetryable = new ApiException(null, new FakeStatusCode(Code.UNAVAILABLE), /* retryable = */
    false);
    ApiException retryable = new ApiException(null, new FakeStatusCode(Code.UNAVAILABLE), /* retryable = */
    true);
    ApiResultRetryAlgorithm<String> algorithm = new ApiResultRetryAlgorithm<>();
    assertFalse(algorithm.shouldRetry(context, nonRetryable, null));
    assertTrue(algorithm.shouldRetry(context, retryable, null));
}
Also used : FakeStatusCode(com.google.api.gax.rpc.testing.FakeStatusCode) Test(org.junit.Test)

Example 5 with FakeStatusCode

use of com.google.api.gax.rpc.testing.FakeStatusCode in project gax-java by googleapis.

the class OpencensusTracerTest method testRetriesExhaustedExample.

@Test
public void testRetriesExhaustedExample() {
    tracer.attemptStarted(0);
    tracer.connectionSelected("1");
    ApiException error0 = new DeadlineExceededException("deadline exceeded", null, new FakeStatusCode(Code.DEADLINE_EXCEEDED), false);
    tracer.attemptFailedRetriesExhausted(error0);
    tracer.operationFailed(error0);
    verify(span).addAnnotation("Attempts exhausted", ImmutableMap.of("attempt", AttributeValue.longAttributeValue(0), "status", AttributeValue.stringAttributeValue("DEADLINE_EXCEEDED"), "status message", AttributeValue.stringAttributeValue("deadline exceeded"), "connection", AttributeValue.stringAttributeValue("1")));
    verify(span).putAttributes(ImmutableMap.of("attempt count", AttributeValue.longAttributeValue(1)));
    verify(span).end(EndSpanOptions.builder().setStatus(Status.DEADLINE_EXCEEDED.withDescription("deadline exceeded")).build());
    verifyNoMoreInteractions(span);
}
Also used : FakeStatusCode(com.google.api.gax.rpc.testing.FakeStatusCode) DeadlineExceededException(com.google.api.gax.rpc.DeadlineExceededException) ApiException(com.google.api.gax.rpc.ApiException) Test(org.junit.Test)

Aggregations

FakeStatusCode (com.google.api.gax.rpc.testing.FakeStatusCode)11 Test (org.junit.Test)11 ApiException (com.google.api.gax.rpc.ApiException)5 DeadlineExceededException (com.google.api.gax.rpc.DeadlineExceededException)4 NotFoundException (com.google.api.gax.rpc.NotFoundException)2 Status (io.opencensus.trace.Status)2 ServerStreamingAttemptException (com.google.api.gax.retrying.ServerStreamingAttemptException)1 CancelledException (com.google.api.gax.rpc.CancelledException)1 Code (com.google.api.gax.rpc.StatusCode.Code)1 CanonicalCode (io.opencensus.trace.Status.CanonicalCode)1