diff options
Diffstat (limited to 'crates/rocie-server/tests/_testenv')
| -rw-r--r-- | crates/rocie-server/tests/_testenv/init.rs | 57 | ||||
| -rw-r--r-- | crates/rocie-server/tests/_testenv/mod.rs | 2 |
2 files changed, 37 insertions, 22 deletions
diff --git a/crates/rocie-server/tests/_testenv/init.rs b/crates/rocie-server/tests/_testenv/init.rs index 5309fea..9cb0b91 100644 --- a/crates/rocie-server/tests/_testenv/init.rs +++ b/crates/rocie-server/tests/_testenv/init.rs @@ -12,17 +12,29 @@ use std::{ env, ffi::OsStr, fmt::Write, - fs, io, mem, + fs, + io::{self, BufRead, BufReader}, + mem, path::{Path, PathBuf}, process::{self, Stdio}, - thread::sleep, - time::Duration, }; use rocie_client::apis::configuration::Configuration; use crate::{_testenv::Paths, testenv::TestEnv}; +macro_rules! function_name { + () => {{ + fn f() {} + fn type_name_of<T>(_: T) -> &'static str { + std::any::type_name::<T>() + } + let name = type_name_of(f); + name.strip_suffix("::{{closure}}::f").unwrap() + }}; +} +pub(crate) use function_name; + fn target_dir() -> PathBuf { // Tests exe is in target/debug/deps, the *rocie-server* exe is in target/debug env::current_exe() @@ -36,8 +48,8 @@ fn target_dir() -> PathBuf { .to_path_buf() } -fn test_dir(name: &'static str, port: u32) -> PathBuf { - target_dir().join("tests").join(name).join(port.to_string()) +fn test_dir(name: &'static str) -> PathBuf { + target_dir().join("tests").join(name) } fn prepare_files_and_dirs(test_dir: &Path) -> io::Result<Paths> { @@ -83,25 +95,24 @@ fn rocie_base_path(port: &str) -> String { format!("http://127.0.0.1:{port}") } -fn rocie_server_args<'a>(paths: &'a Paths, port: &'a str) -> [&'a OsStr; 5] { +fn rocie_server_args(paths: &Paths) -> [&OsStr; 4] { [ OsStr::new("serve"), OsStr::new("--db-path"), paths.db.as_os_str(), - OsStr::new("--port"), - OsStr::new(port), + OsStr::new("--print-port"), ] } impl TestEnv { - pub(crate) fn new(name: &'static str, port: u32) -> TestEnv { - let test_dir = test_dir(name, port); + pub(crate) fn new(name: &'static str) -> TestEnv { + let test_dir = test_dir(name); let paths = prepare_files_and_dirs(&test_dir) .inspect_err(|err| panic!("Error during test dir preparation: {err}")) .unwrap(); - let server_process = { + let (server_process, port) = { let server_exe = find_server_exe(); let mut cmd = process::Command::new(&server_exe); @@ -110,15 +121,22 @@ impl TestEnv { cmd.stdout(Stdio::piped()); cmd.stderr(Stdio::piped()); - cmd.args(rocie_server_args(&paths, port.to_string().as_str())); + cmd.args(rocie_server_args(&paths)); + + let mut child = cmd.spawn().expect("server spawn"); + + let port: u16 = { + let mut stdout = BufReader::new(child.stdout.as_mut().expect("Was captured")); - let child = cmd.spawn().expect("server spawn"); + let mut port = String::new(); + assert_ne!(stdout.read_line(&mut port).expect("Works"), 0); - // Give the server time to start. - // TODO(@bpeetz): Use a better synchronization primitive <2025-09-11> - sleep(Duration::from_millis(240)); + port.trim_end() + .parse() + .expect("The server should reply with a u16 port number") + }; - child + (child, port) }; let config = { @@ -201,10 +219,7 @@ impl Drop for TestEnv { eprintln!( "{}", - format_exit( - rocie_server_args(&self.paths, &self.port).as_slice(), - &output - ) + format_exit(rocie_server_args(&self.paths).as_slice(), &output) ); } } diff --git a/crates/rocie-server/tests/_testenv/mod.rs b/crates/rocie-server/tests/_testenv/mod.rs index a37925e..56cea71 100644 --- a/crates/rocie-server/tests/_testenv/mod.rs +++ b/crates/rocie-server/tests/_testenv/mod.rs @@ -4,7 +4,7 @@ use std::{path::PathBuf, process}; use rocie_client::apis::configuration::Configuration; -mod init; +pub(crate) mod init; pub(crate) mod log; /// Environment for the integration tests. |
