0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-02-08 15:21:26 -05:00

Revert "chore: share HTTP server between tests (#3966)" (#4165)

This reverts commit e6167c7813.
This commit is contained in:
Bartek Iwańczuk 2020-02-28 08:32:02 +01:00 committed by GitHub
parent 1cb1ab6c00
commit 9b6a9b769e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -8,13 +8,11 @@ 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 SERVER: Mutex<Option<Child>> = Mutex::new(None); static ref GUARD: Mutex<()> = Mutex::new(());
static ref SERVER_COUNT: AtomicUsize = AtomicUsize::new(0);
} }
pub fn root_path() -> PathBuf { pub fn root_path() -> PathBuf {
@ -37,39 +35,32 @@ pub fn deno_exe_path() -> PathBuf {
p p
} }
pub struct HttpServerGuard {} pub struct HttpServerGuard<'a> {
#[allow(dead_code)]
g: MutexGuard<'a, ()>,
child: Child,
}
impl Drop for HttpServerGuard { impl<'a> Drop for HttpServerGuard<'a> {
fn drop(&mut self) { fn drop(&mut self) {
let count = SERVER_COUNT.fetch_sub(1, Ordering::SeqCst); match self.child.try_wait() {
// If no more tests hold guard we can kill the server
if count == 1 {
kill_http_server();
}
}
}
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) => { Ok(None) => {
child.kill().expect("failed to kill http_server.py"); 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),
} }
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 { /// Starts tools/http_server.py when the returned guard is dropped, the server
SERVER_COUNT.fetch_add(1, Ordering::SeqCst); /// will be killed.
{ pub fn http_server<'a>() -> HttpServerGuard<'a> {
let mut server_guard = SERVER.lock().unwrap(); // TODO(bartlomieju) Allow tests to use the http server in parallel.
if server_guard.is_none() { let g = GUARD.lock().unwrap();
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())
@ -85,14 +76,12 @@ pub fn http_server() -> HttpServerGuard {
for maybe_line in lines { for maybe_line in lines {
if let Ok(line) = maybe_line { if let Ok(line) = maybe_line {
if line.starts_with("ready") { if line.starts_with("ready") {
server_guard.replace(child);
break; break;
} }
} else { } else {
panic!(maybe_line.unwrap_err()); panic!(maybe_line.unwrap_err());
} }
} }
}
} HttpServerGuard { child, g }
HttpServerGuard {}
} }