Skip to content

Commit d0be180

Browse files
committed
Make @​Retryable and RetryTemplate timeout tests more robust
See gh-35963
1 parent 3731fed commit d0be180

File tree

3 files changed

+25
-25
lines changed

3 files changed

+25
-25
lines changed

spring-context/src/test/java/org/springframework/resilience/ReactiveRetryInterceptorTests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ void timeoutExceededAfterInitialFailure() {
351351
.satisfies(isReactiveException())
352352
.havingCause()
353353
.isInstanceOf(TimeoutException.class)
354-
.withMessageContaining("within 5ms");
354+
.withMessageContaining("within 20ms");
355355
// 1 initial attempt + 0 retries
356356
assertThat(target.counter).hasValue(1);
357357
}
@@ -363,7 +363,7 @@ void timeoutExceededAfterFirstDelayButBeforeFirstRetry() {
363363
.satisfies(isReactiveException())
364364
.havingCause()
365365
.isInstanceOf(TimeoutException.class)
366-
.withMessageContaining("within 5ms");
366+
.withMessageContaining("within 20ms");
367367
// 1 initial attempt + 0 retries
368368
assertThat(target.counter).hasValue(1);
369369
}
@@ -464,16 +464,16 @@ public Mono<Object> retryOperationWithTimeoutNotExceededAndRetriesExhausted() {
464464
});
465465
}
466466

467-
@Retryable(timeout = 5, delay = 0)
467+
@Retryable(timeout = 20, delay = 0)
468468
public Mono<Object> retryOperationWithTimeoutExceededAfterInitialFailure() {
469469
return Mono.fromCallable(() -> {
470470
counter.incrementAndGet();
471-
Thread.sleep(20);
471+
Thread.sleep(100);
472472
throw new IOException(counter.toString());
473473
});
474474
}
475475

