From f090fe3dae4b09ccc5cb84a0fb084966f414e94f Mon Sep 17 00:00:00 2001 From: spacebear Date: Mon, 2 Dec 2024 13:39:53 -0500 Subject: [PATCH] fix e2e test --- payjoin-cli/tests/e2e.rs | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/payjoin-cli/tests/e2e.rs b/payjoin-cli/tests/e2e.rs index eb4bf11f..2954475d 100644 --- a/payjoin-cli/tests/e2e.rs +++ b/payjoin-cli/tests/e2e.rs @@ -11,6 +11,8 @@ mod e2e { use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader}; use tokio::process::Command; + type BoxSendSyncError = Box; + const RECEIVE_SATS: &str = "54321"; #[cfg(not(feature = "v2"))] @@ -180,8 +182,20 @@ mod e2e { let (cert, key) = local_cert_key(); let ohttp_relay_port = find_free_port(); let ohttp_relay = Url::parse(&format!("http://localhost:{}", ohttp_relay_port)).unwrap(); - let directory_port = find_free_port(); - let directory = Url::parse(&format!("https://localhost:{}", directory_port)).unwrap(); + let docker: Cli = Cli::default(); + let db = docker.run(Redis); + let db_host = format!("127.0.0.1:{}", db.get_host_port_ipv4(6379)); + let (port, directory_future) = + init_directory(db_host, (cert.clone(), key)).await.expect("Failed to init directory"); + println!("Directory server started on port IN TEST FN {}", port); + let directory = Url::parse(&format!("https://localhost:{}", port)).unwrap(); + + // Spawn the directory server task + let directory_task = tokio::spawn(async move { + if let Err(e) = directory_future.await { + eprintln!("Directory server error: {:?}", e); + } + }); let gateway_origin = http::Uri::from_str(directory.as_str()).unwrap(); let temp_dir = env::temp_dir(); @@ -189,7 +203,7 @@ mod e2e { let sender_db_path = temp_dir.join("sender_db"); let result: Result<()> = tokio::select! { res = ohttp_relay::listen_tcp(ohttp_relay_port, gateway_origin) => Err(format!("Ohttp relay is long running: {:?}", res).into()), - res = init_directory(directory_port, (cert.clone(), key)) => Err(format!("Directory server is long running: {:?}", res).into()), + res = directory_task => Err(format!("Directory server is long running: {:?}", res).into()), res = send_receive_cli_async(ohttp_relay, directory, cert, receiver_db_path.clone(), sender_db_path.clone()) => res.map_err(|e| format!("send_receive failed: {:?}", e).into()), }; @@ -476,13 +490,17 @@ mod e2e { Err("Timeout waiting for service to be ready".into()) } - async fn init_directory(port: u16, local_cert_key: (Vec, Vec)) -> Result<()> { - let docker: Cli = Cli::default(); + async fn init_directory<'a>( + db_host: String, + local_cert_key: (Vec, Vec), + ) -> std::result::Result< + (u16, tokio::task::JoinHandle>), + BoxSendSyncError, + > { + println!("Database running on {}", db_host); let timeout = Duration::from_secs(2); - let db = docker.run(Redis); - let db_host = format!("127.0.0.1:{}", db.get_host_port_ipv4(6379)); - println!("Database running on {}", db.get_host_port_ipv4(6379)); - payjoin_directory::listen_tcp_with_tls(port, db_host, timeout, local_cert_key).await + payjoin_directory::listen_tcp_with_tls_on_free_port(db_host, timeout, local_cert_key) + .await } // generates or gets a DER encoded localhost cert and key.