use of com.google.api.gax.retrying.TimedAttemptSettings in project gax-java by googleapis.
the class StreamingRetryAlgorithmTest method testNextAttemptReturnsResultAlgorithmSettingsWhenShouldRetry.
@Test
public void testNextAttemptReturnsResultAlgorithmSettingsWhenShouldRetry() {
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(true);
TimedAttemptSettings next = mock(TimedAttemptSettings.class);
when(resultAlgorithm.createNextAttempt(Mockito.eq(context), Mockito.eq(exception), Mockito.<String>isNull(), any(TimedAttemptSettings.class))).thenReturn(next);
ExponentialRetryAlgorithm timedAlgorithm = new ExponentialRetryAlgorithm(DEFAULT_RETRY_SETTINGS, mock(ApiClock.class));
StreamingRetryAlgorithm<String> algorithm = new StreamingRetryAlgorithm<>(resultAlgorithm, timedAlgorithm);
TimedAttemptSettings first = algorithm.createFirstAttempt(context);
TimedAttemptSettings attempt = algorithm.createNextAttempt(context, exception, null, first);
assertThat(attempt).isSameInstanceAs(next);
}
use of com.google.api.gax.retrying.TimedAttemptSettings in project gax-java by googleapis.
the class StreamingRetryAlgorithmTest method testFirstAttemptUsesContextSettings.
@Test
public void testFirstAttemptUsesContextSettings() {
RetryingContext context = mock(RetryingContext.class);
when(context.getRetrySettings()).thenReturn(CONTEXT_RETRY_SETTINGS);
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(CONTEXT_RETRY_SETTINGS);
assertThat(attempt.getRpcTimeout()).isEqualTo(CONTEXT_RETRY_SETTINGS.getInitialRpcTimeout());
}
use of com.google.api.gax.retrying.TimedAttemptSettings 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.TimedAttemptSettings 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.TimedAttemptSettings 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());
}
Aggregations