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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
use std::{
io::{BufRead, BufReader},
process::Command,
time::Duration,
};
#[test]
fn run_sync_example() -> Result<(), Box<dyn std::error::Error>> {
// start the server and give it a moment to start.
let mut server = run_example("server").spawn().unwrap();
std::thread::sleep(Duration::from_secs(2));
let mut client = run_example("client").spawn().unwrap();
let mut client_succeeded = false;
let start = std::time::Instant::now();
let timeout = Duration::from_secs(600);
loop {
if start.elapsed() > timeout {
println!("Running the client timed out. output:");
client.kill().unwrap_or_else(|e| {
println!("This may occur on Windows if the process has exited: {e}");
});
let output = client.stdout.unwrap();
BufReader::new(output).lines().for_each(|line| {
println!("{}", line.unwrap());
});
break;
}
match client.try_wait() {
Ok(Some(status)) => {
client_succeeded = status.success();
break;
}
Ok(None) => {
// still running
continue;
}
Err(e) => {
println!("Error: {e}");
break;
}
}
}
// be sure to clean up the server, the client should have run to completion
server.kill()?;
assert!(client_succeeded);
Ok(())
}
fn run_example(example: &str) -> Command {
let mut cmd = Command::new("cargo");
cmd.arg("run")
.arg("--example")
.arg(example)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.current_dir("example");
cmd
}
|