Skip to content

Commit 9647336

Browse files
committed
test(gax): add tests for RetryAlgorithm changes
1 parent 564a1f0 commit 9647336

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/retrying/RetryAlgorithmTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import static org.junit.jupiter.api.Assertions.assertFalse;
3333
import static org.mockito.Mockito.mock;
34+
import static org.mockito.Mockito.never;
3435
import static org.mockito.Mockito.verify;
3536
import static org.mockito.Mockito.when;
3637

@@ -201,4 +202,34 @@ void testShouldRetryWithContext_noPreviousSettings() {
201202

202203
assertFalse(algorithm.shouldRetry(context, previousThrowable, previousResult, null));
203204
}
205+
206+
@Test
207+
void testShouldRetry_prevThrowableNotNull_shouldRetryBasedOnResultFalse_callsTimedAlgorithm() {
208+
ResultRetryAlgorithm<Object> resultAlgorithm = mock(ResultRetryAlgorithm.class);
209+
TimedRetryAlgorithm timedAlgorithm = mock(TimedRetryAlgorithm.class);
210+
RetryAlgorithm<Object> algorithm = new RetryAlgorithm<>(resultAlgorithm, timedAlgorithm);
211+
Throwable previousThrowable = new Throwable();
212+
Object previousResult = new Object();
213+
TimedAttemptSettings previousSettings = mock(TimedAttemptSettings.class);
214+
when(resultAlgorithm.shouldRetry(previousThrowable, previousResult)).thenReturn(false);
215+
216+
algorithm.shouldRetry(previousThrowable, previousResult, previousSettings);
217+
218+
verify(timedAlgorithm).shouldRetry(previousSettings);
219+
}
220+
221+
@Test
222+
void testShouldRetry_prevThrowableNull_shouldRetryBasedOnResultFalse_shortCircuits() {
223+
ResultRetryAlgorithm<Object> resultAlgorithm = mock(ResultRetryAlgorithm.class);
224+
TimedRetryAlgorithm timedAlgorithm = mock(TimedRetryAlgorithm.class);
225+
RetryAlgorithm<Object> algorithm = new RetryAlgorithm<>(resultAlgorithm, timedAlgorithm);
226+
Object previousResult = new Object();
227+
TimedAttemptSettings previousSettings = mock(TimedAttemptSettings.class);
228+
when(resultAlgorithm.shouldRetry(null, previousResult)).thenReturn(false);
229+
230+
boolean shouldRetry = algorithm.shouldRetry(null, previousResult, previousSettings);
231+
232+
assertFalse(shouldRetry);
233+
verify(timedAlgorithm, never()).shouldRetry(previousSettings);
234+
}
204235
}

0 commit comments

Comments
 (0)