check if server correctly start

in same time than the port was opened.
Rename function into `wait_for_server` according.
This commit is contained in:
Gwen Lg 2024-06-13 21:55:02 +02:00
parent d4937cb5e3
commit 929fa56bed

View File

@ -1,7 +1,7 @@
use crate::server::utils::{get_free_port, wait_for_port}; use crate::server::utils::{get_free_port, wait_for_server};
use mktemp::Temp; use mktemp::Temp;
use std::io::{self, Cursor, Read}; use std::io::{self, Cursor, Read};
use std::process::{Child, Stdio}; use std::process::{Child, ExitStatus, Stdio};
use thiserror::Error; use thiserror::Error;
use zip::result::ZipError; use zip::result::ZipError;
use zip::ZipArchive; use zip::ZipArchive;
@ -14,6 +14,12 @@ pub enum Error {
#[error("Get an available port failed")] #[error("Get an available port failed")]
GetFreePort(#[source] io::Error), GetFreePort(#[source] io::Error),
#[error("Server exit with `{status}`")]
ServerExitWithStatus { status: ExitStatus },
#[error("Error append during check grammalecte-server status")]
ServerCheckStatus(#[source] io::Error),
#[error("Port {port} did not open in time!")] #[error("Port {port} did not open in time!")]
WaitPortOpen { port: u16 }, WaitPortOpen { port: u16 },
@ -81,7 +87,7 @@ impl EmbeddedServer {
log::info!("Will execute file {}", server_file); log::info!("Will execute file {}", server_file);
// Start server // Start server
let child = std::process::Command::new("/usr/bin/python3") let mut child = std::process::Command::new("/usr/bin/python3")
.arg(server_file) .arg(server_file)
.arg("-p") .arg("-p")
.arg(port.to_string()) .arg(port.to_string())
@ -90,7 +96,7 @@ impl EmbeddedServer {
.spawn() .spawn()
.map_err(Error::StartServerProcess)?; .map_err(Error::StartServerProcess)?;
wait_for_port(port)?; wait_for_server(&mut child, port)?;
Ok(Self { Ok(Self {
_srv_dir: dest, _srv_dir: dest,
@ -112,9 +118,9 @@ impl Drop for EmbeddedServer {
} }
mod utils { mod utils {
use std::time::Duration;
use super::Error; use super::Error;
use std::process::Child;
use std::time::Duration;
/// Get a free port /// Get a free port
pub fn get_free_port() -> u16 { pub fn get_free_port() -> u16 {
@ -131,8 +137,9 @@ mod utils {
port port
} }
pub fn wait_for_port(port: u16) -> Result<(), Error> { pub fn wait_for_server(child: &mut Child, port: u16) -> Result<(), Error> {
for _ in 0..50 { for _ in 0..50 {
check_server(child)?;
if port_scanner::scan_port(port) { if port_scanner::scan_port(port) {
return Ok(()); return Ok(());
} }
@ -141,4 +148,11 @@ mod utils {
Err(Error::WaitPortOpen { port }) Err(Error::WaitPortOpen { port })
} }
fn check_server(child: &mut Child) -> Result<(), Error> {
match child.try_wait().map_err(Error::ServerCheckStatus)? {
None => Ok(()), // Continue
Some(status) => Err(Error::ServerExitWithStatus { status }),
}
}
} }