Search in sources :

Example 1 with ServerStreamingAttemptException

use of com.google.api.gax.retrying.ServerStreamingAttemptException in project gax-java by googleapis.

the class ServerStreamingAttemptCallableTest method testInitialRetry.

@Test
@SuppressWarnings("ConstantConditions")
public void testInitialRetry() {
    resumptionStrategy = new MyStreamResumptionStrategy();
    ServerStreamingAttemptCallable<String, String> callable = createCallable();
    callable.start();
    MockServerStreamingCall<String, String> call = innerCallable.popLastCall();
    // Send initial error
    FakeApiException initialError = new FakeApiException(null, Code.UNAVAILABLE, true);
    call.getController().getObserver().onError(initialError);
    // Should notify the outer future
    Throwable outerError = null;
    try {
        fakeRetryingFuture.getAttemptResult().get(1, TimeUnit.SECONDS);
    } catch (ExecutionException e) {
        outerError = e.getCause();
    } catch (Throwable e) {
        outerError = e;
    }
    Truth.assertThat(outerError).isInstanceOf(ServerStreamingAttemptException.class);
    Truth.assertThat(((ServerStreamingAttemptException) outerError).hasSeenResponses()).isFalse();
    Truth.assertThat(((ServerStreamingAttemptException) outerError).canResume()).isTrue();
    Truth.assertThat(outerError.getCause()).isEqualTo(initialError);
    // Make the retry call
    callable.call();
    call = innerCallable.popLastCall();
    // Verify the request and send a response
    Truth.assertThat(call.getRequest()).isEqualTo("request > 0");
}
Also used : ServerStreamingAttemptException(com.google.api.gax.retrying.ServerStreamingAttemptException) ExecutionException(java.util.concurrent.ExecutionException) FakeApiException(com.google.api.gax.rpc.testing.FakeApiException) Test(org.junit.Test)

Example 2 with ServerStreamingAttemptException

use of com.google.api.gax.retrying.ServerStreamingAttemptException in project gax-java by googleapis.

the class ServerStreamingAttemptCallableTest method testMidRetry.

@Test
@SuppressWarnings("ConstantConditions")
public void testMidRetry() {
    resumptionStrategy = new MyStreamResumptionStrategy();
    ServerStreamingAttemptCallable<String, String> callable = createCallable();
    callable.start();
    MockServerStreamingCall<String, String> call = innerCallable.popLastCall();
    // Respond to the initial request with a coupple responses and an error.
    Truth.assertThat(call.getRequest()).isEqualTo("request");
    call.getController().getObserver().onResponse("response1");
    call.getController().getObserver().onResponse("response2");
    FakeApiException innerError = new FakeApiException(null, Code.UNAVAILABLE, true);
    call.getController().getObserver().onError(innerError);
    // Should notify the outer future
    Throwable outerError = null;
    try {
        fakeRetryingFuture.getAttemptResult().get(1, TimeUnit.SECONDS);
    } catch (ExecutionException e) {
        outerError = e.getCause();
    } catch (Throwable e) {
        outerError = e;
    }
    Truth.assertThat(outerError).isInstanceOf(ServerStreamingAttemptException.class);
    Truth.assertThat(((ServerStreamingAttemptException) outerError).hasSeenResponses()).isTrue();
    Truth.assertThat(((ServerStreamingAttemptException) outerError).canResume()).isTrue();
    Truth.assertThat(outerError.getCause()).isEqualTo(innerError);
    // Make the retry call
    callable.call();
    call = innerCallable.popLastCall();
    // Verify that the request was narrowed and send a response
    Truth.assertThat(call.getRequest()).isEqualTo("request > 2");
    call.getController().getObserver().onResponse("response3");
    Truth.assertThat(observer.responses).containsExactly("response1", "response2", "response3").inOrder();
}
Also used : ServerStreamingAttemptException(com.google.api.gax.retrying.ServerStreamingAttemptException) ExecutionException(java.util.concurrent.ExecutionException) FakeApiException(com.google.api.gax.rpc.testing.FakeApiException) Test(org.junit.Test)

Example 3 with ServerStreamingAttemptException

use of com.google.api.gax.retrying.ServerStreamingAttemptException in project gax-java by googleapis.

the class StreamingRetryAlgorithmTest method testNextAttemptResetsTimedSettings.

