Make server test startup failures diagnosable
This commit is contained in:
1 parent
140e1b4c50
commit
594a57e414
1 file changed
+22
-6
+22
-6
@@ -4,7 +4,7 @@
|
|||||||
//! the suite can run in parallel and never touches the developer's `projects/`.
|
//! the suite can run in parallel and never touches the developer's `projects/`.
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
fs,
|
fs::{self, File},
|
||||||
io::{Read, Write},
|
io::{Read, Write},
|
||||||
net::{TcpListener, TcpStream},
|
net::{TcpListener, TcpStream},
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
@@ -64,6 +64,7 @@ impl TestServer {
|
|||||||
.expect("copy input");
|
.expect("copy input");
|
||||||
|
|
||||||
let port = free_port();
|
let port = free_port();
|
||||||
|
let log = File::create(root.join("server.log")).expect("create server log");
|
||||||
let child = Command::new(env!("CARGO_BIN_EXE_texrra"))
|
let child = Command::new(env!("CARGO_BIN_EXE_texrra"))
|
||||||
.arg("serve")
|
.arg("serve")
|
||||||
.arg(root.join("hello.tex"))
|
.arg(root.join("hello.tex"))
|
||||||
@@ -72,24 +73,39 @@ impl TestServer {
|
|||||||
.env("TEXRRA_WEB_DIR", &web)
|
.env("TEXRRA_WEB_DIR", &web)
|
||||||
.env("TEXRRA_PROJECTS_DIR", root.join("projects"))
|
.env("TEXRRA_PROJECTS_DIR", root.join("projects"))
|
||||||
.stdout(Stdio::null())
|
.stdout(Stdio::null())
|
||||||
.stderr(Stdio::inherit())
|
.stderr(Stdio::from(log))
|
||||||
.spawn()
|
.spawn()
|
||||||
.expect("spawn texrra serve");
|
.expect("spawn texrra serve");
|
||||||
let server = Self { child, port, root };
|
let mut server = Self { child, port, root };
|
||||||
server.wait_ready();
|
server.wait_ready();
|
||||||
server
|
server
|
||||||
}
|
}
|
||||||
|
|
||||||
fn wait_ready(&self) {
|
fn wait_ready(&mut self) {
|
||||||
for _ in 0..120 {
|
for _ in 0..600 {
|
||||||
if let Ok(response) = send(self.port, "GET", "/", &[], "text/plain") {
|
if let Ok(response) = send(self.port, "GET", "/", &[], "text/plain") {
|
||||||
if response.status == 200 {
|
if response.status == 200 {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if let Some(status) = self.child.try_wait().expect("poll server") {
|
||||||
|
panic!(
|
||||||
|
"server on port {} exited with {status}:\n{}",
|
||||||
|
self.port,
|
||||||
|
self.log()
|
||||||
|
);
|
||||||
|
}
|
||||||
std::thread::sleep(Duration::from_millis(50));
|
std::thread::sleep(Duration::from_millis(50));
|
||||||
}
|
}
|
||||||
panic!("server on port {} did not become ready", self.port);
|
panic!(
|
||||||
|
"server on port {} did not become ready within 30s:\n{}",
|
||||||
|
self.port,
|
||||||
|
self.log()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn log(&self) -> String {
|
||||||
|
fs::read_to_string(self.root.join("server.log")).unwrap_or_default()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn request(&self, method: &str, path: &str, body: &[u8], content_type: &str) -> Response {
|
fn request(&self, method: &str, path: &str, body: &[u8], content_type: &str) -> Response {
|
||||||
|
|||||||
Reference in new issue
Block a user