Skip to content

Commit

Permalink
Merge pull request #256 from quanweiZhou/add-tests
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
Tim-Zhang authored Oct 9, 2024
2 parents 8caf5ec + e4d0d20 commit db8a8e9
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/bvt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ jobs:
make -C compiler
make -C ttrpc-codegen
make -C example build-examples
# It's important for windows to fail correctly
# https://github.com/actions/runner-images/issues/6668
shell: bash

deny:
runs-on: ubuntu-latest
Expand Down
37 changes: 32 additions & 5 deletions example/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,36 @@ mod utils;
use log::LevelFilter;
use protocols::sync::{agent, agent_ttrpc, health, health_ttrpc};
use std::thread;
use std::time::Duration;
use ttrpc::context::{self, Context};
use ttrpc::error::Error;
use ttrpc::proto::Code;
use ttrpc::Client;

#[cfg(not(target_os = "linux"))]
fn get_fd_count() -> usize {
// currently not support get fd count
0
}

#[cfg(target_os = "linux")]
fn get_fd_count() -> usize {
let path = "/proc/self/fd";
let count = std::fs::read_dir(path).unwrap().count();
println!("get fd count {}", count);
count
}

fn main() {
let expected_fd_count = get_fd_count();
connect_once();
// Give some time for fd to be released in the other thread
thread::sleep(Duration::from_secs(1));
let current_fd_count = get_fd_count();
assert_eq!(current_fd_count, expected_fd_count, "check fd count");
}

fn connect_once() {
simple_logging::log_to_stderr(LevelFilter::Trace);

let c = Client::connect(utils::SOCK_ADDR).unwrap();
Expand Down Expand Up @@ -53,7 +77,10 @@ fn main() {
panic!("not expecting an error from the example server: {:?}", e)
}
Ok(x) => {
panic!("not expecting a OK response from the example server: {:?}", x)
panic!(
"not expecting a OK response from the example server: {:?}",
x
)
}
}
println!(
Expand Down Expand Up @@ -107,7 +134,10 @@ fn main() {
panic!("not expecting an error from the example server: {:?}", e)
}
Ok(s) => {
panic!("not expecting a OK response from the example server: {:?}", s)
panic!(
"not expecting a OK response from the example server: {:?}",
s
)
}
};
println!(
Expand All @@ -116,9 +146,6 @@ fn main() {
now.elapsed()
);

println!("\nsleep 2 seconds ...\n");
thread::sleep(std::time::Duration::from_secs(2));

let version = hc.version(default_ctx(), &health::CheckRequest::new());
assert_eq!("mock.0.1", version.as_ref().unwrap().agent_version.as_str());
assert_eq!("0.0.1", version.as_ref().unwrap().grpc_version.as_str());
Expand Down
26 changes: 24 additions & 2 deletions tests/sync-test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
io::{BufRead, BufReader},
process::Command,
process::{Child, Command},
time::Duration,
};

Expand All @@ -22,13 +22,19 @@ fn run_sync_example() -> Result<(), Box<dyn std::error::Error>> {
});
let output = client.stdout.unwrap();
BufReader::new(output).lines().for_each(|line| {
println!("{}", line.unwrap());
println!("client output: {}", line.unwrap());
});
break;
}

match client.try_wait() {
Ok(Some(status)) => {
println!(
"Client exited with status: {:?} success {}",
&status,
&status.success()
);
wait_with_output("client", client);
client_succeeded = status.success();
break;
}
Expand All @@ -45,6 +51,7 @@ fn run_sync_example() -> Result<(), Box<dyn std::error::Error>> {

// be sure to clean up the server, the client should have run to completion
server.kill()?;
wait_with_output("server", server);
assert!(client_succeeded);
Ok(())
}
Expand All @@ -59,3 +66,18 @@ fn run_example(example: &str) -> Command {
.current_dir("example");
cmd
}

fn wait_with_output(name: &str, cmd: Child) {
if let Ok(output) = cmd.wait_with_output() {
println!("==== {name} output begin");
println!("==== stdout:");
output.stdout.lines().for_each(|line| {
println!("{}", line.unwrap());
});
println!("==== stderr:");
output.stderr.lines().for_each(|line| {
println!("{}", line.unwrap());
});
println!("==== {name} output end");
}
}

0 comments on commit db8a8e9

Please sign in to comment.