From 115d45d0f0ef79e1d14b09846d6f08c3f3038cd2 Mon Sep 17 00:00:00 2001 From: Youngjoon Lee Date: Tue, 29 Aug 2023 20:25:05 +0900 Subject: [PATCH] Revert/improve `get_available_port()` in tests (#334) --- tests/src/lib.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/tests/src/lib.rs b/tests/src/lib.rs index e3096c0b..8166eb45 100644 --- a/tests/src/lib.rs +++ b/tests/src/lib.rs @@ -1,21 +1,25 @@ mod nodes; pub use nodes::NomosNode; +use once_cell::sync::Lazy; // std -use std::fmt::Debug; use std::net::TcpListener; use std::time::Duration; +use std::{fmt::Debug, sync::Mutex}; //crates use fraction::Fraction; use rand::{thread_rng, Rng}; +static NET_PORT: Lazy> = Lazy::new(|| Mutex::new(thread_rng().gen_range(8000..10000))); + pub fn get_available_port() -> u16 { - let mut port: u16 = thread_rng().gen_range(8000..10000); - while TcpListener::bind(("127.0.0.1", port)).is_err() { - port += 1; + let mut port = NET_PORT.lock().unwrap(); + *port += 1; + while TcpListener::bind(("127.0.0.1", *port)).is_err() { + *port += 1; } - port + *port } #[async_trait::async_trait]