Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add try-with-resources to HttpWaitStrategyTest #5397

Merged
merged 3 commits into from May 26, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -84,7 +84,9 @@ 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);
}
}

/**
Expand All @@ -94,7 +96,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);
}
}

/**
Expand Down
Expand Up @@ -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}.
Expand Down Expand Up @@ -45,16 +45,19 @@ public void testWaitUntilReadyWithSuccess() {
public void testWaitUntilReadyWithSuccessWithCustomHeaders() {
HashMap<String, String> 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(
eddumelendez marked this conversation as resolved.
Show resolved Hide resolved
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"));
}
}

/**
Expand All @@ -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);
}
}

/**
Expand All @@ -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);
}
}

/**
Expand All @@ -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);
}
}

/**
Expand All @@ -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);
}
}

/**
Expand All @@ -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);
}
}

/**
Expand All @@ -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);
}
}

/**
Expand All @@ -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);
}
}

/**
Expand Down