11package org .testcontainers .images .retry ;
22
33import com .github .dockerjava .api .exception .InternalServerErrorException ;
4- import org .junit .ClassRule ;
5- import org .junit .Rule ;
6- import org .junit .Test ;
7- import org .junit .rules .ExpectedException ;
8- import org .testcontainers .DockerRegistryContainer ;
4+ import org .junit .jupiter .api .Test ;
95import org .testcontainers .utility .DockerImageName ;
106
117import java .time .Duration ;
12- import java .time .Instant ;
138
149import static org .assertj .core .api .Assertions .assertThat ;
10+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
1511
1612public class ImagePullRetryPolicyTest {
1713
18- @ ClassRule
19- public static DockerRegistryContainer registry = new DockerRegistryContainer ();
20-
21- private final DockerImageName imageName = registry .createImage ();
22-
23- @ Rule
24- public ExpectedException thrown = ExpectedException .none ();
14+ private final DockerImageName imageName = DockerImageName .parse ("any/image:latest" );
2515
2616 @ Test
2717 public void shouldNotRetryWhenUsingFailFastPullRetryPolicy () {
@@ -32,37 +22,37 @@ public void shouldNotRetryWhenUsingFailFastPullRetryPolicy() {
3222
3323 @ Test
3424 public void shouldFailIfTheConfiguredDurationIsNegativeWhenUsingLimitedDurationPullRetryPolicy () {
35- thrown . expect ( IllegalArgumentException . class );
36- thrown . expectMessage ( "should not be negative" );
37- PullRetryPolicy . limitedDuration ( Duration . ofMinutes (- 1 ) );
25+ assertThatThrownBy (() -> PullRetryPolicy . limitedDuration ( Duration . ofMinutes (- 1 )))
26+ . isInstanceOf ( IllegalArgumentException . class )
27+ . hasMessageContaining ( "should not be negative" );
3828 }
3929
4030 @ Test
4131 public void shouldFailIfPullStartedIsNotBeingCalledBeforeShouldRetryWhenUsingLimitedDurationPullRetryPolicy () {
42- thrown .expect (IllegalStateException .class );
43- thrown .expectMessage ("Please, check that pullStarted has been called." );
4432 ImagePullRetryPolicy policy = PullRetryPolicy .limitedDuration (Duration .ofMinutes (1 ));
45- policy .shouldRetry (imageName , new Exception ());
33+ assertThatThrownBy (() -> policy .shouldRetry (imageName , new Exception ()))
34+ .isInstanceOf (IllegalStateException .class )
35+ .hasMessageContaining ("Please, check that pullStarted has been called." );
4636 }
4737
4838 @ Test
49- public void shouldRetryDuringTheConfiguredAmountOfTimeWhenUsingLimitedDurationPullRetryPolicy () {
39+ public void shouldRetryDuringTheConfiguredAmountOfTimeWhenUsingLimitedDurationPullRetryPolicy () throws InterruptedException {
5040 Duration maxAllowedDuration = Duration .ofMillis (100 );
51- Instant lastRetryAllowed = Instant .now ().plus (maxAllowedDuration );
5241 ImagePullRetryPolicy policy = PullRetryPolicy .limitedDuration (maxAllowedDuration );
5342 policy .pullStarted ();
54- while (Instant .now ().isBefore (lastRetryAllowed )) {
55- assertThat (policy .shouldRetry (imageName , new Exception ())).isTrue ();
56- }
43+
44+ assertThat (policy .shouldRetry (imageName , new Exception ())).isTrue ();
45+
46+ Thread .sleep (maxAllowedDuration .toMillis () + 50 );
5747
5848 assertThat (policy .shouldRetry (imageName , new Exception ())).isFalse ();
5949 }
6050
6151 @ Test
6252 public void shouldFailIfTheConfiguredNumberOfAttemptsIsNegativeWhenUsingNoOfAttemptsPullRetryPolicy () {
63- thrown . expect ( IllegalArgumentException . class );
64- thrown . expectMessage ( "should not be negative" );
65- PullRetryPolicy . noOfAttempts (- 1 );
53+ assertThatThrownBy (() -> PullRetryPolicy . noOfAttempts (- 1 ))
54+ . isInstanceOf ( IllegalArgumentException . class )
55+ . hasMessageContaining ( "should not be negative" );
6656 }
6757
6858 @ Test
0 commit comments