Skip to content

Commit 8c5f923

Browse files
committed
ut: Add test_server_lifetime
Add ut for server's lifetime. Fixes: #284 Signed-off-by: jokemanfire <hu.dingyang@zte.com.cn> Signed-off-by: Tim Zhang <tim@hyper.sh>
1 parent ed0e560 commit 8c5f923

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
@@ -601,3 +601,36 @@ impl HandlerContext {
601601
.ok();
602602
}
603603
}
604+
605+
#[cfg(target_os = "linux")]
606+
#[cfg(test)]
607+
mod tests {
608+
use super::*;
609+
610+
pub const SOCK_ADDR: &str = r"unix://@/tmp/ttrpc-server-unit-test";
611+
612+
pub fn is_socket_in_use(sock_path: &str) -> bool {
613+
let output = std::process::Command::new("bash")
614+
.args(["-c", &format!("lsof -U|grep {}", sock_path)])
615+
.output()
616+
.expect("Failed to execute lsof command");
617+
618+
output.status.success()
619+
}
620+
621+
#[tokio::test]
622+
async fn test_server_lifetime() {
623+
let addr = SOCK_ADDR
624+
.strip_prefix("unix://@")
625+
.expect("socket address is not expected");
626+
{
627+
let mut server = Server::new().bind(SOCK_ADDR).unwrap();
628+
server.start().await.unwrap();
629+
assert!(is_socket_in_use(addr));
630+
}
631+
632+
// Sleep to wait for shutdown of server caused by server's lifetime over
633+
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
634+
assert!(!is_socket_in_use(addr));
635+
}
636+
}

0 commit comments

Comments
 (0)