Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up e2e test temp dirs on panic #565

Merged
merged 1 commit into from
Mar 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions payjoin-cli/tests/e2e.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#[cfg(feature = "_danger-local-https")]
mod e2e {
use std::env;
use std::path::PathBuf;
use std::process::{ExitStatus, Stdio};

use nix::sys::signal::{kill, Signal};
use nix::unistd::Pid;
use payjoin_test_utils::{init_bitcoind_sender_receiver, BoxError};
use tokio::fs;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::process::Command;

Expand All @@ -17,6 +17,18 @@ mod e2e {
child.wait().await
}

struct CleanupGuard {
paths: Vec<PathBuf>,
}

impl Drop for CleanupGuard {
fn drop(&mut self) {
for path in &self.paths {
cleanup_temp_file(path);
}
}
}

const RECEIVE_SATS: &str = "54321";

#[cfg(feature = "v1")]
Expand All @@ -26,6 +38,8 @@ mod e2e {
let temp_dir = env::temp_dir();
let receiver_db_path = temp_dir.join("receiver_db");
let sender_db_path = temp_dir.join("sender_db");
let _cleanup_guard =
CleanupGuard { paths: vec![receiver_db_path.clone(), sender_db_path.clone()] };
let receiver_db_path_clone = receiver_db_path.clone();
let sender_db_path_clone = sender_db_path.clone();
let port = find_free_port()?;
Expand Down Expand Up @@ -130,8 +144,6 @@ mod e2e {
})
.await?;

cleanup_temp_file(&receiver_db_path).await;
cleanup_temp_file(&sender_db_path).await;
assert!(payjoin_sent, "Payjoin send was not detected");

fn find_free_port() -> Result<u16, BoxError> {
Expand All @@ -157,14 +169,15 @@ mod e2e {
let temp_dir = env::temp_dir();
let receiver_db_path = temp_dir.join("receiver_db");
let sender_db_path = temp_dir.join("sender_db");
let _cleanup_guard =
CleanupGuard { paths: vec![receiver_db_path.clone(), sender_db_path.clone()] };

let result = tokio::select! {
res = services.take_ohttp_relay_handle() => Err(format!("Ohttp relay is long running: {:?}", res).into()),
res = services.take_directory_handle() => Err(format!("Directory server is long running: {:?}", res).into()),
res = send_receive_cli_async(&services, receiver_db_path.clone(), sender_db_path.clone()) => res,
};

cleanup_temp_file(&receiver_db_path).await;
cleanup_temp_file(&sender_db_path).await;
assert!(result.is_ok(), "send_receive failed: {:#?}", result.unwrap_err());

async fn send_receive_cli_async(
Expand Down Expand Up @@ -361,8 +374,8 @@ mod e2e {
Ok(())
}

async fn cleanup_temp_file(path: &std::path::Path) {
if let Err(e) = fs::remove_dir_all(path).await {
fn cleanup_temp_file(path: &std::path::Path) {
if let Err(e) = std::fs::remove_dir_all(path) {
eprintln!("Failed to remove {:?}: {}", path, e);
}
}
Expand Down