Skip to content

Commit

Permalink
harness: consume output of balancer when running tests (#1221)
Browse files Browse the repository at this point in the history
* grab stdout & stderror

* minor change

* spawn tasks to relog the child process logs

---------

Co-authored-by: Carson McManus <[email protected]>
  • Loading branch information
Victor-M-Giraldo and dyc3 authored Jan 14, 2024
1 parent 8cba966 commit 8338d83
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions crates/harness/src/test_runner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{collections::HashMap, net::SocketAddr, sync::Arc};
use std::{collections::HashMap, net::SocketAddr, process::Stdio, sync::Arc};

use tokio::process::{Child, Command};
use tokio::{
io::{AsyncBufReadExt, BufReader},
process::{Child, Command},
};

use test_context::AsyncTestContext;
use tracing::warn;
Expand Down Expand Up @@ -82,10 +85,12 @@ impl TestRunner {
"debug",
])
.envs(envs)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("Failed to start balancer");
println!("waiting for balancer to start");
loop {
println!("waiting for balancer to start");
match client
.get(&format!("http://localhost:{}/api/status", opts.port))
.send()
Expand All @@ -109,6 +114,36 @@ impl TestRunner {
}
}

let child_stdout = BufReader::new(child.stdout.take().expect("failed to get child stdout"));
let child_stderr = BufReader::new(child.stderr.take().expect("failed to get child stderr"));

async fn relog_lines(
mut reader: BufReader<impl tokio::io::AsyncRead + Unpin>,
prefix: &str,
) {
let mut line = String::new();
loop {
line.clear();
match reader.read_line(&mut line).await {
Ok(0) => break,
Ok(_) => {
println!("{}: {}", prefix, line);
}
Err(e) => {
println!("{} error: {}", prefix, e);
break;
}
}
}
}

tokio::spawn(async move {
relog_lines(child_stdout, "balancer stdout").await;
});
tokio::spawn(async move {
relog_lines(child_stderr, "balancer stderr").await;
});

Ok(child)
}

Expand Down

0 comments on commit 8338d83

Please sign in to comment.