-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Switch browser containers to use LogMessageWaitStrategy #328
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,28 @@ | ||
package org.testcontainers.containers; | ||
|
||
import com.google.common.util.concurrent.Uninterruptibles; | ||
import com.github.dockerjava.api.command.InspectContainerResponse; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.junit.runner.Description; | ||
import org.openqa.selenium.remote.BrowserType; | ||
import org.openqa.selenium.remote.DesiredCapabilities; | ||
import org.openqa.selenium.remote.RemoteWebDriver; | ||
import org.rnorth.ducttape.unreliables.Unreliables; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.testcontainers.containers.traits.LinkableContainer; | ||
import org.testcontainers.containers.traits.VncService; | ||
import org.testcontainers.containers.wait.LogMessageWaitStrategy; | ||
|
||
import java.io.File; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.text.SimpleDateFormat; | ||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Date; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import static com.google.common.base.Preconditions.checkState; | ||
import static java.time.temporal.ChronoUnit.SECONDS; | ||
|
||
/** | ||
* A chrome/firefox/custom container based on SeleniumHQ's standalone container sets. | ||
|
@@ -55,14 +55,17 @@ public class BrowserWebDriverContainer<SELF extends BrowserWebDriverContainer<SE | |
/** | ||
*/ | ||
public BrowserWebDriverContainer() { | ||
|
||
this.waitStrategy = new LogMessageWaitStrategy() | ||
.withRegEx(".*RemoteWebDriver instances should connect to.*\n") | ||
.withStartupTimeout(Duration.of(15, SECONDS)); | ||
} | ||
|
||
/** | ||
* Constructor taking a specific webdriver container name and tag | ||
* @param dockerImageName | ||
*/ | ||
public BrowserWebDriverContainer(String dockerImageName) { | ||
this(); | ||
super.setDockerImageName(dockerImageName); | ||
this.customImageNameIsSet = true; | ||
} | ||
|
@@ -143,26 +146,18 @@ public int getPort() { | |
} | ||
|
||
@Override | ||
protected void waitUntilContainerStarted() { | ||
// Repeatedly try and open a webdriver session | ||
|
||
AtomicInteger backoff = new AtomicInteger(1000); | ||
|
||
driver = Unreliables.retryUntilSuccess(120, TimeUnit.SECONDS, () -> { | ||
Uninterruptibles.sleepUninterruptibly(backoff.getAndUpdate(current -> (int)(current * 1.5)), TimeUnit.MILLISECONDS); | ||
RemoteWebDriver driver = new RemoteWebDriver(getSeleniumAddress(), desiredCapabilities); | ||
driver.getCurrentUrl(); | ||
|
||
logger().info("Obtained a connection to container ({})", BrowserWebDriverContainer.this.getSeleniumAddress()); | ||
return driver; | ||
}); | ||
|
||
protected void containerIsStarted(InspectContainerResponse containerInfo) { | ||
if (recordingMode != VncRecordingMode.SKIP) { | ||
LOGGER.debug("Starting VNC recording"); | ||
VncRecordingSidekickContainer recordingSidekickContainer = new VncRecordingSidekickContainer<>(this); | ||
|
||
// Use multiple startup attempts due to race condition between Selenium being available and VNC being available | ||
VncRecordingSidekickContainer recordingSidekickContainer = new VncRecordingSidekickContainer<>(this) | ||
.withStartupAttempts(3); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. N.B. this has become necessary because we're now much quicker at starting the WebDriver connection; it exposes a race condition against the VNC socket. In my testing this led to ~5% of tests randomly failing due to the vnc recorder getting 'Connection Refused'. Adding multiple startup attempts for the VNC recorder resolves the issue; we could potentially include a wait in the VNC recorder (or here), but that adds a bit more complexity. |
||
|
||
recordingSidekickContainer.start(); | ||
currentVncRecordings.add(recordingSidekickContainer); | ||
} | ||
this.driver = new RemoteWebDriver(getSeleniumAddress(), desiredCapabilities); | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.testcontainers.junit; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.openqa.selenium.remote.DesiredCapabilities; | ||
import org.testcontainers.containers.BrowserWebDriverContainer; | ||
|
||
import java.io.IOException; | ||
import java.time.Duration; | ||
|
||
import static java.time.temporal.ChronoUnit.SECONDS; | ||
|
||
/** | ||
* | ||
*/ | ||
public class CustomWaitTimeoutWebDriverContainerTest extends BaseWebDriverContainerTest { | ||
|
||
@Rule | ||
public BrowserWebDriverContainer chromeWithCustomTimeout = new BrowserWebDriverContainer<>() | ||
.withDesiredCapabilities(DesiredCapabilities.chrome()) | ||
.withStartupTimeout(Duration.of(30, SECONDS)); | ||
|
||
@Test | ||
public void simpleTest() throws IOException { | ||
doSimpleWebdriverTest(chromeWithCustomTimeout); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No assertion in this test unfortunately; I couldn't think of a way to do it that wasn't over-engineered. In manual testing I've dialled this right down to 1 second to verify that the timeout is having an effect. |
||
} | ||
} |
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 can avoid having all this code, which is nice.