|
30 | 30 | package com.google.api.gax.retrying; |
31 | 31 |
|
32 | 32 | import static org.junit.jupiter.api.Assertions.assertFalse; |
| 33 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
33 | 35 | import static org.mockito.Mockito.mock; |
34 | 36 | import static org.mockito.Mockito.never; |
35 | 37 | import static org.mockito.Mockito.verify; |
36 | 38 | import static org.mockito.Mockito.when; |
37 | 39 |
|
| 40 | +import java.util.concurrent.CancellationException; |
38 | 41 | import org.junit.jupiter.api.Test; |
39 | 42 |
|
40 | 43 | @SuppressWarnings({"unchecked", "deprecation"}) |
@@ -114,6 +117,45 @@ void testNextAttemptWithContext() { |
114 | 117 | verify(resultAlgorithm).shouldRetry(context, previousThrowable, previousResult); |
115 | 118 | } |
116 | 119 |
|
| 120 | + @Test |
| 121 | + void testCreateNextAttempt_exceptionAndTimeout_defersToTimingException() { |
| 122 | + ResultRetryAlgorithm<Object> resultAlgorithm = mock(ResultRetryAlgorithm.class); |
| 123 | + TimedRetryAlgorithm timedAlgorithm = mock(TimedRetryAlgorithm.class); |
| 124 | + RetryAlgorithm<Object> algorithm = new RetryAlgorithm<>(resultAlgorithm, timedAlgorithm); |
| 125 | + Throwable throwable = new Throwable(); |
| 126 | + Object result = new Object(); |
| 127 | + TimedAttemptSettings previousSettings = mock(TimedAttemptSettings.class); |
| 128 | + TimedAttemptSettings nextSettings = mock(TimedAttemptSettings.class); |
| 129 | + |
| 130 | + when(resultAlgorithm.shouldRetry(throwable, result)).thenReturn(false); |
| 131 | + when(timedAlgorithm.createNextAttempt(previousSettings)).thenReturn(nextSettings); |
| 132 | + when(timedAlgorithm.shouldRetry(nextSettings)).thenThrow(new CancellationException()); |
| 133 | + |
| 134 | + assertThrows( |
| 135 | + CancellationException.class, |
| 136 | + () -> algorithm.createNextAttempt(throwable, result, previousSettings)); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + void testCreateNextAttempt_exceptionAndNoTimeout_returnsNull() { |
| 141 | + ResultRetryAlgorithm<Object> resultAlgorithm = mock(ResultRetryAlgorithm.class); |
| 142 | + TimedRetryAlgorithm timedAlgorithm = mock(TimedRetryAlgorithm.class); |
| 143 | + RetryAlgorithm<Object> algorithm = new RetryAlgorithm<>(resultAlgorithm, timedAlgorithm); |
| 144 | + Throwable throwable = new Throwable(); |
| 145 | + Object result = new Object(); |
| 146 | + TimedAttemptSettings previousSettings = mock(TimedAttemptSettings.class); |
| 147 | + TimedAttemptSettings nextSettings = mock(TimedAttemptSettings.class); |
| 148 | + |
| 149 | + when(resultAlgorithm.shouldRetry(throwable, result)).thenReturn(false); |
| 150 | + when(timedAlgorithm.createNextAttempt(previousSettings)).thenReturn(nextSettings); |
| 151 | + when(timedAlgorithm.shouldRetry(nextSettings)).thenReturn(true); |
| 152 | + |
| 153 | + TimedAttemptSettings nextAttemptSettings = |
| 154 | + algorithm.createNextAttempt(throwable, result, previousSettings); |
| 155 | + |
| 156 | + assertNull(nextAttemptSettings); |
| 157 | + } |
| 158 | + |
117 | 159 | @Test |
118 | 160 | void testShouldRetry() { |
119 | 161 | ResultRetryAlgorithm<Object> resultAlgorithm = mock(ResultRetryAlgorithm.class); |
@@ -203,23 +245,6 @@ void testShouldRetryWithContext_noPreviousSettings() { |
203 | 245 | assertFalse(algorithm.shouldRetry(context, previousThrowable, previousResult, null)); |
204 | 246 | } |
205 | 247 |
|
206 | | - @Test |
207 | | - void testShouldRetry_resultFalseAndThrowableNotNull_callsTimed() { |
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 | | - boolean shouldRetry = |
217 | | - algorithm.shouldRetry(previousThrowable, previousResult, previousSettings); |
218 | | - |
219 | | - assertFalse(shouldRetry); |
220 | | - verify(timedAlgorithm).shouldRetry(previousSettings); |
221 | | - } |
222 | | - |
223 | 248 | @Test |
224 | 249 | void testShouldRetry_resultFalseAndThrowableNull_shortCircuits() { |
225 | 250 | ResultRetryAlgorithm<Object> resultAlgorithm = mock(ResultRetryAlgorithm.class); |
|
0 commit comments