Skip to content

Commit c4bf737

Browse files
committed
mutex
1 parent 5958b3a commit c4bf737

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

payjoin/tests/integration.rs

+21-11
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ mod integration {
174174
#[cfg(feature = "danger-local-https")]
175175
#[cfg(feature = "v2")]
176176
mod v2 {
177+
use std::collections::HashSet;
177178
use std::sync::Arc;
178179
use std::time::Duration;
179180

@@ -184,11 +185,13 @@ mod integration {
184185
use reqwest::{Client, ClientBuilder, Error, Response};
185186
use testcontainers_modules::redis::Redis;
186187
use testcontainers_modules::testcontainers::clients::Cli;
188+
use tokio::sync::Mutex;
187189

188190
use super::*;
189191

190192
static TESTS_TIMEOUT: Lazy<Duration> = Lazy::new(|| Duration::from_secs(20));
191193
static WAIT_SERVICE_INTERVAL: Lazy<Duration> = Lazy::new(|| Duration::from_secs(3));
194+
static RESERVED_PORTS: Lazy<Mutex<HashSet<u16>>> = Lazy::new(|| Mutex::new(HashSet::new()));
192195

193196
#[tokio::test]
194197
async fn test_bad_ohttp_keys() {
@@ -197,7 +200,7 @@ mod integration {
197200
.expect("Invalid OhttpKeys");
198201

199202
let (cert, key) = local_cert_key();
200-
let port = find_free_port();
203+
let port = reserve_port().await;
201204
let directory = Url::parse(&format!("https://localhost:{}", port)).unwrap();
202205
tokio::select!(
203206
err = init_directory(port, (cert.clone(), key)) => panic!("Directory server exited early: {:?}", err),
@@ -231,10 +234,10 @@ mod integration {
231234
async fn test_session_expiration() {
232235
init_tracing();
233236
let (cert, key) = local_cert_key();
234-
let ohttp_relay_port = find_free_port();
237+
let ohttp_relay_port = reserve_port().await;
235238
let ohttp_relay =
236239
Url::parse(&format!("http://localhost:{}", ohttp_relay_port)).unwrap();
237-
let directory_port = find_free_port();
240+
let directory_port = reserve_port().await;
238241
let directory = Url::parse(&format!("https://localhost:{}", directory_port)).unwrap();
239242
let gateway_origin = http::Uri::from_str(directory.as_str()).unwrap();
240243
tokio::select!(
@@ -299,10 +302,10 @@ mod integration {
299302
async fn v2_to_v2() {
300303
init_tracing();
301304
let (cert, key) = local_cert_key();
302-
let ohttp_relay_port = find_free_port();
305+
let ohttp_relay_port = reserve_port().await;
303306
let ohttp_relay =
304307
Url::parse(&format!("http://localhost:{}", ohttp_relay_port)).unwrap();
305-
let directory_port = find_free_port();
308+
let directory_port = reserve_port().await;
306309
let directory = Url::parse(&format!("https://localhost:{}", directory_port)).unwrap();
307310
let gateway_origin = http::Uri::from_str(directory.as_str()).unwrap();
308311
tokio::select!(
@@ -431,10 +434,10 @@ mod integration {
431434
async fn v2_to_v2_mixed_input_script_types() {
432435
init_tracing();
433436
let (cert, key) = local_cert_key();
434-
let ohttp_relay_port = find_free_port();
437+
let ohttp_relay_port = reserve_port().await;
435438
let ohttp_relay =
436439
Url::parse(&format!("http://localhost:{}", ohttp_relay_port)).unwrap();
437-
let directory_port = find_free_port();
440+
let directory_port = reserve_port().await;
438441
let directory = Url::parse(&format!("https://localhost:{}", directory_port)).unwrap();
439442
let gateway_origin = http::Uri::from_str(directory.as_str()).unwrap();
440443
tokio::select!(
@@ -648,10 +651,10 @@ mod integration {
648651
async fn v1_to_v2() {
649652
init_tracing();
650653
let (cert, key) = local_cert_key();
651-
let ohttp_relay_port = find_free_port();
654+
let ohttp_relay_port = reserve_port().await;
652655
let ohttp_relay =
653656
Url::parse(&format!("http://localhost:{}", ohttp_relay_port)).unwrap();
654-
let directory_port = find_free_port();
657+
let directory_port = reserve_port().await;
655658
let directory = Url::parse(&format!("https://localhost:{}", directory_port)).unwrap();
656659
let gateway_origin = http::Uri::from_str(directory.as_str()).unwrap();
657660
tokio::select!(
@@ -912,9 +915,16 @@ mod integration {
912915
))
913916
}
914917

915-
fn find_free_port() -> u16 {
918+
async fn reserve_port() -> u16 {
919+
let mut reserved_ports = RESERVED_PORTS.lock().await;
916920
let listener = std::net::TcpListener::bind("0.0.0.0:0").unwrap();
917-
listener.local_addr().unwrap().port()
921+
let port = listener.local_addr().unwrap().port();
922+
if reserved_ports.insert(port) {
923+
println!("{:?}", &reserved_ports);
924+
port
925+
} else {
926+
panic!("PORT RESERVED!!!");
927+
}
918928
}
919929

920930
async fn wait_for_service_ready(

0 commit comments

Comments
 (0)