From 76710124b99be29fd3ee1d314b7637d96882f01b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Thu, 26 May 2022 09:35:12 -0500 Subject: [PATCH 1/3] Add try-with-resources to HttpWaitStrategyTest --- .../strategy/AbstractWaitStrategyTest.java | 10 ++- .../wait/strategy/HttpWaitStrategyTest.java | 85 ++++++++++++------- 2 files changed, 60 insertions(+), 35 deletions(-) diff --git a/core/src/test/java/org/testcontainers/junit/wait/strategy/AbstractWaitStrategyTest.java b/core/src/test/java/org/testcontainers/junit/wait/strategy/AbstractWaitStrategyTest.java index de16eb0e582..786be0377f2 100644 --- a/core/src/test/java/org/testcontainers/junit/wait/strategy/AbstractWaitStrategyTest.java +++ b/core/src/test/java/org/testcontainers/junit/wait/strategy/AbstractWaitStrategyTest.java @@ -84,7 +84,11 @@ protected GenericContainer startContainerWithCommand( * @param shellCommand the shell command to execute */ protected void waitUntilReadyAndSucceed(String shellCommand) { - waitUntilReadyAndSucceed(startContainerWithCommand(shellCommand)); + try ( + GenericContainer container = startContainerWithCommand(shellCommand) + ) { + waitUntilReadyAndSucceed(container); + } } /** @@ -94,7 +98,9 @@ protected void waitUntilReadyAndSucceed(String shellCommand) { * @param shellCommand the shell command to execute */ protected void waitUntilReadyAndTimeout(String shellCommand) { - waitUntilReadyAndTimeout(startContainerWithCommand(shellCommand)); + try (GenericContainer container = startContainerWithCommand(shellCommand)) { + waitUntilReadyAndTimeout(container); + } } /** diff --git a/core/src/test/java/org/testcontainers/junit/wait/strategy/HttpWaitStrategyTest.java b/core/src/test/java/org/testcontainers/junit/wait/strategy/HttpWaitStrategyTest.java index 7014749115e..bcd0129fc99 100644 --- a/core/src/test/java/org/testcontainers/junit/wait/strategy/HttpWaitStrategyTest.java +++ b/core/src/test/java/org/testcontainers/junit/wait/strategy/HttpWaitStrategyTest.java @@ -11,8 +11,8 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Predicate; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; -import static org.junit.Assert.assertThat; /** * Tests for {@link HttpWaitStrategy}. @@ -45,16 +45,19 @@ public void testWaitUntilReadyWithSuccess() { public void testWaitUntilReadyWithSuccessWithCustomHeaders() { HashMap headers = new HashMap<>(); headers.put("baz", "boo"); - GenericContainer container = startContainerWithCommand( - createShellCommand("200 OK", GOOD_RESPONSE_BODY), - createHttpWaitStrategy(ready).withHeader("foo", "bar").withHeaders(headers) - ); - waitUntilReadyAndSucceed(container); + try ( + GenericContainer container = startContainerWithCommand( + createShellCommand("200 OK", GOOD_RESPONSE_BODY), + createHttpWaitStrategy(ready).withHeader("foo", "bar").withHeaders(headers) + ) + ) { + waitUntilReadyAndSucceed(container); - String logs = container.getLogs(); + String logs = container.getLogs(); - assertThat(logs, containsString("foo: bar")); - assertThat(logs, containsString("baz: boo")); + assertThat(logs, containsString("foo: bar")); + assertThat(logs, containsString("baz: boo")); + } } /** @@ -63,12 +66,14 @@ public void testWaitUntilReadyWithSuccessWithCustomHeaders() { */ @Test public void testWaitUntilReadyWithTlsAndAllowUnsecure() { - waitUntilReadyAndSucceed( - startContainerWithCommand( + try ( + GenericContainer container = startContainerWithCommand( createHttpsShellCommand("200 OK", GOOD_RESPONSE_BODY, 8080), createHttpWaitStrategy(ready).usingTls().allowInsecure() ) - ); + ) { + waitUntilReadyAndSucceed(container); + } } /** @@ -77,12 +82,14 @@ public void testWaitUntilReadyWithTlsAndAllowUnsecure() { */ @Test public void testWaitUntilReadyWithUnauthorizedWithLambda() { - waitUntilReadyAndSucceed( - startContainerWithCommand( + try ( + GenericContainer container = startContainerWithCommand( createShellCommand("401 UNAUTHORIZED", GOOD_RESPONSE_BODY), createHttpWaitStrategy(ready).forStatusCodeMatching(it -> it >= 200 && it < 300 || it == 401) ) - ); + ) { + waitUntilReadyAndSucceed(container); + } } /** @@ -91,12 +98,14 @@ public void testWaitUntilReadyWithUnauthorizedWithLambda() { */ @Test public void testWaitUntilReadyWithManyStatusCodes() { - waitUntilReadyAndSucceed( - startContainerWithCommand( + try ( + GenericContainer container = startContainerWithCommand( createShellCommand("401 UNAUTHORIZED", GOOD_RESPONSE_BODY), createHttpWaitStrategy(ready).forStatusCode(300).forStatusCode(401).forStatusCode(500) ) - ); + ) { + waitUntilReadyAndSucceed(container); + } } /** @@ -106,15 +115,17 @@ public void testWaitUntilReadyWithManyStatusCodes() { */ @Test public void testWaitUntilReadyWithManyStatusCodesAndLambda() { - waitUntilReadyAndSucceed( - startContainerWithCommand( + try ( + GenericContainer container = startContainerWithCommand( createShellCommand("401 UNAUTHORIZED", GOOD_RESPONSE_BODY), createHttpWaitStrategy(ready) .forStatusCode(300) .forStatusCode(500) .forStatusCodeMatching(it -> it == 401) ) - ); + ) { + waitUntilReadyAndSucceed(container); + } } /** @@ -124,12 +135,14 @@ public void testWaitUntilReadyWithManyStatusCodesAndLambda() { */ @Test public void testWaitUntilReadyWithTimeoutAndWithManyStatusCodesAndLambda() { - waitUntilReadyAndTimeout( - startContainerWithCommand( + try ( + GenericContainer container = startContainerWithCommand( createShellCommand("401 UNAUTHORIZED", GOOD_RESPONSE_BODY), createHttpWaitStrategy(ready).forStatusCode(300).forStatusCodeMatching(it -> it == 500) ) - ); + ) { + waitUntilReadyAndTimeout(container); + } } /** @@ -141,12 +154,14 @@ public void testWaitUntilReadyWithTimeoutAndWithManyStatusCodesAndLambda() { */ @Test public void testWaitUntilReadyWithTimeoutAndWithLambdaShouldNotMatchOk() { - waitUntilReadyAndTimeout( - startContainerWithCommand( + try ( + GenericContainer container = startContainerWithCommand( createShellCommand("200 OK", GOOD_RESPONSE_BODY), createHttpWaitStrategy(ready).forStatusCodeMatching(it -> it >= 300) ) - ); + ) { + waitUntilReadyAndTimeout(container); + } } /** @@ -172,26 +187,30 @@ public void testWaitUntilReadyWithTimeoutAndBadResponseBody() { */ @Test public void testWaitUntilReadyWithSpecificPort() { - waitUntilReadyAndSucceed( - startContainerWithCommand( + try ( + GenericContainer container = startContainerWithCommand( createShellCommand("200 OK", GOOD_RESPONSE_BODY, 9090), createHttpWaitStrategy(ready).forPort(9090), 7070, 8080, 9090 ) - ); + ) { + waitUntilReadyAndSucceed(container); + } } @Test public void testWaitUntilReadyWithTimoutCausedByReadTimeout() { - waitUntilReadyAndTimeout( - startContainerWithCommand( + try ( + GenericContainer container = startContainerWithCommand( createShellCommand("0 Connection Refused", GOOD_RESPONSE_BODY, 9090), createHttpWaitStrategy(ready).forPort(9090).withReadTimeout(Duration.ofMillis(1)), 9090 ) - ); + ) { + waitUntilReadyAndTimeout(container); + } } /** From 36b1270a5b5e4c84fd4f22b1a15cc3c9f232d42d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Thu, 26 May 2022 09:47:32 -0500 Subject: [PATCH 2/3] Fix format --- .../junit/wait/strategy/AbstractWaitStrategyTest.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/core/src/test/java/org/testcontainers/junit/wait/strategy/AbstractWaitStrategyTest.java b/core/src/test/java/org/testcontainers/junit/wait/strategy/AbstractWaitStrategyTest.java index 786be0377f2..530638f0f70 100644 --- a/core/src/test/java/org/testcontainers/junit/wait/strategy/AbstractWaitStrategyTest.java +++ b/core/src/test/java/org/testcontainers/junit/wait/strategy/AbstractWaitStrategyTest.java @@ -84,9 +84,7 @@ protected GenericContainer startContainerWithCommand( * @param shellCommand the shell command to execute */ protected void waitUntilReadyAndSucceed(String shellCommand) { - try ( - GenericContainer container = startContainerWithCommand(shellCommand) - ) { + try (GenericContainer container = startContainerWithCommand(shellCommand)) { waitUntilReadyAndSucceed(container); } } From e6cbcf5f72ff229185995e9eb04ab67a01a3b5b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez=20Gonzales?= Date: Thu, 26 May 2022 09:55:51 -0500 Subject: [PATCH 3/3] Update core/src/test/java/org/testcontainers/junit/wait/strategy/HttpWaitStrategyTest.java Co-authored-by: Sergei Egorov --- .../junit/wait/strategy/HttpWaitStrategyTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/test/java/org/testcontainers/junit/wait/strategy/HttpWaitStrategyTest.java b/core/src/test/java/org/testcontainers/junit/wait/strategy/HttpWaitStrategyTest.java index bcd0129fc99..972bf2ab66a 100644 --- a/core/src/test/java/org/testcontainers/junit/wait/strategy/HttpWaitStrategyTest.java +++ b/core/src/test/java/org/testcontainers/junit/wait/strategy/HttpWaitStrategyTest.java @@ -46,7 +46,7 @@ public void testWaitUntilReadyWithSuccessWithCustomHeaders() { HashMap headers = new HashMap<>(); headers.put("baz", "boo"); try ( - GenericContainer container = startContainerWithCommand( + GenericContainer container = startContainerWithCommand( createShellCommand("200 OK", GOOD_RESPONSE_BODY), createHttpWaitStrategy(ready).withHeader("foo", "bar").withHeaders(headers) )