motorcortex-rust 0.5.0

Motorcortex Rust: a Rust client for the Motorcortex Core real-time control system (async + blocking).
Documentation
use std::process::{Child, Command};
use std::sync::Once;
use std::thread;
use std::time::Duration;

// Use Once to ensure the server starts only once
static INIT: Once = Once::new();
static mut SERVER: Option<Child> = None;

#[ctor::ctor]
pub fn global_setup() {
    // This block will be executed before any tests start
    INIT.call_once(|| {
        println!("Starting test server...");

        let server_path = format!(
            "{}/tests/server/build/test_server",
            env!("CARGO_MANIFEST_DIR")
        );
        let server_config = format!(
            "{}/tests/server/config/config.json",
            env!("CARGO_MANIFEST_DIR")
        );
        println!("Server path: {}", server_path);
        let server_process = Command::new(server_path)
            .arg("-c")
            .arg(server_config)
            .arg("-s") // Update this path
            .spawn()
            .expect("Failed to start test server");

        // Use unsafe static mut to store the server process
        unsafe {
            SERVER = Some(server_process);
        }

        // Wait for the server to initialize
        thread::sleep(Duration::from_secs(5)); // Adjust the delay as needed
        println!("Test server started successfully.");
    });
}

#[ctor::dtor]
#[allow(static_mut_refs)]
pub fn global_cleanup() {
    // Accessing mutable static with unsafe
    unsafe {
        // Take ownership of the SERVER value
        if let Some(mut server_process) = SERVER.take() {
            println!("Stopping the test server...");
            // Kill the process
            if let Err(e) = server_process.kill() {
                println!("Failed to kill the test server: {:?}", e);
            } else {
                println!("Test server successfully killed.");
            }

            // Wait for the process to exit
            if let Err(e) = server_process.wait() {
                println!("Failed to clean up the test server: {:?}", e);
            } else {
                println!("Test server exited cleanly.");
            }
        } else {
            println!("No server process is running.");
        }
    }
}