From 638143a10da534e7ae3a00ed6ae87617ff7eb1f5 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Fri, 18 Dec 2020 11:54:13 +1100 Subject: [PATCH] Replace hard coded port by dynamic available one Fixes #84. --- Cargo.lock | 7 +++++++ swap/Cargo.toml | 1 + swap/tests/happy_path.rs | 5 +++-- swap/tests/happy_path_restart_alice.rs | 11 ++++++----- swap/tests/happy_path_restart_bob.rs | 14 +++++++------- swap/tests/punish.rs | 5 +++-- swap/tests/refund_restart_alice.rs | 5 +++-- 7 files changed, 30 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b015f2d9..c9de62a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1151,6 +1151,12 @@ dependencies = [ "version_check", ] +[[package]] +name = "get-port" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac6c41a39c60ae1fc5bf0e220347ce90fa1e4bb0fcdac65b09bb5f4576bebc84" + [[package]] name = "get_if_addrs" version = "0.5.2" @@ -3312,6 +3318,7 @@ dependencies = [ "ecdsa_fun", "futures", "genawaiter", + "get-port", "hyper", "libp2p", "libp2p-tokio-socks5", diff --git a/swap/Cargo.toml b/swap/Cargo.toml index e3abd5cb..f30ae7e4 100644 --- a/swap/Cargo.toml +++ b/swap/Cargo.toml @@ -48,6 +48,7 @@ void = "1" xmr-btc = { path = "../xmr-btc" } [dev-dependencies] +get-port = "3" hyper = "0.13" port_check = "0.1" spectral = "0.6" diff --git a/swap/tests/happy_path.rs b/swap/tests/happy_path.rs index 5138c0a7..daabc75e 100644 --- a/swap/tests/happy_path.rs +++ b/swap/tests/happy_path.rs @@ -1,5 +1,6 @@ use crate::testutils::{init_alice, init_bob}; use futures::future::try_join; +use get_port::get_port; use libp2p::Multiaddr; use rand::rngs::OsRng; use swap::{alice, bob}; @@ -35,8 +36,8 @@ async fn happy_path() { let xmr_alice = xmr_to_swap * 10; let xmr_bob = xmr_btc::monero::Amount::ZERO; - // todo: This should not be hardcoded - let alice_multiaddr: Multiaddr = "/ip4/127.0.0.1/tcp/9876" + let port = get_port().expect("Failed to find a free port"); + let alice_multiaddr: Multiaddr = format!("/ip4/127.0.0.1/tcp/{}", port) .parse() .expect("failed to parse Alice's address"); diff --git a/swap/tests/happy_path_restart_alice.rs b/swap/tests/happy_path_restart_alice.rs index 3053bec1..75bf8a1a 100644 --- a/swap/tests/happy_path_restart_alice.rs +++ b/swap/tests/happy_path_restart_alice.rs @@ -1,17 +1,17 @@ +use crate::testutils::{init_alice, init_bob}; +use get_port::get_port; use libp2p::Multiaddr; use rand::rngs::OsRng; +use std::convert::TryFrom; use swap::{alice, alice::swap::AliceState, bitcoin, bob, storage::Database}; use tempfile::tempdir; use testcontainers::clients::Cli; +use testutils::init_tracing; use uuid::Uuid; use xmr_btc::config::Config; pub mod testutils; -use crate::testutils::{init_alice, init_bob}; -use std::convert::TryFrom; -use testutils::init_tracing; - #[tokio::test] async fn given_alice_restarts_after_encsig_is_learned_resume_swap() { let _guard = init_tracing(); @@ -31,7 +31,8 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() { let bob_btc_starting_balance = btc_to_swap * 10; let alice_xmr_starting_balance = xmr_to_swap * 10; - let alice_multiaddr: Multiaddr = "/ip4/127.0.0.1/tcp/9877" + let port = get_port().expect("Failed to find a free port"); + let alice_multiaddr: Multiaddr = format!("/ip4/127.0.0.1/tcp/{}", port) .parse() .expect("failed to parse Alice's address"); diff --git a/swap/tests/happy_path_restart_bob.rs b/swap/tests/happy_path_restart_bob.rs index e22e234f..98d19aa2 100644 --- a/swap/tests/happy_path_restart_bob.rs +++ b/swap/tests/happy_path_restart_bob.rs @@ -1,18 +1,17 @@ +use crate::testutils::{init_alice, init_bob}; +use get_port::get_port; use libp2p::Multiaddr; use rand::rngs::OsRng; -use swap::{alice, bitcoin, bob, storage::Database}; +use std::convert::TryFrom; +use swap::{alice, bitcoin, bob, bob::swap::BobState, storage::Database}; use tempfile::tempdir; use testcontainers::clients::Cli; +use testutils::init_tracing; use uuid::Uuid; use xmr_btc::config::Config; pub mod testutils; -use crate::testutils::{init_alice, init_bob}; -use std::convert::TryFrom; -use swap::bob::swap::BobState; -use testutils::init_tracing; - #[tokio::test] async fn given_bob_restarts_after_encsig_is_sent_resume_swap() { let _guard = init_tracing(); @@ -32,7 +31,8 @@ async fn given_bob_restarts_after_encsig_is_sent_resume_swap() { let bob_btc_starting_balance = btc_to_swap * 10; let alice_xmr_starting_balance = xmr_to_swap * 10; - let alice_multiaddr: Multiaddr = "/ip4/127.0.0.1/tcp/9877" + let port = get_port().expect("Failed to find a free port"); + let alice_multiaddr: Multiaddr = format!("/ip4/127.0.0.1/tcp/{}", port) .parse() .expect("failed to parse Alice's address"); diff --git a/swap/tests/punish.rs b/swap/tests/punish.rs index a7ddb1d8..717983ef 100644 --- a/swap/tests/punish.rs +++ b/swap/tests/punish.rs @@ -1,5 +1,6 @@ use crate::testutils::{init_alice, init_bob}; use futures::future::try_join; +use get_port::get_port; use libp2p::Multiaddr; use rand::rngs::OsRng; use swap::{alice, alice::swap::AliceState, bob, bob::swap::BobState}; @@ -33,8 +34,8 @@ async fn alice_punishes_if_bob_never_acts_after_fund() { let alice_btc_starting_balance = bitcoin::Amount::ZERO; let alice_xmr_starting_balance = xmr_to_swap * 10; - // todo: This should not be hardcoded - let alice_multiaddr: Multiaddr = "/ip4/127.0.0.1/tcp/9877" + let port = get_port().expect("Failed to find a free port"); + let alice_multiaddr: Multiaddr = format!("/ip4/127.0.0.1/tcp/{}", port) .parse() .expect("failed to parse Alice's address"); diff --git a/swap/tests/refund_restart_alice.rs b/swap/tests/refund_restart_alice.rs index bbcf6d27..a397c37b 100644 --- a/swap/tests/refund_restart_alice.rs +++ b/swap/tests/refund_restart_alice.rs @@ -1,5 +1,6 @@ use crate::testutils::{init_alice, init_bob}; use futures::future::try_join; +use get_port::get_port; use libp2p::Multiaddr; use rand::rngs::OsRng; use swap::{alice, alice::swap::AliceState, bob, bob::swap::BobState, storage::Database}; @@ -35,8 +36,8 @@ async fn given_alice_restarts_after_xmr_is_locked_abort_swap() { let alice_btc_starting_balance = bitcoin::Amount::ZERO; let alice_xmr_starting_balance = xmr_to_swap * 10; - // todo: This should not be hardcoded - let alice_multiaddr: Multiaddr = "/ip4/127.0.0.1/tcp/9879" + let port = get_port().expect("Failed to find a free port"); + let alice_multiaddr: Multiaddr = format!("/ip4/127.0.0.1/tcp/{}", port) .parse() .expect("failed to parse Alice's address");