Skip to content

Commit

Permalink
fixup! Have directory give US a port
Browse files Browse the repository at this point in the history
ensure that the db: testcontainer::Container variable does not go out of scope while the directory is running.

previously the directory task itself was awaited on by init_directory, whereas in the modified code it is instead returned as part of the result due to the different return value of listen_tcp_with_tls_on_free_port. this indirection de-coupled the db variable's lifetime from that of the directory, allowing it to go out of scope earlier than expected.
  • Loading branch information
nothingmuch authored and spacebear21 committed Dec 2, 2024
1 parent 4baeaa2 commit f489ac9
Showing 1 changed file with 38 additions and 15 deletions.
53 changes: 38 additions & 15 deletions payjoin/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ mod integration {
use payjoin::receive::v2::{PayjoinProposal, Receiver, UncheckedProposal};
use payjoin::{HpkeKeyPair, OhttpKeys, PjUri, UriExt};
use reqwest::{Client, ClientBuilder, Error, Response};
use testcontainers::Container;
use testcontainers_modules::redis::Redis;
use testcontainers_modules::testcontainers::clients::Cli;

Expand All @@ -200,8 +201,13 @@ mod integration {

let (cert, key) = local_cert_key();
dbg!("G");
let (port, directory_future) =
init_directory((cert.clone(), key)).await.expect("Failed to init directory");
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();

Expand Down Expand Up @@ -249,8 +255,13 @@ mod integration {
let ohttp_relay_port = find_free_port();
let ohttp_relay =
Url::parse(&format!("http://localhost:{}", ohttp_relay_port)).unwrap();
let (directory_port, directory_handle) =
init_directory((cert.clone(), key)).await.expect("Failed to init directory");
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 (directory_port, directory_handle) = init_directory(db_host, (cert.clone(), key))
.await
.expect("Failed to init directory");
let directory = Url::parse(&format!("https://localhost:{}", directory_port)).unwrap();
let gateway_origin = http::Uri::from_str(directory.as_str()).unwrap();
tokio::select!(
Expand Down Expand Up @@ -319,8 +330,13 @@ mod integration {
let ohttp_relay_port = find_free_port();
let ohttp_relay =
Url::parse(&format!("http://localhost:{}", ohttp_relay_port)).unwrap();
let (directory_port, directory_future) =
init_directory((cert.clone(), key)).await.expect("Failed to init directory");
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 (directory_port, directory_future) = init_directory(db_host, (cert.clone(), key))
.await
.expect("Failed to init directory");
let directory = Url::parse(&format!("https://localhost:{}", directory_port)).unwrap();
let gateway_origin = http::Uri::from_str(directory.as_str()).unwrap();
tokio::select!(
Expand Down Expand Up @@ -449,8 +465,13 @@ mod integration {
let ohttp_relay_port = find_free_port();
let ohttp_relay =
Url::parse(&format!("http://localhost:{}", ohttp_relay_port)).unwrap();
let (directory_port, directory_future) =
init_directory((cert.clone(), key)).await.expect("Failed to init directory");
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 (directory_port, directory_future) = init_directory(db_host, (cert.clone(), key))
.await
.expect("Failed to init directory");
let directory = Url::parse(&format!("https://localhost:{}", directory_port)).unwrap();
let gateway_origin = http::Uri::from_str(directory.as_str()).unwrap();
tokio::select!(
Expand Down Expand Up @@ -662,8 +683,12 @@ mod integration {
let ohttp_relay_port = find_free_port();
let ohttp_relay =
Url::parse(&format!("http://localhost:{}", ohttp_relay_port)).unwrap();
let (directory_port, directory_future) =
init_directory((cert.clone(), key)).await.expect("Failed to init directory");
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 (directory_port, directory_future) = init_directory(db_host, (cert.clone(), key))
.await
.expect("Failed to init directory");
let directory = Url::parse(&format!("https://localhost:{}", directory_port)).unwrap();
let gateway_origin = http::Uri::from_str(directory.as_str()).unwrap();
tokio::select!(
Expand Down Expand Up @@ -789,15 +814,13 @@ mod integration {
}
}

async fn init_directory(
async fn init_directory<'a>(
db_host: String,
local_cert_key: (Vec<u8>, Vec<u8>),
) -> Result<(u16, tokio::task::JoinHandle<Result<(), BoxSendSyncError>>), BoxSendSyncError>
{
let docker: Cli = Cli::default();
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_on_free_port(db_host, timeout, local_cert_key)
.await
}
Expand Down

0 comments on commit f489ac9

Please sign in to comment.