Revert/improve `get_available_port()` in tests (#334)
This commit is contained in:
parent
2bd8ea92b1
commit
115d45d0f0
|
@ -1,21 +1,25 @@
|
||||||
mod nodes;
|
mod nodes;
|
||||||
pub use nodes::NomosNode;
|
pub use nodes::NomosNode;
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
// std
|
// std
|
||||||
use std::fmt::Debug;
|
|
||||||
use std::net::TcpListener;
|
use std::net::TcpListener;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
use std::{fmt::Debug, sync::Mutex};
|
||||||
|
|
||||||
//crates
|
//crates
|
||||||
use fraction::Fraction;
|
use fraction::Fraction;
|
||||||
use rand::{thread_rng, Rng};
|
use rand::{thread_rng, Rng};
|
||||||
|
|
||||||
|
static NET_PORT: Lazy<Mutex<u16>> = Lazy::new(|| Mutex::new(thread_rng().gen_range(8000..10000)));
|
||||||
|
|
||||||
pub fn get_available_port() -> u16 {
|
pub fn get_available_port() -> u16 {
|
||||||
let mut port: u16 = thread_rng().gen_range(8000..10000);
|
let mut port = NET_PORT.lock().unwrap();
|
||||||
while TcpListener::bind(("127.0.0.1", port)).is_err() {
|
*port += 1;
|
||||||
port += 1;
|
while TcpListener::bind(("127.0.0.1", *port)).is_err() {
|
||||||
|
*port += 1;
|
||||||
}
|
}
|
||||||
port
|
*port
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
|
|
Loading…
Reference in New Issue