test: improve test_txt_public_stun_server with timeout and retry mechanism (#2014)

This commit is contained in:
Luna Yao
2026-03-26 02:32:07 +01:00
committed by GitHub
parent e2684a93de
commit 8e4dc508bb
+14 -11
View File
@@ -1342,6 +1342,7 @@ mod tests {
common::scoped_task::ScopedTask, common::scoped_task::ScopedTask,
tunnel::{udp::UdpTunnelListener, TunnelListener}, tunnel::{udp::UdpTunnelListener, TunnelListener},
}; };
use tokio::time::{sleep, timeout};
use super::*; use super::*;
@@ -1399,18 +1400,20 @@ mod tests {
async fn test_txt_public_stun_server() { async fn test_txt_public_stun_server() {
let stun_servers = vec!["txt:stun.easytier.cn".to_string()]; let stun_servers = vec!["txt:stun.easytier.cn".to_string()];
let detector = UdpNatTypeDetector::new(stun_servers, 1); let detector = UdpNatTypeDetector::new(stun_servers, 1);
for _ in 0..5 { timeout(Duration::from_secs(30), async {
let ret = detector.detect_nat_type(0).await; loop {
println!("{:#?}, {:?}", ret, ret.as_ref().map(|x| x.nat_type())); let ret = detector.detect_nat_type(0).await;
if let Ok(resp) = ret { println!("{:#?}, {:?}", ret, ret.as_ref().map(|x| x.nat_type()));
assert!(!resp.stun_resps.is_empty()); if let Ok(resp) = ret {
return; if !resp.stun_resps.is_empty() {
return;
}
}
sleep(Duration::from_secs(1)).await;
} }
} })
debug_assert!( .await
false, .expect("stun server should be available");
"should not reach here, stun server should be available"
);
} }
#[tokio::test] #[tokio::test]