|
22 | 22 | import com.mongodb.internal.async.function.LoopState.AttachmentKey;
|
23 | 23 | import com.mongodb.internal.operation.retry.AttachmentKeys;
|
24 | 24 | import org.junit.jupiter.api.Assertions;
|
| 25 | +import org.junit.jupiter.api.DisplayName; |
25 | 26 | import org.junit.jupiter.api.Test;
|
26 | 27 | import org.junit.jupiter.params.ParameterizedTest;
|
27 | 28 | import org.junit.jupiter.params.provider.Arguments;
|
@@ -268,6 +269,68 @@ void advanceOrThrowPredicateFalse(final TimeoutContext timeoutContext) {
|
268 | 269 | assertThrows(attemptException.getClass(), () -> retryState.advanceOrThrow(attemptException, (e1, e2) -> e2, (rs, e) -> false));
|
269 | 270 | }
|
270 | 271 |
|
| 272 | + @ParameterizedTest |
| 273 | + @MethodSource({"infiniteTimeout"}) |
| 274 | + @DisplayName("should rethrow detected timeout exception even if timeout in retry state is not expired") |
| 275 | + void advanceReThrowDetectedTimeoutExceptionEvenIfTimeoutInRetryStateIsNotExpired(final TimeoutContext timeoutContext) { |
| 276 | + RetryState retryState = new RetryState(timeoutContext); |
| 277 | + |
| 278 | + MongoOperationTimeoutException expectedTimeoutException = TimeoutContext.createMongoTimeoutException("Server selection failed"); |
| 279 | + MongoOperationTimeoutException actualTimeoutException = |
| 280 | + assertThrows(expectedTimeoutException.getClass(), () -> retryState.advanceOrThrow(expectedTimeoutException, |
| 281 | + (e1, e2) -> expectedTimeoutException, |
| 282 | + (rs, e) -> false)); |
| 283 | + |
| 284 | + Assertions.assertEquals(actualTimeoutException, expectedTimeoutException); |
| 285 | + } |
| 286 | + |
| 287 | + @Test |
| 288 | + @DisplayName("should throw timeout exception from retry, when transformer swallows original timeout exception") |
| 289 | + void advanceThrowTimeoutExceptionWhenTransformerSwallowOriginalTimeoutException() { |
| 290 | + RetryState retryState = new RetryState(TIMEOUT_CONTEXT_INFINITE_GLOBAL_TIMEOUT); |
| 291 | + RuntimeException previousAttemptException = new RuntimeException() { |
| 292 | + }; |
| 293 | + MongoOperationTimeoutException expectedTimeoutException = TimeoutContext.createMongoTimeoutException("Server selection failed"); |
| 294 | + |
| 295 | + retryState.advanceOrThrow(previousAttemptException, |
| 296 | + (e1, e2) -> previousAttemptException, |
| 297 | + (rs, e) -> true); |
| 298 | + |
| 299 | + MongoOperationTimeoutException actualTimeoutException = |
| 300 | + assertThrows(expectedTimeoutException.getClass(), () -> retryState.advanceOrThrow(expectedTimeoutException, |
| 301 | + (e1, e2) -> previousAttemptException, |
| 302 | + (rs, e) -> false)); |
| 303 | + |
| 304 | + Assertions.assertNotEquals(actualTimeoutException, expectedTimeoutException); |
| 305 | + Assertions.assertEquals("Retry attempt timed out.", actualTimeoutException.getMessage()); |
| 306 | + Assertions.assertEquals(previousAttemptException, actualTimeoutException.getCause(), |
| 307 | + "Retry timeout exception should have a cause if transformer returned non-timeout exception."); |
| 308 | + } |
| 309 | + |
| 310 | + |
| 311 | + @Test |
| 312 | + @DisplayName("should throw original timeout exception from retry, when transformer returns original timeout exception") |
| 313 | + void advanceThrowOriginalTimeoutExceptionWhenTransformerReturnsOriginalTimeoutException() { |
| 314 | + RetryState retryState = new RetryState(TIMEOUT_CONTEXT_INFINITE_GLOBAL_TIMEOUT); |
| 315 | + RuntimeException previousAttemptException = new RuntimeException() { |
| 316 | + }; |
| 317 | + MongoOperationTimeoutException expectedTimeoutException = TimeoutContext |
| 318 | + .createMongoTimeoutException("Server selection failed"); |
| 319 | + |
| 320 | + retryState.advanceOrThrow(previousAttemptException, |
| 321 | + (e1, e2) -> previousAttemptException, |
| 322 | + (rs, e) -> true); |
| 323 | + |
| 324 | + MongoOperationTimeoutException actualTimeoutException = |
| 325 | + assertThrows(expectedTimeoutException.getClass(), () -> retryState.advanceOrThrow(expectedTimeoutException, |
| 326 | + (e1, e2) -> expectedTimeoutException, |
| 327 | + (rs, e) -> false)); |
| 328 | + |
| 329 | + Assertions.assertEquals(actualTimeoutException, expectedTimeoutException); |
| 330 | + Assertions.assertNull(actualTimeoutException.getCause(), |
| 331 | + "Original timeout exception should not have a cause if transformer already returned timeout exception."); |
| 332 | + } |
| 333 | + |
271 | 334 | @Test
|
272 | 335 | void advanceOrThrowPredicateTrueAndLastAttempt() {
|
273 | 336 | RetryState retryState = RetryState.withNonRetryableState();
|
|
0 commit comments