1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-29 16:30:56 -05:00

chore: share HTTP server between tests (#3966)

This commit is contained in:
Bartek Iwańczuk 2020-02-11 22:02:35 +01:00 committed by GitHub
parent 63718ab305
commit e6167c7813
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -8,11 +8,13 @@ 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::process::Stdio;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::sync::Mutex; use std::sync::Mutex;
use std::sync::MutexGuard;
lazy_static! { lazy_static! {
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 { pub fn root_path() -> PathBuf {
@ -35,32 +37,40 @@ pub fn deno_exe_path() -> PathBuf {
p p
} }
pub struct HttpServerGuard<'a> { pub struct HttpServerGuard {}
#[allow(dead_code)]
g: MutexGuard<'a, ()>,
child: Child,
}
impl<'a> Drop for HttpServerGuard<'a> { impl Drop for HttpServerGuard {
fn drop(&mut self) { fn drop(&mut self) {
match self.child.try_wait() { let count = SERVER_COUNT.fetch_sub(1, Ordering::SeqCst);
Ok(None) => { // If no more tests hold guard we can kill the server
self.child.kill().expect("failed to kill http_server.py");
} if count == 1 {
Ok(Some(status)) => { kill_http_server();
panic!("http_server.py exited unexpectedly {}", status)
}
Err(e) => panic!("http_server.py err {}", e),
} }
} }
} }
/// Starts tools/http_server.py when the returned guard is dropped, the server fn kill_http_server() {
/// will be killed. let mut server_guard = SERVER.lock().unwrap();
pub fn http_server<'a>() -> HttpServerGuard<'a> { let mut child = server_guard
// TODO(ry) Allow tests to use the http server in parallel. .take()
let g = GUARD.lock().unwrap(); .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);
}
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..."); println!("tools/http_server.py starting...");
let mut child = Command::new("python") let mut child = Command::new("python")
.current_dir(root_path()) .current_dir(root_path())
@ -74,6 +84,9 @@ pub fn http_server<'a>() -> HttpServerGuard<'a> {
let mut lines = BufReader::new(stdout).lines(); let mut lines = BufReader::new(stdout).lines();
let line = lines.next().unwrap().unwrap(); let line = lines.next().unwrap().unwrap();
assert!(line.starts_with("ready")); assert!(line.starts_with("ready"));
server_guard.replace(child);
}
}
HttpServerGuard { child, g } HttpServerGuard {}
} }