-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
bsideup
merged 17 commits into
testcontainers:master
from
dadoonet:pr/http-multiple-ports
Apr 13, 2018
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
cf637ce
Support multiple HTTP status codes for HttpWaitStrategy
dadoonet da44bd9
Revert changes in Deprecated class
dadoonet 0af2eab
Merge branch 'master' into pr/http-multiple-ports
dadoonet e5ddd16
Revert changes in Deprecated class
dadoonet 1b7cbaf
Make forStatusCode chainable and add forStatusCodeMatching method
dadoonet f2d1592
Fix quality
dadoonet bf12100
Fix compile issue
dadoonet 3b34909
Support both Predicates and Status codes
dadoonet afe06dc
Fix setter
dadoonet 9f1b28a
Revert unrelated change
dadoonet cb4875d
Merge branch 'master' into pr/http-multiple-ports
dadoonet bb39373
Fix error message (note that with predicates it might be incorrect th…
dadoonet 10ca9cd
Fix default predicate
dadoonet 370c35a
Move WaitStrategy tests to the right package
dadoonet 808dcf8
Add a test (and fix the bug!)
dadoonet d6837f7
Add more tests
dadoonet 6b24c2a
Add entry in changelog
dadoonet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 0 additions & 48 deletions
48
core/src/test/java/org/testcontainers/junit/wait/HostPortWaitStrategyTest.java
This file was deleted.
Oops, something went wrong.
72 changes: 0 additions & 72 deletions
72
core/src/test/java/org/testcontainers/junit/wait/HttpWaitStrategyTest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
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}, | ||
|
@@ -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))); | ||
} | ||
|
||
/** | ||
|
@@ -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)); | ||
} | ||
|
||
/** | ||
|
@@ -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()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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) inHttpWaitStrategyTest
&AbstractWaitStrategyTest
since the old classes are just wrappers nowThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dadoonet ping :)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, thanks :)