Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

tests: hold "nothing listens" ports so a parallel test can't take them (#675)

Oleksandr Ivanchenko23eefb184fc7
diff --git a/tests/browser_recovery_test.rs b/tests/browser_recovery_test.rs
index 7a76c5ad..44459e5b 100644
--- a/tests/browser_recovery_test.rs
+++ b/tests/browser_recovery_test.rs
@@ -153,8 +153,11 @@ async fn a_browser_that_stays_stalled_is_reported_busy_and_retryable() {
#[tokio::test]
async fn a_browser_that_is_not_there_gets_the_docker_text_at_once() {
- // a port nothing listens on: connection refused
- let closed = TcpListener::bind("127.0.0.1:0").await.unwrap().local_addr().unwrap().port();
+ // a port nothing listens on: connection refused. The socket stays bound (never listening) for
+ // the whole test, so no parallel test's server is given this port; a freed port could go to a
+ // stalled mock browser, which turned "not there" into "overloaded" after 40 s (#649 CI)
+ let refusing = refusing_port();
+ let closed = refusing.local_addr().unwrap().port();
let tmp = tempfile::tempdir().unwrap();
let (_state, id, port, app_task) = server(format!("http://127.0.0.1:{closed}"), &tmp).await;
@@ -168,6 +171,27 @@ async fn a_browser_that_is_not_there_gets_the_docker_text_at_once() {
assert!(started.elapsed() < Duration::from_secs(5), "a refused connection isn't retried for 45 s");
app_task.abort();
+ drop(refusing);
+}
+
+/// A loopback port that refuses every connection for as long as the socket lives: bound, never
+/// listening, and without SO_REUSEADDR so no other socket can bind it meanwhile.
+fn refusing_port() -> tokio::net::TcpSocket {
+ let socket = tokio::net::TcpSocket::new_v4().unwrap();
+ socket.set_reuseaddr(false).unwrap();
+ socket.bind("127.0.0.1:0".parse().unwrap()).unwrap();
+ socket
+}
+
+#[tokio::test]
+async fn a_held_refusing_port_refuses_and_cannot_be_taken() {
+ let refusing = refusing_port();
+ let addr = refusing.local_addr().unwrap();
+ let err = tokio::net::TcpStream::connect(addr).await.unwrap_err();
+ assert_eq!(err.kind(), std::io::ErrorKind::ConnectionRefused);
+ // a server binding the same port (tokio's TcpListener sets SO_REUSEADDR) is refused
+ assert!(TcpListener::bind(addr).await.is_err(), "the port is held for the whole test");
+ drop(refusing);
}
#[tokio::test]
diff --git a/tests/custom_api_test.rs b/tests/custom_api_test.rs
index d23f864a..23c90994 100644
--- a/tests/custom_api_test.rs
+++ b/tests/custom_api_test.rs
@@ -139,10 +139,14 @@ async fn fake_server(lists: bool) -> (String, Seen) {
(root, seen)
}
-/// A port nothing listens on.
-async fn closed_port() -> u16 {
- let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
- listener.local_addr().unwrap().port()
+/// A port nothing listens on, held for as long as the socket lives: bound, never listening and
+/// without SO_REUSEADDR, so a parallel test's server can't be given it the way a freed port can.
+fn closed_port() -> (tokio::net::TcpSocket, u16) {
+ let socket = tokio::net::TcpSocket::new_v4().unwrap();
+ socket.set_reuseaddr(false).unwrap();
+ socket.bind("127.0.0.1:0".parse().unwrap()).unwrap();
+ let port = socket.local_addr().unwrap().port();
+ (socket, port)
}
async fn call(app: &Router, method: &str, uri: &str, body: Option<Value>) -> (StatusCode, Value) {
@@ -437,7 +441,7 @@ async fn a_rejected_key_is_shown_on_the_account_and_refuses_the_turn() {
#[tokio::test]
async fn a_server_that_is_not_running_says_so() {
let env = setup().await;
- let port = closed_port().await;
+ let (_closed, port) = closed_port();
let space = env.space("Offline").await;
let (s, acct) = env
.account(
diff --git a/tests/token_cli_test.rs b/tests/token_cli_test.rs
index a3a5056b..a9750a8d 100644
--- a/tests/token_cli_test.rs
+++ b/tests/token_cli_test.rs
@@ -101,8 +101,12 @@ fn token_without_a_running_server_fails_without_printing_the_token() {
let dir = tempfile::tempdir().unwrap();
let tok = "abcdefghijklmnopqrstuvwxyz012345";
std::fs::write(dir.path().join("api-token"), format!("{tok}\n")).unwrap();
- // A port nobody listens on
- let port = std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port();
+ // A port nobody listens on: bound and never listening for the whole test, so a parallel
+ // test's server can't be given it the way a freed port can
+ let refusing = tokio::net::TcpSocket::new_v4().unwrap();
+ refusing.set_reuseaddr(false).unwrap();
+ refusing.bind("127.0.0.1:0".parse().unwrap()).unwrap();
+ let port = refusing.local_addr().unwrap().port();
let out = kwnt_token_link(dir.path(), port);
let stdout = String::from_utf8_lossy(&out.stdout);
let stderr = String::from_utf8_lossy(&out.stderr);
@@ -110,6 +114,7 @@ fn token_without_a_running_server_fails_without_printing_the_token() {
assert!(!stdout.contains(tok) && !stderr.contains(tok), "{stdout}\n{stderr}");
assert!(stderr.contains("Is `kwnt serve` running?"), "{stderr}");
assert!(stderr.contains("kwnt token --raw"), "{stderr}");
+ drop(refusing);
}
#[test]