Skip to content

Commit fb5278b

Browse files
quanweiZhouquanwei.zqw
authored andcommitted
example: check client fd leak
check client fd leak Fixes: #255 Signed-off-by: Quanwei Zhou <quanweiZhou@linux.alibaba.com>
1 parent ffc8735 commit fb5278b

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

example/client.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,36 @@ use ttrpc::error::Error;
2323
use ttrpc::proto::Code;
2424
use ttrpc::Client;
2525

26+
#[cfg(not(target_os = "linux"))]
27+
fn get_fd_count() -> usize {
28+
// currently not support get fd count
29+
0
30+
}
31+
32+
#[cfg(target_os = "linux")]
33+
fn get_fd_count() -> usize {
34+
let path = "/proc/self/fd";
35+
let count = std::fs::read_dir(path).unwrap().count();
36+
println!("get fd count {}", count);
37+
count
38+
}
39+
2640
fn main() {
41+
connect_once();
42+
let expected_fd_count = get_fd_count();
43+
44+
// connect 3 times and check the fd leak.
45+
for index in 0..3 {
46+
connect_once();
47+
let current_fd_count = get_fd_count();
48+
assert_eq!(
49+
expected_fd_count, current_fd_count,
50+
"check fd count in {index}"
51+
);
52+
}
53+
}
54+
55+
fn connect_once() {
2756
simple_logging::log_to_stderr(LevelFilter::Trace);
2857

2958
let c = Client::connect(utils::SOCK_ADDR).unwrap();

0 commit comments

Comments
 (0)