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

Support multiple HTTP status codes for HttpWaitStrategy #630

Merged
merged 17 commits into from
Apr 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ All notable changes to this project will be documented in this file.

### Changed

- Support multiple HTTP status codes for HttpWaitStrategy ([\#630](https://github.com/testcontainers/testcontainers-java/issues/630))

## [1.7.0] - 2018-04-07

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public static HostPortWaitStrategy forListeningPort() {
*/
public static HttpWaitStrategy forHttp(String path) {
return new HttpWaitStrategy()
.forPath(path)
.forStatusCode(HttpURLConnection.HTTP_OK);
.forPath(path);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
Expand All @@ -32,11 +33,16 @@ public class HttpWaitStrategy extends AbstractWaitStrategy {
private static final String AUTH_BASIC = "Basic ";

private String path = "/";
private int statusCode = HttpURLConnection.HTTP_OK;
private Set<Integer> statusCodes = new HashSet<>();
private boolean tlsEnabled;
private String username;
private String password;
private Predicate<String> responsePredicate;
private Predicate<Integer> statusCodePredicate = responseCode -> {
// If we did not provide any status code, we assume by default HttpURLConnection.HTTP_OK
if (statusCodes.isEmpty() && HttpURLConnection.HTTP_OK == responseCode) return true;
return statusCodes.contains(responseCode);
};

/**
* Waits for the given status code.
Expand All @@ -45,7 +51,17 @@ public class HttpWaitStrategy extends AbstractWaitStrategy {
* @return this
*/
public HttpWaitStrategy forStatusCode(int statusCode) {
this.statusCode = statusCode;
statusCodes.add(statusCode);
return this;
}

/**
* Waits for the status code to pass the given predicate
* @param statusCodePredicate The predicate to test the response against
* @return this
*/
public HttpWaitStrategy forStatusCodeMatching(Predicate<Integer> statusCodePredicate) {
this.statusCodePredicate = this.statusCodePredicate.or(statusCodePredicate);
return this;
}

Expand Down Expand Up @@ -122,7 +138,7 @@ protected void waitUntilReady() {
connection.setRequestMethod("GET");
connection.connect();

if (statusCode != connection.getResponseCode()) {
if (!statusCodePredicate.test(connection.getResponseCode())) {
throw new RuntimeException(String.format("HTTP response code was: %s",
connection.getResponseCode()));
}
Expand All @@ -144,7 +160,8 @@ protected void waitUntilReady() {

} catch (TimeoutException e) {
throw new ContainerLaunchException(String.format(
"Timed out waiting for URL to be accessible (%s should return HTTP %s)", uri, statusCode));
"Timed out waiting for URL to be accessible (%s should return HTTP %s)", uri, statusCodes.isEmpty() ?
HttpURLConnection.HTTP_OK : statusCodes));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public static HostPortWaitStrategy forListeningPort() {
*/
public static HttpWaitStrategy forHttp(String path) {
return new HttpWaitStrategy()
.forPath(path)
.forStatusCode(HttpURLConnection.HTTP_OK);
.forPath(path);
}

/**
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
package org.testcontainers.junit.wait;
package org.testcontainers.junit.wait.strategy;
Copy link
Member

@bsideup bsideup Apr 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... I'm not sure these changes are related to the PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's somewhat needed to test the new methods I added. See strategy/HttpWaitStrategyTest.java class.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should just start using new implementation (under org.testcontainers.containers.wait.strategy package) in HttpWaitStrategyTest & AbstractWaitStrategyTest since the old classes are just wrappers now

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dadoonet ping :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum. I'm missing something. I probably misunderstood what you are expecting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I added a call to my method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I added a call to my method

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should just fix the imports in the old test and start using new wait strategies, so that you can also add your new method to that test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me. I'll do that. In which case, I'll also move the tests to the new package, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thanks :)


import org.jetbrains.annotations.NotNull;
import org.junit.Before;
import org.rnorth.ducttape.RetryCountExceededException;
import org.rnorth.visibleassertions.VisibleAssertions;
import org.testcontainers.containers.ContainerLaunchException;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.WaitStrategy;
import org.testcontainers.containers.wait.strategy.WaitStrategy;

import java.time.Duration;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;

/**
* Common test methods for {@link WaitStrategy} implementations.
*
* @author Pete Cornish {@literal <[email protected]>}
*/
public abstract class AbstractWaitStrategyTest<W extends WaitStrategy> {
private static final long WAIT_TIMEOUT_MILLIS = 3000;
private static final String IMAGE_NAME = "alpine:latest";
static final long WAIT_TIMEOUT_MILLIS = 3000;
static final String IMAGE_NAME = "alpine:latest";

/**
* Indicates that the WaitStrategy has completed waiting successfully.
*/
private AtomicBoolean ready;
AtomicBoolean ready;

/**
* Subclasses should return an instance of {@link W} that sets {@code ready} to {@code true},
Expand All @@ -50,14 +50,23 @@ public void setUp() throws Exception {
* @return the (unstarted) container
*/
private GenericContainer startContainerWithCommand(String shellCommand) {
final WaitStrategy waitStrategy = buildWaitStrategy(ready)
.withStartupTimeout(Duration.ofMillis(WAIT_TIMEOUT_MILLIS));
return startContainerWithCommand(shellCommand, buildWaitStrategy(ready));
}

/**
* Starts a GenericContainer with the {@link #IMAGE_NAME} image, passing the given {@code shellCommand} as
* a parameter to {@literal sh -c} (the container CMD) and apply a give wait strategy.
* Note that the timeout will be overwritten if any with {@link #WAIT_TIMEOUT_MILLIS}.
* @param shellCommand the shell command to execute
* @param waitStrategy The wait strategy to apply
* @return the (unstarted) container
*/
protected GenericContainer startContainerWithCommand(String shellCommand, WaitStrategy waitStrategy) {
// apply WaitStrategy to container
return new GenericContainer(IMAGE_NAME)
.withExposedPorts(8080)
.withCommand("sh", "-c", shellCommand)
.waitingFor(waitStrategy);
.waitingFor(waitStrategy.withStartupTimeout(Duration.ofMillis(WAIT_TIMEOUT_MILLIS)));
}

/**
Expand All @@ -66,13 +75,7 @@ private GenericContainer startContainerWithCommand(String shellCommand) {
* @param shellCommand the shell command to execute
*/
protected void waitUntilReadyAndSucceed(String shellCommand) {
final GenericContainer container = startContainerWithCommand(shellCommand);

// start() blocks until successful or timeout
container.start();

assertTrue(String.format("Expected container to be ready after timeout of %sms",
WAIT_TIMEOUT_MILLIS), ready.get());
waitUntilReadyAndSucceed(startContainerWithCommand(shellCommand));
}

/**
Expand All @@ -82,12 +85,33 @@ protected void waitUntilReadyAndSucceed(String shellCommand) {
* @param shellCommand the shell command to execute
*/
protected void waitUntilReadyAndTimeout(String shellCommand) {
final GenericContainer container = startContainerWithCommand(shellCommand);
waitUntilReadyAndTimeout(startContainerWithCommand(shellCommand));
}

/**
* Expects that the WaitStrategy throws a {@link RetryCountExceededException} after unsuccessful connection
* to a container with a listening port.
*
* @param container the container to start
*/
protected void waitUntilReadyAndTimeout(GenericContainer container) {
// start() blocks until successful or timeout
VisibleAssertions.assertThrows("an exception is thrown when timeout occurs (" + WAIT_TIMEOUT_MILLIS + "ms)",
ContainerLaunchException.class,
container::start);

}

/**
* Expects that the WaitStrategy returns successfully after connection to a container with a listening port.
*
* @param container the container to start
*/
protected void waitUntilReadyAndSucceed(GenericContainer container) {
// start() blocks until successful or timeout
container.start();

assertTrue(String.format("Expected container to be ready after timeout of %sms",
WAIT_TIMEOUT_MILLIS), ready.get());
}
}
Loading