diff --git a/tests/common/mod.rs b/tests/common/mod.rs index aaa4dab..c3db66e 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -4,6 +4,7 @@ use nostr_rs_relay::server::start_server; //use http::{Request, Response}; use hyper::{Client, StatusCode, Uri}; use std::net::TcpListener; +use std::sync::atomic::{AtomicU16, Ordering}; use std::sync::mpsc as syncmpsc; use std::sync::mpsc::{Receiver as MpscReceiver, Sender as MpscSender}; use std::thread; @@ -20,7 +21,7 @@ pub struct Relay { pub fn start_relay() -> Result { // setup tracing let _trace_sub = tracing_subscriber::fmt::try_init(); - info!("Starting up from main"); + info!("Starting a new relay"); // replace default settings let mut settings = config::Settings::default(); // identify open port @@ -87,10 +88,21 @@ pub async fn wait_for_healthy_relay(relay: &Relay) -> Result<()> { } // from https://elliotekj.com/posts/2017/07/25/find-available-tcp-port-rust/ +// This needed some modification; if multiple tasks all ask for open ports, they will tend to get the same one. +// instead we should try to try these incrementally/globally. + +static PORT_COUNTER: AtomicU16 = AtomicU16::new(4030); + fn get_available_port() -> Option { - (4030..20000).find(|port| port_is_available(*port)) + let startsearch = PORT_COUNTER.fetch_add(10, Ordering::SeqCst); + if startsearch >= 20000 { + // wrap around + PORT_COUNTER.store(4030, Ordering::Relaxed); + } + (startsearch..20000).find(|port| port_is_available(*port)) } pub fn port_is_available(port: u16) -> bool { + info!("checking on port {}", port); match TcpListener::bind(("127.0.0.1", port)) { Ok(_) => true, Err(_) => false, diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 58ad3f9..c95b380 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -12,8 +12,6 @@ async fn start_and_stop() -> Result<()> { let relay = common::start_relay()?; // wait for the relay's webserver to start up and deliver a page: common::wait_for_healthy_relay(&relay).await?; - - let relay = common::start_relay()?; let port = relay.port; // just make sure we can startup and shut down. // if we send a shutdown message before the server is listening, @@ -43,5 +41,7 @@ async fn relay_home_page() -> Result<()> { // get a relay and wait for startup... let relay = common::start_relay()?; common::wait_for_healthy_relay(&relay).await?; + // tell relay to shutdown + let _res = relay.shutdown_tx.send(()); Ok(()) }