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");
}
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();
}
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());
}
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();
}
}
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();
}
}
Aggregations