From 9a7cd2cb35d586abbd28b0dae0ebdf43b6db8d8e Mon Sep 17 00:00:00 2001 From: hatoo Date: Tue, 5 Sep 2023 12:14:20 +0900 Subject: [PATCH] Fix test --- Cargo.lock | 7 ------- Cargo.toml | 1 - tests/tests.rs | 20 +++++--------------- 3 files changed, 5 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 28c168d2..a9b8f181 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -548,12 +548,6 @@ dependencies = [ "slab", ] -[[package]] -name = "get-port" -version = "4.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "888123007db34fbff15b5a347d46364dfbad531d6cb43de52cc0b62558f570e2" - [[package]] name = "getrandom" version = "0.2.10" @@ -956,7 +950,6 @@ dependencies = [ "float-ord", "flume", "futures", - "get-port", "http", "humantime", "hyper", diff --git a/Cargo.toml b/Cargo.toml index 7965d724..a0187c5a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -71,6 +71,5 @@ jemallocator = "0.5.0" assert_cmd = "2.0.2" axum = "0.6.20" bytes = "1.0" -get-port = "4.0.0" http = "0.2" lazy_static = "1.4.0" diff --git a/tests/tests.rs b/tests/tests.rs index 2abbfdca..3b928517 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1,7 +1,7 @@ use std::{ convert::Infallible, net::{Ipv6Addr, SocketAddr}, - sync::{Mutex, OnceLock}, + sync::atomic::AtomicU16, }; use assert_cmd::Command; @@ -11,7 +11,6 @@ use axum::{ routing::{any, get}, Router, }; -use get_port::Ops; use http::{HeaderMap, Response}; use hyper::{ body::to_bytes, @@ -20,25 +19,18 @@ use hyper::{ Body, }; -static PORT_LOCK: OnceLock> = OnceLock::new(); - -// get_port only check ipv4 port and it seems that port for ipv6 is independent to ipv4 -// so we chose to serialize runs for ipv6 -static IPV6_LOCK: OnceLock> = OnceLock::new(); +// Port 5111- is reserved for testing +static PORT: AtomicU16 = AtomicU16::new(5111); fn bind_port() -> (hyper::server::Builder, u16) { - let _guard = PORT_LOCK.get_or_init(|| Mutex::new(())).lock().unwrap(); - - let port = get_port::tcp::TcpPort::any("127.0.0.1").unwrap(); + let port = PORT.fetch_add(1, std::sync::atomic::Ordering::Relaxed); let addr = SocketAddr::new("127.0.0.1".parse().unwrap(), port); (axum::Server::bind(&addr), port) } fn bind_port_ipv6() -> (hyper::server::Builder, u16) { - let _guard = PORT_LOCK.get_or_init(|| Mutex::new(())).lock().unwrap(); - - let port = get_port::tcp::TcpPort::any("127.0.0.1").unwrap(); + let port = PORT.fetch_add(1, std::sync::atomic::Ordering::Relaxed); let addr = SocketAddr::new(std::net::IpAddr::V6(Ipv6Addr::LOCALHOST), port); (axum::Server::bind(&addr), port) @@ -268,7 +260,6 @@ async fn get_host_with_connect_to_ipv6_target(host: &'static str) -> String { }), ); - let _guard = IPV6_LOCK.get_or_init(|| Mutex::new(())).lock().unwrap(); let (server, port) = bind_port_ipv6(); tokio::spawn(async move { server.serve(app.into_make_service()).await.unwrap(); @@ -571,7 +562,6 @@ async fn test_ipv6() { }), ); - let _guard = IPV6_LOCK.get_or_init(|| Mutex::new(())).lock().unwrap(); let (service, port) = bind_port_ipv6(); tokio::spawn(async move { service.serve(app.into_make_service()).await.unwrap();