diff options
author | Andy Hayden <andyhayden1@gmail.com> | 2019-10-29 13:06:14 -0700 |
---|---|---|
committer | Ry Dahl <ry@tinyclouds.org> | 2019-10-29 16:06:14 -0400 |
commit | 49e04fb240ce700a6871d6b490536a879afd99f5 (patch) | |
tree | 6015f4ea285fff2f10f1971da562c5a345711eba | |
parent | 0d41b10ade91039339173476848f7368b6dc0853 (diff) |
fix race condition in test http server (#3237)
-rw-r--r-- | cli/test_util.rs | 15 | ||||
-rwxr-xr-x | tools/http_server.py | 16 |
2 files changed, 17 insertions, 14 deletions
diff --git a/cli/test_util.rs b/cli/test_util.rs index e10c0af7c..2eb452bca 100644 --- a/cli/test_util.rs +++ b/cli/test_util.rs @@ -8,6 +8,7 @@ use std::path::PathBuf; use std::process::Child; use std::process::Command; +use std::process::Stdio; use std::sync::Mutex; use std::sync::MutexGuard; @@ -62,16 +63,18 @@ pub fn http_server<'a>() -> HttpServerGuard<'a> { let g = GUARD.lock().unwrap(); println!("tools/http_server.py starting..."); - let child = Command::new("python") + let mut child = Command::new("python") .current_dir(root_path()) - .arg("tools/http_server.py") + .args(&["-u", "tools/http_server.py"]) + .stdout(Stdio::piped()) .spawn() .expect("failed to execute child"); - // Wait 1 second for the server to come up. TODO(ry) this is Racy. - std::thread::sleep(std::time::Duration::from_secs(2)); - - println!("tools/http_server.py ready"); + let stdout = child.stdout.as_mut().unwrap(); + use std::io::{BufRead, BufReader}; + let mut lines = BufReader::new(stdout).lines(); + let line = lines.next().unwrap().unwrap(); + assert!(line.starts_with("ready")); HttpServerGuard { child, g } } diff --git a/tools/http_server.py b/tools/http_server.py index 0a8212b82..76efab73b 100755 --- a/tools/http_server.py +++ b/tools/http_server.py @@ -178,20 +178,20 @@ def spawn(): while any(not s.thread.is_alive() for s in servers): sleep(0.01) try: - yield + print "ready" + yield servers finally: for s in servers: s.server.shutdown() def main(): - servers = (server(), redirect_server(), another_redirect_server(), - double_redirects_server(), inf_redirects_server()) - try: - while all(s.thread.is_alive() for s in servers): - sleep(10) - except KeyboardInterrupt: - pass + with spawn() as servers: + try: + while all(s.thread.is_alive() for s in servers): + sleep(1) + except KeyboardInterrupt: + pass sys.exit(1) |