@Test
public void testNextAttemptResetsTimedSettings() {
    RetryingContext context = mock(RetryingContext.class);
    BasicResultRetryAlgorithm<String> resultAlgorithm = new BasicResultRetryAlgorithm<>();
    ServerStreamingAttemptException exception = mock(ServerStreamingAttemptException.class);
    when(exception.canResume()).thenReturn(true);
    when(exception.hasSeenResponses()).thenReturn(true);
    UnavailableException cause = mock(UnavailableException.class);
    when(exception.getCause()).thenReturn(cause);
    ExponentialRetryAlgorithm timedAlgorithm = new ExponentialRetryAlgorithm(DEFAULT_RETRY_SETTINGS, mock(ApiClock.class));
    StreamingRetryAlgorithm<String> algorithm = new StreamingRetryAlgorithm<>(resultAlgorithm, timedAlgorithm);
    TimedAttemptSettings first = algorithm.createFirstAttempt(context);
    TimedAttemptSettings second = algorithm.createNextAttempt(context, mock(Exception.class), null, first);
    TimedAttemptSettings third = algorithm.createNextAttempt(context, exception, null, second);
    assertThat(third.getFirstAttemptStartTimeNanos()).isEqualTo(first.getFirstAttemptStartTimeNanos());
    // The timeout values are reset to the second call.
    assertThat(third.getRpcTimeout()).isEqualTo(second.getRpcTimeout());
}
Also used : ApiClock(com.google.api.core.ApiClock) StreamingRetryAlgorithm(com.google.api.gax.retrying.StreamingRetryAlgorithm) ServerStreamingAttemptException(com.google.api.gax.retrying.ServerStreamingAttemptException) ExponentialRetryAlgorithm(com.google.api.gax.retrying.ExponentialRetryAlgorithm) BasicResultRetryAlgorithm(com.google.api.gax.retrying.BasicResultRetryAlgorithm) TimedAttemptSettings(com.google.api.gax.retrying.TimedAttemptSettings) RetryingContext(com.google.api.gax.retrying.RetryingContext) ServerStreamingAttemptException(com.google.api.gax.retrying.ServerStreamingAttemptException) Test(org.junit.Test)

Example 4 with ServerStreamingAttemptException

use of com.google.api.gax.retrying.ServerStreamingAttemptException in project gax-java by googleapis.

the class ServerStreamingAttemptCallable method onCancel.

/**
 * Called when the outer {@link ResponseObserver} wants to prematurely cancel the stream.
 *
 * @see StreamController#cancel()
 */
private void onCancel() {
    StreamController localInnerController;
    synchronized (lock) {
        if (cancellationCause != null) {
            return;
        }
        // NOTE: BasicRetryingFuture will replace j.u.c.CancellationExceptions with it's own,
        // which will not have the current stacktrace, so a special wrapper has be used here.
        cancellationCause = new ServerStreamingAttemptException(new CancellationException("User cancelled stream"), resumptionStrategy.canResume(), seenSuccessSinceLastError);
        localInnerController = innerController;
    }
    if (localInnerController != null) {
        localInnerController.cancel();
    }
}
Also used : CancellationException(java.util.concurrent.CancellationException) ServerStreamingAttemptException(com.google.api.gax.retrying.ServerStreamingAttemptException)

Example 5 with ServerStreamingAttemptException

use of com.google.api.gax.retrying.ServerStreamingAttemptException in project java-bigquerystorage by googleapis.

the class ReadRowsAttemptCallable method onCancel.

/**
 * Called when the outer {@link ResponseObserver} wants to prematurely cancel the stream.
 *
 * @see StreamController#cancel()
 */
private void onCancel() {
    StreamController localInnerController;
    synchronized (lock) {
        if (cancellationCause != null) {
            return;
        }
        // NOTE: BasicRetryingFuture will replace j.u.c.CancellationExceptions with it's own,
        // which will not have the current stacktrace, so a special wrapper has be used here.
        cancellationCause = new ServerStreamingAttemptException(new CancellationException("User cancelled stream"), resumptionStrategy.canResume(), seenSuccessSinceLastError);
        localInnerController = innerController;
    }
    if (localInnerController != null) {
        localInnerController.cancel();
    }
}
Also used : StreamController(com.google.api.gax.rpc.StreamController) CancellationException(java.util.concurrent.CancellationException) ServerStreamingAttemptException(com.google.api.gax.retrying.ServerStreamingAttemptException)

Aggregations

ServerStreamingAttemptException (com.google.api.gax.retrying.ServerStreamingAttemptException)10 Test (org.junit.Test)6 CancellationException (java.util.concurrent.CancellationException)4 BasicResultRetryAlgorithm (com.google.api.gax.retrying.BasicResultRetryAlgorithm)3 ExponentialRetryAlgorithm (com.google.api.gax.retrying.ExponentialRetryAlgorithm)3 RetryingContext (com.google.api.gax.retrying.RetryingContext)3 StreamingRetryAlgorithm (com.google.api.gax.retrying.StreamingRetryAlgorithm)3 TimedAttemptSettings (com.google.api.gax.retrying.TimedAttemptSettings)3 StreamController (com.google.api.gax.rpc.StreamController)3 ApiClock (com.google.api.core.ApiClock)2 FakeApiException (com.google.api.gax.rpc.testing.FakeApiException)2 ExecutionException (java.util.concurrent.ExecutionException)2 DeadlineExceededException (com.google.api.gax.rpc.DeadlineExceededException)1 FakeStatusCode (com.google.api.gax.rpc.testing.FakeStatusCode)1 Status (io.opencensus.trace.Status)1