From e465a55b9ef3f49024bb5446a50a7b30d18313b2 Mon Sep 17 00:00:00 2001 From: faumaray Date: Sun, 25 Oct 2026 02:21:55 +0300 Subject: [PATCH] Give the fallback gateway test a port that stays refused the_next_published_gateway_is_used_when_one_fails took its "port nothing listens on" by binding a listener to port 0 and dropping it at once. A port that is free only for a moment can be given to another socket before the engine connects to it: the other tests of the binary bind port 0 for their fake gateways and proxies, and the one that gets the port answers the connection, so the test then passes over a gateway that is no longer closed, or fails on the 650 ms deadline. The test now binds a TCP socket and keeps it, without listening, for its whole length: the port is taken, so nobody else gets it, and a connection to it is refused at once. It checks that refusal before it relies on it. Co-Authored-By: Claude Sonnet 5.5 --- crates/voelin-core/tests/gateway.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/voelin-core/tests/gateway.rs b/crates/voelin-core/tests/gateway.rs index a623282..d4b254b 100644 --- a/crates/voelin-core/tests/gateway.rs +++ b/crates/voelin-core/tests/gateway.rs @@ -816,7 +816,16 @@ async fn not_found_proxy() -> String { #[tokio::test(flavor = "multi_thread")] async fn the_next_published_gateway_is_used_when_one_fails() { let proxy = not_found_proxy().await; - let closed = std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port(); + // A port that is taken and never listened on: connecting is refused at once, + // and no other test of this process can be given it meanwhile (one that was + // only free for a moment could be, the listeners of the others bind port 0). + let reserved = tokio::net::TcpSocket::new_v4().unwrap(); + reserved.bind("127.0.0.1:0".parse().unwrap()).unwrap(); + let closed = reserved.local_addr().unwrap().port(); + assert!( + tokio::net::TcpStream::connect(("127.0.0.1", closed)).await.is_err(), + "something listens on port {closed}" + ); let gw = FakeGateway::start().await; let path = temp_db("fallback"); let engine = Engine::start_with(Settings::in_memory(), History::open(&path).unwrap());