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/`.
|
||||
|
||||
use std::{
|
||||
fs,
|
||||
fs::{self, File},
|
||||
io::{Read, Write},
|
||||
net::{TcpListener, TcpStream},
|
||||
path::{Path, PathBuf},
|
||||
@@ -64,6 +64,7 @@ impl TestServer {
|
||||
.expect("copy input");
|
||||
|
||||
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"))
|
||||
.arg("serve")
|
||||
.arg(root.join("hello.tex"))
|
||||
@@ -72,24 +73,39 @@ impl TestServer {
|
||||
.env("TEXRRA_WEB_DIR", &web)
|
||||
.env("TEXRRA_PROJECTS_DIR", root.join("projects"))
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::inherit())
|
||||
.stderr(Stdio::from(log))
|
||||
.spawn()
|
||||
.expect("spawn texrra serve");
|
||||
let server = Self { child, port, root };
|
||||
let mut server = Self { child, port, root };
|
||||
server.wait_ready();
|
||||
server
|
||||
}
|
||||
|
||||
fn wait_ready(&self) {
|
||||
for _ in 0..120 {
|
||||
fn wait_ready(&mut self) {
|
||||
for _ in 0..600 {
|
||||
if let Ok(response) = send(self.port, "GET", "/", &[], "text/plain") {
|
||||
if response.status == 200 {
|
||||
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));
|
||||
}
|
||||
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 {
|
||||
|
||||
Reference in new issue
Block a user