Skip to content

Commit da12f5e

Browse files
committed
test(core): harden free port availability check
1 parent 66fe6f0 commit da12f5e

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

crates/openshell-core/src/forward.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,12 +1019,28 @@ mod tests {
10191019
#[test]
10201020
fn check_port_available_free_port() {
10211021
// Bind to port 0 to get an OS-assigned free port, then drop the
1022-
// listener so the port is released before we test it.
1023-
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
1024-
let port = listener.local_addr().unwrap().port();
1025-
drop(listener);
1022+
// listener so the port is released before we test it. On busy CI
1023+
// hosts, another process can claim that single ephemeral port before
1024+
// we re-bind it, so retry with fresh OS-assigned ports.
1025+
let mut last_error = None;
1026+
for _ in 0..20 {
1027+
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
1028+
let port = listener.local_addr().unwrap().port();
1029+
drop(listener);
1030+
1031+
match check_port_available(&ForwardSpec::new(port)) {
1032+
Ok(()) => return,
1033+
Err(err) => {
1034+
last_error = Some(err.to_string());
1035+
std::thread::sleep(std::time::Duration::from_millis(10));
1036+
}
1037+
}
1038+
}
10261039

1027-
assert!(check_port_available(&ForwardSpec::new(port)).is_ok());
1040+
panic!(
1041+
"expected an OS-assigned port to be available; last error: {}",
1042+
last_error.unwrap_or_else(|| "none".to_string())
1043+
);
10281044
}
10291045

10301046
#[test]

0 commit comments

Comments
 (0)