diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2026-01-07 21:54:53 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2026-01-07 22:04:33 +0000 |
| commit | fba872909559c42ac48b6acd697e61ab987e0f71 (patch) | |
| tree | 8c98768df4f759eb5f80558a6915e6eb92698fd3 /tests/common/mock_relay.rs | |
| parent | 457e296d90e2f7c2808e216f2ef0608b70f76553 (diff) | |
test: fix hanging unit tests issue
Diffstat (limited to 'tests/common/mock_relay.rs')
| -rw-r--r-- | tests/common/mock_relay.rs | 43 |
1 files changed, 23 insertions, 20 deletions
diff --git a/tests/common/mock_relay.rs b/tests/common/mock_relay.rs index 123c29e..b6376a7 100644 --- a/tests/common/mock_relay.rs +++ b/tests/common/mock_relay.rs | |||
| @@ -72,14 +72,35 @@ impl MockRelay { | |||
| 72 | /// The relay accepts all events without validation and stores them | 72 | /// The relay accepts all events without validation and stores them |
| 73 | /// in an in-memory database. | 73 | /// in an in-memory database. |
| 74 | pub async fn start() -> Self { | 74 | pub async fn start() -> Self { |
| 75 | let port = find_free_port(); | 75 | // Create and bind listener (eliminates port race condition) |
| 76 | Self::start_on_port(port).await | 76 | let std_listener = std::net::TcpListener::bind("127.0.0.1:0") |
| 77 | .expect("Failed to bind to random port"); | ||
| 78 | let port = std_listener | ||
| 79 | .local_addr() | ||
| 80 | .expect("Failed to get local addr") | ||
| 81 | .port(); | ||
| 82 | |||
| 83 | // Convert to tokio listener (keeps port bound) | ||
| 84 | std_listener | ||
| 85 | .set_nonblocking(true) | ||
| 86 | .expect("Failed to set non-blocking"); | ||
| 87 | let listener = TcpListener::from_std(std_listener) | ||
| 88 | .expect("Failed to convert to tokio listener"); | ||
| 89 | |||
| 90 | Self::start_with_listener(listener, port).await | ||
| 77 | } | 91 | } |
| 78 | 92 | ||
| 79 | /// Start a mock relay on a specific port. | 93 | /// Start a mock relay on a specific port. |
| 80 | pub async fn start_on_port(port: u16) -> Self { | 94 | pub async fn start_on_port(port: u16) -> Self { |
| 81 | let addr: SocketAddr = ([127, 0, 0, 1], port).into(); | 95 | let addr: SocketAddr = ([127, 0, 0, 1], port).into(); |
| 96 | let listener = TcpListener::bind(addr) | ||
| 97 | .await | ||
| 98 | .expect("Failed to bind to address"); | ||
| 99 | Self::start_with_listener(listener, port).await | ||
| 100 | } | ||
| 82 | 101 | ||
| 102 | /// Internal method to start the relay with an existing listener. | ||
| 103 | async fn start_with_listener(listener: TcpListener, port: u16) -> Self { | ||
| 83 | // Create a simple relay with no write policy (accepts all events) | 104 | // Create a simple relay with no write policy (accepts all events) |
| 84 | let relay = LocalRelayBuilder::default().build(); | 105 | let relay = LocalRelayBuilder::default().build(); |
| 85 | 106 | ||
| @@ -89,11 +110,6 @@ impl MockRelay { | |||
| 89 | // Clone relay for the server task | 110 | // Clone relay for the server task |
| 90 | let server_relay = relay.clone(); | 111 | let server_relay = relay.clone(); |
| 91 | 112 | ||
| 92 | // Start the HTTP/WebSocket server | ||
| 93 | let listener = TcpListener::bind(addr) | ||
| 94 | .await | ||
| 95 | .expect("Failed to bind to address"); | ||
| 96 | |||
| 97 | let handle = tokio::spawn(async move { | 113 | let handle = tokio::spawn(async move { |
| 98 | loop { | 114 | loop { |
| 99 | tokio::select! { | 115 | tokio::select! { |
| @@ -245,19 +261,6 @@ fn derive_accept_key(request_key: &[u8]) -> String { | |||
| 245 | base64::Engine::encode(&base64::engine::general_purpose::STANDARD, hash.as_byte_array()) | 261 | base64::Engine::encode(&base64::engine::general_purpose::STANDARD, hash.as_byte_array()) |
| 246 | } | 262 | } |
| 247 | 263 | ||
| 248 | /// Find a free port to use for the server. | ||
| 249 | fn find_free_port() -> u16 { | ||
| 250 | use std::net::TcpListener; | ||
| 251 | |||
| 252 | let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind to random port"); | ||
| 253 | let port = listener | ||
| 254 | .local_addr() | ||
| 255 | .expect("Failed to get local addr") | ||
| 256 | .port(); | ||
| 257 | drop(listener); | ||
| 258 | port | ||
| 259 | } | ||
| 260 | |||
| 261 | /// Wait for the server to be ready to accept connections. | 264 | /// Wait for the server to be ready to accept connections. |
| 262 | async fn wait_for_server_ready(port: u16) { | 265 | async fn wait_for_server_ready(port: u16) { |
| 263 | let max_attempts = 50; // 5 seconds total | 266 | let max_attempts = 50; // 5 seconds total |