Skip to content

Commit b3e229e

Browse files
authored
Merge pull request #298 from Tim-Zhang/fix-server-drop
async/server: Fix the the dropping of the server object
2 parents d79bd82 + 8c5f923 commit b3e229e

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/asynchronous/server.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ impl Server {
162162
fd_tx = stop_listen_rx.recv() => {
163163
if let Some(fd_tx) = fd_tx {
164164
fd_tx.send(incoming).await.unwrap();
165-
break;
166165
}
166+
break;
167167
}
168168
}
169169
}
@@ -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)