Skip to content

Commit

Permalink
Don't unwrap in e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
spacebear21 committed Jan 28, 2025
1 parent f1a5c36 commit 5ca026e
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions payjoin-cli/tests/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod e2e {

use nix::sys::signal::{kill, Signal};
use nix::unistd::Pid;
use payjoin_test_utils::init_bitcoind_sender_receiver;
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,21 +17,21 @@ mod e2e {

const RECEIVE_SATS: &str = "54321";

#[cfg(not(feature = "v2"))]
//#[cfg(not(feature = "v2"))]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn send_receive_payjoin() {
let (bitcoind, _sender, _receiver) = init_bitcoind_sender_receiver(None, None).unwrap();
async fn send_receive_payjoin() -> Result<(), BoxError> {
let (bitcoind, _sender, _receiver) = init_bitcoind_sender_receiver(None, None)?;
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 receiver_db_path_clone = receiver_db_path.clone();
let sender_db_path_clone = sender_db_path.clone();
let port = find_free_port()?;

let payjoin_sent = tokio::spawn(async move {
let receiver_rpchost = format!("http://{}/wallet/receiver", bitcoind.params.rpc_socket);
let sender_rpchost = format!("http://{}/wallet/sender", bitcoind.params.rpc_socket);
let cookie_file = &bitcoind.params.cookie_file;
let port = find_free_port();
let pj_endpoint = format!("https://localhost:{}", port);
let payjoin_cli = env!("CARGO_BIN_EXE_payjoin-cli");

Expand Down Expand Up @@ -118,29 +118,28 @@ mod e2e {

sigint(&cli_receiver).expect("Failed to kill payjoin-cli");
sigint(&cli_sender).expect("Failed to kill payjoin-cli");
payjoin_sent
payjoin_sent.unwrap_or(Some(false)).expect("rx channel closed prematurely")
})
.await;
.await?;

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

fn find_free_port() -> u16 {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
listener.local_addr().unwrap().port()
assert!(payjoin_sent, "Payjoin send was not detected");

fn find_free_port() -> Result<u16, BoxError> {
let listener = std::net::TcpListener::bind("127.0.0.1:0")?;
Ok(listener.local_addr()?.port())
}

Ok(())
}

#[cfg(feature = "v2")]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn send_receive_payjoin() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
async fn send_receive_paydjoin() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
use std::path::PathBuf;

use payjoin_test_utils::{init_tracing, BoxError, TestServices};
use payjoin_test_utils::{init_tracing, TestServices};
use tokio::process::Child;

type Result<T> = std::result::Result<T, BoxError>;
Expand Down

0 comments on commit 5ca026e

Please sign in to comment.