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