Skip to content

Commit db8a8e9

Browse files
authored
Merge pull request #256 from quanweiZhou/add-tests
Add tests
2 parents 8caf5ec + e4d0d20 commit db8a8e9

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

.github/workflows/bvt.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ jobs:
2929
make -C compiler
3030
make -C ttrpc-codegen
3131
make -C example build-examples
32+
# It's important for windows to fail correctly
33+
# https://github.com/actions/runner-images/issues/6668
34+
shell: bash
3235

3336
deny:
3437
runs-on: ubuntu-latest

example/client.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,36 @@ mod utils;
1818
use log::LevelFilter;
1919
use protocols::sync::{agent, agent_ttrpc, health, health_ttrpc};
2020
use std::thread;
21+
use std::time::Duration;
2122
use ttrpc::context::{self, Context};
2223
use ttrpc::error::Error;
2324
use ttrpc::proto::Code;
2425
use ttrpc::Client;
2526

27+
#[cfg(not(target_os = "linux"))]
28+
fn get_fd_count() -> usize {
29+
// currently not support get fd count
30+
0
31+
}
32+
33+
#[cfg(target_os = "linux")]
34+
fn get_fd_count() -> usize {
35+
let path = "/proc/self/fd";
36+
let count = std::fs::read_dir(path).unwrap().count();
37+
println!("get fd count {}", count);
38+
count
39+
}
40+
2641
fn main() {
42+
let expected_fd_count = get_fd_count();
43+
connect_once();
44+
// Give some time for fd to be released in the other thread
45+
thread::sleep(Duration::from_secs(1));
46+
let current_fd_count = get_fd_count();
47+
assert_eq!(current_fd_count, expected_fd_count, "check fd count");
48+
}
49+
50+
fn connect_once() {
2751
simple_logging::log_to_stderr(LevelFilter::Trace);
2852

2953
let c = Client::connect(utils::SOCK_ADDR).unwrap();
@@ -53,7 +77,10 @@ fn main() {
5377
panic!("not expecting an error from the example server: {:?}", e)
5478
}
5579
Ok(x) => {
56-
panic!("not expecting a OK response from the example server: {:?}", x)
80+
panic!(
81+
"not expecting a OK response from the example server: {:?}",
82+
x
83+
)
5784
}
5885
}
5986
println!(
@@ -107,7 +134,10 @@ fn main() {
107134
panic!("not expecting an error from the example server: {:?}", e)
108135
}
109136
Ok(s) => {
110-
panic!("not expecting a OK response from the example server: {:?}", s)
137+
panic!(
138+
"not expecting a OK response from the example server: {:?}",
139+
s
140+
)
111141
}
112142
};
113143
println!(
@@ -116,9 +146,6 @@ fn main() {
116146
now.elapsed()
117147
);
118148

119-
println!("\nsleep 2 seconds ...\n");
120-
thread::sleep(std::time::Duration::from_secs(2));
121-
122149
let version = hc.version(default_ctx(), &health::CheckRequest::new());
123150
assert_eq!("mock.0.1", version.as_ref().unwrap().agent_version.as_str());
124151
assert_eq!("0.0.1", version.as_ref().unwrap().grpc_version.as_str());

tests/sync-test.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{
22
io::{BufRead, BufReader},
3-
process::Command,
3+
process::{Child, Command},
44
time::Duration,
55
};
66

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

3030
match client.try_wait() {
3131
Ok(Some(status)) => {
32+
println!(
33+
"Client exited with status: {:?} success {}",
34+
&status,
35+
&status.success()
36+
);
37+
wait_with_output("client", client);
3238
client_succeeded = status.success();
3339
break;
3440
}
@@ -45,6 +51,7 @@ fn run_sync_example() -> Result<(), Box<dyn std::error::Error>> {
4551

4652
// be sure to clean up the server, the client should have run to completion
4753
server.kill()?;
54+
wait_with_output("server", server);
4855
assert!(client_succeeded);
4956
Ok(())
5057
}
@@ -59,3 +66,18 @@ fn run_example(example: &str) -> Command {
5966
.current_dir("example");
6067
cmd
6168
}
69+
70+
fn wait_with_output(name: &str, cmd: Child) {
71+
if let Ok(output) = cmd.wait_with_output() {
72+
println!("==== {name} output begin");
73+
println!("==== stdout:");
74+
output.stdout.lines().for_each(|line| {
75+
println!("{}", line.unwrap());
76+
});
77+
println!("==== stderr:");
78+
output.stderr.lines().for_each(|line| {
79+
println!("{}", line.unwrap());
80+
});
81+
println!("==== {name} output end");
82+
}
83+
}

0 commit comments

Comments
 (0)