476-
@Retryable(timeout = 5, delay = 10)
476+
@Retryable(timeout = 20, delay = 100) // Delay > Timeout
477477
public Mono<Object> retryOperationWithTimeoutExceededAfterFirstDelayButBeforeFirstRetry() {
478478
return Mono.fromCallable(() -> {
479479
counter.incrementAndGet();
@@ -486,7 +486,7 @@ public Mono<Object> retryOperationWithTimeoutExceededAfterFirstRetry() {
486486
return Mono.fromCallable(() -> {
487487
counter.incrementAndGet();
488488
if (counter.get() == 2) {
489-
Thread.sleep(50);
489+
Thread.sleep(100);
490490
}
491491
throw new IOException(counter.toString());
492492
});
@@ -497,7 +497,7 @@ public Mono<Object> retryOperationWithTimeoutExceededAfterSecondRetry() {
497497
return Mono.fromCallable(() -> {
498498
counter.incrementAndGet();
499499
if (counter.get() == 3) {
500-
Thread.sleep(50);
500+
Thread.sleep(100);
501501
}
502502
throw new IOException(counter.toString());
503503
});

spring-context/src/test/java/org/springframework/resilience/RetryInterceptorTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -426,14 +426,14 @@ public void retryOperationWithTimeoutNotExceededAndRetriesExhausted() throws Exc
426426
throw new IOException(Integer.toString(counter));
427427
}
428428

429-
@Retryable(timeout = 5, delay = 10)
429+
@Retryable(timeout = 20, delay = 0)
430430
public void retryOperationWithTimeoutExceededAfterInitialFailure() throws Exception {
431431
counter++;
432-
Thread.sleep(10);
432+
Thread.sleep(100);
433433
throw new IOException(Integer.toString(counter));
434434
}
435435

436-
@Retryable(timeout = 5, delay = 10)
436+
@Retryable(timeout = 20, delay = 100) // Delay > Timeout
437437
public void retryOperationWithTimeoutExceededAfterFirstDelayButBeforeFirstRetry() throws IOException {
438438
counter++;
439439
throw new IOException(Integer.toString(counter));
@@ -443,7 +443,7 @@ public void retryOperationWithTimeoutExceededAfterFirstDelayButBeforeFirstRetry(
443443
public void retryOperationWithTimeoutExceededAfterFirstRetry() throws Exception {
444444
counter++;
445445
if (counter == 2) {
446-
Thread.sleep(50);
446+
Thread.sleep(100);
447447
}
448448
throw new IOException(Integer.toString(counter));
449449
}
@@ -452,7 +452,7 @@ public void retryOperationWithTimeoutExceededAfterFirstRetry() throws Exception
452452
public void retryOperationWithTimeoutExceededAfterSecondRetry() throws Exception {
453453
counter++;
454454
if (counter == 3) {
455-
Thread.sleep(50);
455+
Thread.sleep(100);
456456
}
457457
throw new IOException(Integer.toString(counter));
458458
}

spring-core/src/test/java/org/springframework/core/retry/RetryTemplateTests.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -413,14 +413,14 @@ class TimeoutTests {
413413

414414
@Test
415415
void retryWithImmediateSuccessAndTimeoutExceeded() throws Exception {
416-
RetryPolicy retryPolicy = RetryPolicy.builder().timeout(Duration.ofMillis(5)).build();
416+
RetryPolicy retryPolicy = RetryPolicy.builder().timeout(Duration.ofMillis(10)).build();
417417
RetryTemplate retryTemplate = new RetryTemplate(retryPolicy);
418418
retryTemplate.setRetryListener(retryListener);
419419

420420
AtomicInteger invocationCount = new AtomicInteger();
421421
Retryable<String> retryable = () -> {
422422
invocationCount.incrementAndGet();
423-
Thread.sleep(10);
423+
Thread.sleep(100);
424424
return "always succeeds";
425425
};
426426

@@ -435,15 +435,15 @@ void retryWithImmediateSuccessAndTimeoutExceeded() throws Exception {
435435
@Test
436436
void retryWithInitialFailureAndZeroRetriesRetryPolicyAndTimeoutExceeded() {
437437
RetryPolicy retryPolicy = RetryPolicy.builder()
438-
.timeout(Duration.ofMillis(5))
438+
.timeout(Duration.ofMillis(10))
439439
.predicate(throwable -> false) // Zero retries
440440
.build();
441441
RetryTemplate retryTemplate = new RetryTemplate(retryPolicy);
442442
retryTemplate.setRetryListener(retryListener);
443443

444444
Exception exception = new RuntimeException("Boom!");
445445
Retryable<String> retryable = () -> {
446-
Thread.sleep(10);
446+
Thread.sleep(100);
447447
throw exception;
448448
};
449449

@@ -461,22 +461,22 @@ void retryWithInitialFailureAndZeroRetriesRetryPolicyAndTimeoutExceeded() {
461461
@Test
462462
void retryWithTimeoutExceededAfterInitialFailure() throws Exception {
463463
RetryPolicy retryPolicy = RetryPolicy.builder()
464-
.timeout(Duration.ofMillis(5))
464+
.timeout(Duration.ofMillis(10))
465465
.delay(Duration.ZERO)
466466
.build();
467467
RetryTemplate retryTemplate = new RetryTemplate(retryPolicy);
468468
retryTemplate.setRetryListener(retryListener);
469469

470470
AtomicInteger invocationCount = new AtomicInteger();
471471
Retryable<String> retryable = () -> {
472-
Thread.sleep(10);
472+
Thread.sleep(100);
473473
throw new CustomException("Boom " + invocationCount.incrementAndGet());
474474
};
475475

476476
assertThat(invocationCount).hasValue(0);
477477
assertThatExceptionOfType(RetryException.class)
478478
.isThrownBy(() -> retryTemplate.execute(retryable))
479-
.withMessageMatching("Retry policy for operation '.+?' exceeded timeout \\(5ms\\); aborting execution")
479+
.withMessageMatching("Retry policy for operation '.+?' exceeded timeout \\(10ms\\); aborting execution")
480480
.withCause(new CustomException("Boom 1"))
481481
.satisfies(throwable -> inOrder.verify(retryListener).onRetryPolicyTimeout(
482482
eq(retryPolicy), eq(retryable), eq(throwable)));
@@ -488,8 +488,8 @@ void retryWithTimeoutExceededAfterInitialFailure() throws Exception {
488488
@Test
489489
void retryWithTimeoutExceededAfterFirstDelayButBeforeFirstRetry() throws Exception {
490490
RetryPolicy retryPolicy = RetryPolicy.builder()
491-
.timeout(Duration.ofMillis(5))
492-
.delay(Duration.ofMillis(10)) // Delay > Timeout
491+
.timeout(Duration.ofMillis(20))
492+
.delay(Duration.ofMillis(100)) // Delay > Timeout
493493
.build();
494494
RetryTemplate retryTemplate = new RetryTemplate(retryPolicy);
495495
retryTemplate.setRetryListener(retryListener);
@@ -503,8 +503,8 @@ void retryWithTimeoutExceededAfterFirstDelayButBeforeFirstRetry() throws Excepti
503503
assertThatExceptionOfType(RetryException.class)
504504
.isThrownBy(() -> retryTemplate.execute(retryable))
505505
.withMessageMatching("""
506-
Retry policy for operation '.+?' would exceed timeout \\(5ms\\) \
507-
due to pending sleep time \\(10ms\\); preemptively aborting execution\
506+
Retry policy for operation '.+?' would exceed timeout \\(20ms\\) \
507+
due to pending sleep time \\(100ms\\); preemptively aborting execution\
508508
""")
509509
.withCause(new CustomException("Boom 1"))
510510
.satisfies(throwable -> inOrder.verify(retryListener).onRetryPolicyTimeout(
@@ -527,7 +527,7 @@ void retryWithTimeoutExceededAfterFirstRetry() throws Exception {
527527
Retryable<String> retryable = () -> {
528528
int currentInvocation = invocationCount.incrementAndGet();
529529
if (currentInvocation == 2) {
530-
Thread.sleep(50);
530+
Thread.sleep(100);
531531
}
532532
throw new CustomException("Boom " + currentInvocation);
533533
};
@@ -562,7 +562,7 @@ void retryWithTimeoutExceededAfterSecondRetry() throws Exception {
562562
Retryable<String> retryable = () -> {
563563
int currentInvocation = invocationCount.incrementAndGet();
564564
if (currentInvocation == 3) {
565-
Thread.sleep(50);
565+
Thread.sleep(100);
566566
}
567567
throw new CustomException("Boom " + currentInvocation);
568568
};

0 commit comments

Comments
 (0)