fix race condition in test http server (#3237)

This commit is contained in:
Andy Hayden 2019-10-29 13:06:14 -07:00 committed by Ry Dahl
parent 0d41b10ade
commit 49e04fb240
2 changed files with 17 additions and 14 deletions

View file

@ -8,6 +8,7 @@
use std::path::PathBuf; use std::path::PathBuf;
use std::process::Child; use std::process::Child;
use std::process::Command; use std::process::Command;
use std::process::Stdio;
use std::sync::Mutex; use std::sync::Mutex;
use std::sync::MutexGuard; use std::sync::MutexGuard;
@ -62,16 +63,18 @@ pub fn http_server<'a>() -> HttpServerGuard<'a> {
let g = GUARD.lock().unwrap(); let g = GUARD.lock().unwrap();
println!("tools/http_server.py starting..."); println!("tools/http_server.py starting...");
let child = Command::new("python") let mut child = Command::new("python")
.current_dir(root_path()) .current_dir(root_path())
.arg("tools/http_server.py") .args(&["-u", "tools/http_server.py"])
.stdout(Stdio::piped())
.spawn() .spawn()
.expect("failed to execute child"); .expect("failed to execute child");
// Wait 1 second for the server to come up. TODO(ry) this is Racy. let stdout = child.stdout.as_mut().unwrap();
std::thread::sleep(std::time::Duration::from_secs(2)); use std::io::{BufRead, BufReader};
let mut lines = BufReader::new(stdout).lines();
println!("tools/http_server.py ready"); let line = lines.next().unwrap().unwrap();
assert!(line.starts_with("ready"));
HttpServerGuard { child, g } HttpServerGuard { child, g }
} }

View file

@ -178,18 +178,18 @@ def spawn():
while any(not s.thread.is_alive() for s in servers): while any(not s.thread.is_alive() for s in servers):
sleep(0.01) sleep(0.01)
try: try:
yield print "ready"
yield servers
finally: finally:
for s in servers: for s in servers:
s.server.shutdown() s.server.shutdown()
def main(): def main():
servers = (server(), redirect_server(), another_redirect_server(), with spawn() as servers:
double_redirects_server(), inf_redirects_server())
try: try:
while all(s.thread.is_alive() for s in servers): while all(s.thread.is_alive() for s in servers):
sleep(10) sleep(1)
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
sys.exit(1) sys.exit(1)