test_server/
lib.rs

1#[macro_use]
2extern crate lazy_static;
3use std::process::Child;
4use std::sync::atomic::{AtomicUsize, Ordering};
5
6// global unique portnumber between all test threads
7lazy_static!{ static ref PORT: AtomicUsize = AtomicUsize::new(22222); }
8
9pub struct ChildKiller(Child);
10
11impl Drop for ChildKiller {
12    fn drop(&mut self) {
13        let _ = self.0.kill();
14    }
15}
16
17pub fn start_dummy_server(port: Option<u16>) -> (ChildKiller, u16) {
18    use std::process::{Command, Stdio};
19    use std::thread::sleep;
20    use std::time::Duration;
21
22    // get and increment global port number for current test
23    let p =  match port {
24        Some(p) => p,
25        None => PORT.fetch_add(1, Ordering::SeqCst) as u16
26    };
27    let ck = ChildKiller(Command::new("./test-server/test-server")
28                             .arg(p.to_string())
29                             .stdout(Stdio::null())
30                             .spawn()
31                             .unwrap_or_else(|e| panic!("failed to execute process: {}", e)));
32    sleep(Duration::from_millis(500));
33    (ck, p)
34}