mirror of
https://github.com/denoland/deno.git
synced 2025-01-03 12:58:54 -05:00
chore: share HTTP server between tests (#6362)
This commit is contained in:
parent
90c5dcfe79
commit
70147ee564
1 changed files with 54 additions and 39 deletions
|
@ -12,8 +12,9 @@ use std::process::Child;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
use std::process::Output;
|
use std::process::Output;
|
||||||
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;
|
|
||||||
use tempfile::TempDir;
|
use tempfile::TempDir;
|
||||||
|
|
||||||
pub const PERMISSION_VARIANTS: [&str; 5] =
|
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=><]"
|
r"[\x1b\x9b][\[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-PRZcf-nqry=><]"
|
||||||
).unwrap();
|
).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 {
|
pub fn root_path() -> PathBuf {
|
||||||
|
@ -56,55 +58,68 @@ 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(bartlomieju) 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);
|
||||||
|
}
|
||||||
|
|
||||||
println!("tools/http_server.py starting...");
|
/// Starts tools/http_server.py when the returned guard is dropped and there are
|
||||||
let mut child = Command::new("python")
|
// no more guard being held, the server will be killed.
|
||||||
.current_dir(root_path())
|
pub fn http_server() -> HttpServerGuard {
|
||||||
.args(&["-u", "tools/http_server.py"])
|
SERVER_COUNT.fetch_add(1, Ordering::SeqCst);
|
||||||
.stdout(Stdio::piped())
|
|
||||||
.spawn()
|
|
||||||
.expect("failed to execute child");
|
|
||||||
|
|
||||||
let stdout = child.stdout.as_mut().unwrap();
|
{
|
||||||
use std::io::{BufRead, BufReader};
|
let mut server_guard = SERVER.lock().unwrap();
|
||||||
let lines = BufReader::new(stdout).lines();
|
if server_guard.is_none() {
|
||||||
// Wait for "ready" on stdout. See tools/http_server.py
|
println!("tools/http_server.py starting...");
|
||||||
for maybe_line in lines {
|
let mut child = Command::new("python")
|
||||||
if let Ok(line) = maybe_line {
|
.current_dir(root_path())
|
||||||
if line.starts_with("ready") {
|
.args(&["-u", "tools/http_server.py"])
|
||||||
break;
|
.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 {
|
server_guard.replace(child);
|
||||||
panic!(maybe_line.unwrap_err());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpServerGuard { child, g }
|
HttpServerGuard {}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper function to strip ansi codes.
|
/// Helper function to strip ansi codes.
|
||||||
|
|
Loading…
Reference in a new issue