Skip to content

Commit c1f8cde

Browse files
committed
ut: Add test_server_lifetime
Add ut for server's lifetime. Fixes: #284 Signed-off-by: jokemanfire <[email protected]> Signed-off-by: Tim Zhang <[email protected]>
1 parent bf2a6fb commit c1f8cde

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/asynchronous/server.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,3 +686,36 @@ impl HandlerContext {
686686
.ok();
687687
}
688688
}
689+
690+
#[cfg(target_os = "linux")]
691+
#[cfg(test)]
692+
mod tests {
693+
use super::*;
694+
695+
pub const SOCK_ADDR: &str = r"unix://@/tmp/ttrpc-server-unit-test";
696+
697+
pub fn is_socket_in_use(sock_path: &str) -> bool {
698+
let output = std::process::Command::new("bash")
699+
.args(["-c", &format!("lsof -U|grep {}", sock_path)])
700+
.output()
701+
.expect("Failed to execute lsof command");
702+
703+
output.status.success()
704+
}
705+
706+
#[tokio::test]
707+
async fn test_server_lifetime() {
708+
let addr = SOCK_ADDR
709+
.strip_prefix("unix://@")
710+
.expect("socket address is not expected");
711+
{
712+
let mut server = Server::new().bind(SOCK_ADDR).unwrap();
713+
server.start().await.unwrap();
714+
assert!(is_socket_in_use(addr));
715+
}
716+
717+
// Sleep to wait for shutdown of server caused by server's lifetime over
718+
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
719+
assert!(!is_socket_in_use(addr));
720+
}
721+
}

0 commit comments

Comments
 (0)