summaryrefslogtreecommitdiff
path: root/test_util/src/lib.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2020-06-18 18:40:29 +0200
committerGitHub <noreply@github.com>2020-06-18 12:40:29 -0400
commit70147ee56418c3d67a29070c01746a44d353e855 (patch)
tree7776def02a886d4a0772d8e10153abbea882cda9 /test_util/src/lib.rs
parent90c5dcfe79cd010b429e775a3ebcb3c5d78fa6ff (diff)
chore: share HTTP server between tests (#6362)
Diffstat (limited to 'test_util/src/lib.rs')
-rw-r--r--test_util/src/lib.rs95
1 files changed, 55 insertions, 40 deletions
diff --git a/test_util/src/lib.rs b/test_util/src/lib.rs
index 85cfa0879..bf9b10498 100644
--- a/test_util/src/lib.rs
+++ b/test_util/src/lib.rs
@@ -12,8 +12,9 @@ use std::process::Child;
use std::process::Command;
use std::process::Output;
use std::process::Stdio;
+use std::sync::atomic::AtomicUsize;
+use std::sync::atomic::Ordering;
use std::sync::Mutex;
-use std::sync::MutexGuard;
use tempfile::TempDir;
pub const PERMISSION_VARIANTS: [&str; 5] =
@@ -29,7 +30,8 @@ lazy_static! {
r"[\x1b\x9b][\[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-PRZcf-nqry=><]"
).unwrap();
- static ref GUARD: Mutex<()> = Mutex::new(());
+ static ref SERVER: Mutex<Option<Child>> = Mutex::new(None);
+ static ref SERVER_COUNT: AtomicUsize = AtomicUsize::new(0);
}
pub fn root_path() -> PathBuf {
@@ -56,55 +58,68 @@ pub fn deno_exe_path() -> PathBuf {
p
}
-pub struct HttpServerGuard<'a> {
- #[allow(dead_code)]
- g: MutexGuard<'a, ()>,
- child: Child,
-}
+pub struct HttpServerGuard {}
-impl<'a> Drop for HttpServerGuard<'a> {
+impl Drop for HttpServerGuard {
fn drop(&mut self) {
- match self.child.try_wait() {
- Ok(None) => {
- self.child.kill().expect("failed to kill http_server.py");
- }
- Ok(Some(status)) => {
- panic!("http_server.py exited unexpectedly {}", status)
- }
- Err(e) => panic!("http_server.py err {}", e),
+ let count = SERVER_COUNT.fetch_sub(1, Ordering::SeqCst);
+ // If no more tests hold guard we can kill the server
+
+ if count == 1 {
+ kill_http_server();
}
}
}
-/// Starts tools/http_server.py when the returned guard is dropped, the server
-/// will be killed.
-pub fn http_server<'a>() -> HttpServerGuard<'a> {
- // TODO(bartlomieju) Allow tests to use the http server in parallel.
- let g = GUARD.lock().unwrap();
+fn kill_http_server() {
+ let mut server_guard = SERVER.lock().unwrap();
+ let mut child = server_guard
+ .take()
+ .expect("Trying to kill server but already killed");
+ match child.try_wait() {
+ Ok(None) => {
+ child.kill().expect("failed to kill http_server.py");
+ }
+ Ok(Some(status)) => panic!("http_server.py exited unexpectedly {}", status),
+ Err(e) => panic!("http_server.py error: {}", e),
+ }
+ drop(server_guard);
+}
- println!("tools/http_server.py starting...");
- let mut child = Command::new("python")
- .current_dir(root_path())
- .args(&["-u", "tools/http_server.py"])
- .stdout(Stdio::piped())
- .spawn()
- .expect("failed to execute child");
-
- let stdout = child.stdout.as_mut().unwrap();
- use std::io::{BufRead, BufReader};
- let lines = BufReader::new(stdout).lines();
- // Wait for "ready" on stdout. See tools/http_server.py
- for maybe_line in lines {
- if let Ok(line) = maybe_line {
- if line.starts_with("ready") {
- break;
+/// Starts tools/http_server.py when the returned guard is dropped and there are
+// no more guard being held, the server will be killed.
+pub fn http_server() -> HttpServerGuard {
+ SERVER_COUNT.fetch_add(1, Ordering::SeqCst);
+
+ {
+ let mut server_guard = SERVER.lock().unwrap();
+ if server_guard.is_none() {
+ println!("tools/http_server.py starting...");
+ let mut child = Command::new("python")
+ .current_dir(root_path())
+ .args(&["-u", "tools/http_server.py"])
+ .stdout(Stdio::piped())
+ .spawn()
+ .expect("failed to execute child");
+
+ let stdout = child.stdout.as_mut().unwrap();
+ use std::io::{BufRead, BufReader};
+ let lines = BufReader::new(stdout).lines();
+ // Wait for "ready" on stdout. See tools/http_server.py
+ for maybe_line in lines {
+ if let Ok(line) = maybe_line {
+ if line.starts_with("ready") {
+ break;
+ }
+ } else {
+ panic!(maybe_line.unwrap_err());
+ }
}
- } else {
- panic!(maybe_line.unwrap_err());
+ server_guard.replace(child);
}
}
- HttpServerGuard { child, g }
+ HttpServerGuard {}
}
/// Helper function to strip ansi codes.