Search in sources :

Example 1 with BasicResultRetryAlgorithm

use of com.google.api.gax.retrying.BasicResultRetryAlgorithm 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());
}
Also used : ApiClock(com.google.api.core.ApiClock) StreamingRetryAlgorithm(com.google.api.gax.retrying.StreamingRetryAlgorithm) 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) Test(org.junit.Test)

Example 2 with BasicResultRetryAlgorithm

use of com.google.api.gax.retrying.BasicResultRetryAlgorithm 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 3 with BasicResultRetryAlgorithm

use of com.google.api.gax.retrying.BasicResultRetryAlgorithm 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());
}
Also used : ApiClock(com.google.api.core.ApiClock) StreamingRetryAlgorithm(com.google.api.gax.retrying.StreamingRetryAlgorithm) 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) Test(org.junit.Test)

Example 4 with BasicResultRetryAlgorithm

use of com.google.api.gax.retrying.BasicResultRetryAlgorithm 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();
}
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) Test(org.junit.Test)

Example 5 with BasicResultRetryAlgorithm

use of com.google.api.gax.retrying.BasicResultRetryAlgorithm 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();
}
Also used : 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) Test(org.junit.Test)

Aggregations

BasicResultRetryAlgorithm (com.google.api.gax.retrying.BasicResultRetryAlgorithm)5 ExponentialRetryAlgorithm (com.google.api.gax.retrying.ExponentialRetryAlgorithm)5 RetryingContext (com.google.api.gax.retrying.RetryingContext)5 StreamingRetryAlgorithm (com.google.api.gax.retrying.StreamingRetryAlgorithm)5 TimedAttemptSettings (com.google.api.gax.retrying.TimedAttemptSettings)5 Test (org.junit.Test)5 ApiClock (com.google.api.core.ApiClock)4 ServerStreamingAttemptException (com.google.api.gax.retrying.ServerStreamingAttemptException)3