use of com.google.api.gax.retrying.StreamingRetryAlgorithm in project gax-java by googleapis.
the class StreamingRetryAlgorithmTest method testNextAttemptReturnsNullWhenShouldNotRetry.
@Test
public void testNextAttemptReturnsNullWhenShouldNotRetry() {
RetryingContext context = mock(RetryingContext.class);
@SuppressWarnings("unchecked") BasicResultRetryAlgorithm<String> resultAlgorithm = mock(BasicResultRetryAlgorithm.class);
UnavailableException exception = mock(UnavailableException.class);
when(resultAlgorithm.shouldRetry(context, exception, null)).thenReturn(false);
ExponentialRetryAlgorithm timedAlgorithm = new ExponentialRetryAlgorithm(DEFAULT_RETRY_SETTINGS, mock(ApiClock.class));
StreamingRetryAlgorithm<String> algorithm = new StreamingRetryAlgorithm<>(resultAlgorithm, timedAlgorithm);
TimedAttemptSettings attempt = algorithm.createNextAttempt(context, exception, null, mock(TimedAttemptSettings.class));
assertThat(attempt).isNull();
TimedAttemptSettings attemptWithoutContext = algorithm.createNextAttempt(exception, null, mock(TimedAttemptSettings.class));
assertThat(attemptWithoutContext).isNull();
}
use of com.google.api.gax.retrying.StreamingRetryAlgorithm in project gax-java by googleapis.
the class StreamingRetryAlgorithmTest method testFirstAttemptUsesDefaultSettings.
@Test
public void testFirstAttemptUsesDefaultSettings() {
RetryingContext context = mock(RetryingContext.class);
BasicResultRetryAlgorithm<String> resultAlgorithm = new BasicResultRetryAlgorithm<>();
ExponentialRetryAlgorithm timedAlgorithm = new ExponentialRetryAlgorithm(DEFAULT_RETRY_SETTINGS, mock(ApiClock.class));
StreamingRetryAlgorithm<String> algorithm = new StreamingRetryAlgorithm<>(resultAlgorithm, timedAlgorithm);
TimedAttemptSettings attempt = algorithm.createFirstAttempt(context);
assertThat(attempt.getGlobalSettings()).isSameInstanceAs(DEFAULT_RETRY_SETTINGS);
assertThat(attempt.getRpcTimeout()).isEqualTo(DEFAULT_RETRY_SETTINGS.getInitialRpcTimeout());
}
use of com.google.api.gax.retrying.StreamingRetryAlgorithm in project gax-java by googleapis.
the class StreamingRetryAlgorithmTest method testShouldNotRetryIfAttemptIsNonResumable.
@Test
public void testShouldNotRetryIfAttemptIsNonResumable() {
RetryingContext context = mock(RetryingContext.class);
ServerStreamingAttemptException exception = mock(ServerStreamingAttemptException.class);
when(exception.canResume()).thenReturn(false);
UnavailableException cause = mock(UnavailableException.class);
when(exception.getCause()).thenReturn(cause);
BasicResultRetryAlgorithm<String> resultAlgorithm = new BasicResultRetryAlgorithm<>();
ExponentialRetryAlgorithm timedAlgorithm = new ExponentialRetryAlgorithm(DEFAULT_RETRY_SETTINGS, mock(ApiClock.class));
StreamingRetryAlgorithm<String> algorithm = new StreamingRetryAlgorithm<>(resultAlgorithm, timedAlgorithm);
// This should return false because the attempt exception indicates that it is non-resumable.
boolean shouldRetry = algorithm.shouldRetry(context, exception, null, mock(TimedAttemptSettings.class));
assertThat(shouldRetry).isFalse();
boolean shouldRetryWithoutContext = algorithm.shouldRetry(exception, null, mock(TimedAttemptSettings.class));
assertThat(shouldRetryWithoutContext).isFalse();
}
use of com.google.api.gax.retrying.StreamingRetryAlgorithm in project gax-java by googleapis.
the class StreamingRetryAlgorithmTest method testShouldRetryIfAllSayYes.
@Test
public void testShouldRetryIfAllSayYes() {
RetryingContext context = mock(RetryingContext.class);
ServerStreamingAttemptException exception = mock(ServerStreamingAttemptException.class);
when(exception.canResume()).thenReturn(true);
UnavailableException cause = mock(UnavailableException.class);
when(exception.getCause()).thenReturn(cause);
BasicResultRetryAlgorithm<String> resultAlgorithm = new BasicResultRetryAlgorithm<>();
ExponentialRetryAlgorithm timedAlgorithm = mock(ExponentialRetryAlgorithm.class);
when(timedAlgorithm.shouldRetry(Mockito.eq(context), any(TimedAttemptSettings.class))).thenReturn(true);
StreamingRetryAlgorithm<String> algorithm = new StreamingRetryAlgorithm<>(resultAlgorithm, timedAlgorithm);
boolean shouldRetry = algorithm.shouldRetry(context, exception, null, mock(TimedAttemptSettings.class));
assertThat(shouldRetry).isTrue();
}
use of com.google.api.gax.retrying.StreamingRetryAlgorithm in project java-bigquerystorage by googleapis.
the class EnhancedBigQueryStorageStub method readRowsCallable.
public ServerStreamingCallable<ReadRowsRequest, ReadRowsResponse> readRowsCallable() {
ServerStreamingCallable<ReadRowsRequest, ReadRowsResponse> innerCallable = GrpcRawCallableFactory.createServerStreamingCallable(GrpcCallSettings.<ReadRowsRequest, ReadRowsResponse>newBuilder().setMethodDescriptor(BigQueryStorageGrpc.getReadRowsMethod()).setParamsExtractor(new RequestParamsExtractor<ReadRowsRequest>() {
@Override
public Map<String, String> extract(ReadRowsRequest request) {
return ImmutableMap.of("read_position.stream.name", String.valueOf(request.getReadPosition().getStream().getName()));
}
}).build(), stubSettings.readRowsSettings().getRetryableCodes());
ServerStreamingCallSettings<ReadRowsRequest, ReadRowsResponse> callSettings = stubSettings.readRowsSettings();
StreamingRetryAlgorithm<Void> retryAlgorithm = new StreamingRetryAlgorithm<>(new ApiResultRetryAlgorithm<Void>(readRowsRetryAttemptListener), new ExponentialRetryAlgorithm(callSettings.getRetrySettings(), context.getClock()));
ScheduledRetryingExecutor<Void> retryingExecutor = new ScheduledRetryingExecutor<>(retryAlgorithm, context.getExecutor());
if (context.getStreamWatchdog() != null) {
innerCallable = Callables.watched(innerCallable, callSettings, context);
}
ReadRowsRetryingCallable outerCallable = new ReadRowsRetryingCallable(context.getDefaultCallContext(), innerCallable, retryingExecutor, callSettings.getResumptionStrategy());
ServerStreamingCallable<ReadRowsRequest, ReadRowsResponse> traced = new TracedServerStreamingCallable<>(outerCallable, context.getTracerFactory(), SpanName.of(TRACING_OUTER_CLIENT_NAME, "ReadRows"));
return traced.withDefaultCallContext(context.getDefaultCallContext());
}
Aggregations