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

Restart WebDriver on failure #4267

Merged
merged 1 commit into from
Nov 14, 2024
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
* Deprecate `--reference-types` in favor of automatic target feature detection.
[#4237](https://github.com/rustwasm/wasm-bindgen/pull/4237)

* `wasm-bindgen-test-runner` now tries to restart the WebDriver on failure, instead of spending its timeout period trying to connect to a non-existing WebDriver.
[#4267](https://github.com/rustwasm/wasm-bindgen/pull/4267)

### Fixed

* Fixed methods with `self: &Self` consuming the object.
Expand Down
71 changes: 46 additions & 25 deletions crates/cli/src/bin/wasm-bindgen-test-runner/headless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,36 +61,49 @@ pub fn run(server: &SocketAddr, shell: &Shell, timeout: u64) -> Result<(), Error
let driver_url = match driver.location() {
Locate::Remote(url) => Ok(url.clone()),
Locate::Local((path, args)) => {
// Allow tests to run in parallel (in theory) by finding any open port
// available for our driver. We can't bind the port for the driver, but
// hopefully the OS gives this invocation unique ports across processes
let driver_addr = TcpListener::bind("127.0.0.1:0")?.local_addr()?;

// Spawn the driver binary, collecting its stdout/stderr in separate
// threads. We'll print this output later.
let mut cmd = Command::new(path);
cmd.args(args).arg(format!("--port={}", driver_addr.port()));
let mut child = BackgroundChild::spawn(path, &mut cmd, shell)?;
drop_log = Box::new(move || {
let _ = &child;
child.print_stdio_on_drop = false;
});

// Wait for the driver to come online and bind its port before we try to
// connect to it.
let start = Instant::now();
let max = Duration::new(5, 0);
let mut bound = false;
while start.elapsed() < max {
if TcpStream::connect(driver_addr).is_ok() {
bound = true;
break;

let (driver_addr, mut child) = 'outer: loop {
// Allow tests to run in parallel (in theory) by finding any open port
// available for our driver. We can't bind the port for the driver, but
// hopefully the OS gives this invocation unique ports across processes
let driver_addr = TcpListener::bind("127.0.0.1:0")?.local_addr()?;
// Spawn the driver binary, collecting its stdout/stderr in separate
// threads. We'll print this output later.
let mut cmd = Command::new(path);
cmd.args(args).arg(format!("--port={}", driver_addr.port()));
let mut child = BackgroundChild::spawn(path, &mut cmd, shell)?;

// Wait for the driver to come online and bind its port before we try to
// connect to it.
loop {
if child.has_failed() {
if start.elapsed() >= max {
bail!("driver failed to start")
}

println!("Failed to start driver, trying again ...");

thread::sleep(Duration::from_millis(100));
break;
} else if TcpStream::connect(driver_addr).is_ok() {
break 'outer (driver_addr, child);
} else if start.elapsed() >= max {
bail!("driver failed to bind port during startup")
} else {
thread::sleep(Duration::from_millis(100));
}
}
thread::sleep(Duration::from_millis(100));
}
if !bound {
bail!("driver failed to bind port during startup")
}
};

drop_log = Box::new(move || {
let _ = &child;
child.print_stdio_on_drop = false;
});

Url::parse(&format!("http://{}", driver_addr)).map_err(Error::from)
}
}?;
Expand Down Expand Up @@ -646,6 +659,14 @@ impl<'a> BackgroundChild<'a> {
print_stdio_on_drop: true,
})
}

fn has_failed(&mut self) -> bool {
match self.child.try_wait() {
Ok(Some(status)) => !status.success(),
Ok(None) => false,
Err(_) => true,
}
}
}

impl<'a> Drop for BackgroundChild<'a> {
Expand Down