nostr-rs-relay

My dev fork of nostr-rs-relay
git clone git://jb55.com/nostr-rs-relay
Log | Files | Refs | README | LICENSE

commit a4df9445b6a94acff9a2b00168637006de4e6163
parent 92da9d71f8f0745b26c4dab654c6d5eabe6eb865
Author: Greg Heartsfield <scsibug@imap.cc>
Date:   Wed, 28 Sep 2022 07:55:06 -0500

test: improve port selection

Diffstat:
Mtests/common/mod.rs | 16++++++++++++++--
Mtests/integration_test.rs | 4++--
2 files changed, 16 insertions(+), 4 deletions(-)

diff --git 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<Relay> { // 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<u16> { - (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 @@ -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(()) }