Use lib to ensure ports are free

This commit is contained in:
Philipp Hoenisch 2020-10-23 11:28:58 +11:00
parent f7bcfa5e14
commit c0109d12cd
No known key found for this signature in database
GPG Key ID: E5F8E74C672BC666
7 changed files with 23 additions and 20 deletions

View File

@ -7,6 +7,7 @@ edition = "2018"
[dependencies]
anyhow = "1"
futures = "0.3"
port_check = "0.1"
rand = "0.7"
reqwest = { version = "0.10", default-features = false, features = ["json", "native-tls"] }
serde = { version = "1.0", features = ["derive"] }

View File

@ -24,8 +24,7 @@
pub mod image;
pub mod rpc;
use anyhow::Result;
use rand::Rng;
use anyhow::{anyhow, Result};
use serde::Deserialize;
use std::time::Duration;
use testcontainers::{clients::Cli, core::Port, Container, Docker};
@ -58,12 +57,15 @@ pub struct Monero {
impl<'c> Monero {
/// Starts a new regtest monero container.
pub fn new(cli: &'c Cli) -> (Self, Container<'c, Cli, image::Monero>) {
let mut rng = rand::thread_rng();
let monerod_rpc_port: u16 = rng.gen_range(1024, u16::MAX);
let miner_wallet_rpc_port: u16 = rng.gen_range(1024, u16::MAX);
let alice_wallet_rpc_port: u16 = rng.gen_range(1024, u16::MAX);
let bob_wallet_rpc_port: u16 = rng.gen_range(1024, u16::MAX);
pub fn new(cli: &'c Cli) -> Result<(Self, Container<'c, Cli, image::Monero>)> {
let monerod_rpc_port: u16 =
port_check::free_local_port().ok_or_else(|| anyhow!("Could not retrieve free port"))?;
let miner_wallet_rpc_port: u16 =
port_check::free_local_port().ok_or_else(|| anyhow!("Could not retrieve free port"))?;
let alice_wallet_rpc_port: u16 =
port_check::free_local_port().ok_or_else(|| anyhow!("Could not retrieve free port"))?;
let bob_wallet_rpc_port: u16 =
port_check::free_local_port().ok_or_else(|| anyhow!("Could not retrieve free port"))?;
let image = image::Monero::default()
.with_mapped_port(Port {
@ -90,7 +92,7 @@ impl<'c> Monero {
let docker = cli.run(image);
println!("image ran");
(
Ok((
Self {
monerod_rpc_port,
miner_wallet_rpc_port,
@ -98,7 +100,7 @@ impl<'c> Monero {
bob_wallet_rpc_port,
},
docker,
)
))
}
pub fn miner_wallet_rpc_client(&self) -> wallet::Client {

View File

@ -8,7 +8,7 @@ const BOB_FUND_AMOUNT: u64 = 0;
#[tokio::test]
async fn init_accounts_for_alice_and_bob() {
let tc = Cli::default();
let (monero, _container) = Monero::new(&tc);
let (monero, _container) = Monero::new(&tc).unwrap();
monero
.init(ALICE_FUND_AMOUNT, BOB_FUND_AMOUNT)
.await

View File

@ -9,7 +9,7 @@ fn init_cli() -> Cli {
#[tokio::test]
async fn connect_to_monerod() {
let tc = init_cli();
let (monero, _container) = Monero::new(&tc);
let (monero, _container) = Monero::new(&tc).unwrap();
let cli = monero.monerod_rpc_client();
let header = cli

View File

@ -5,7 +5,7 @@ use testcontainers::clients::Cli;
#[tokio::test]
async fn wallet_and_accounts() {
let tc = Cli::default();
let (monero, _container) = Monero::new(&tc);
let (monero, _container) = Monero::new(&tc).unwrap();
let cli = monero.miner_wallet_rpc_client();
println!("creating wallet ...");
@ -24,7 +24,7 @@ async fn wallet_and_accounts() {
#[tokio::test]
async fn create_account_and_retrieve_it() {
let tc = Cli::default();
let (monero, _container) = Monero::new(&tc);
let (monero, _container) = Monero::new(&tc).unwrap();
let cli = monero.miner_wallet_rpc_client();
let label = "Iron Man"; // This is intentionally _not_ Alice or Bob.
@ -58,7 +58,7 @@ async fn transfer_and_check_tx_key() {
let fund_bob = 0;
let tc = Cli::default();
let (monero, _container) = Monero::new(&tc);
let (monero, _container) = Monero::new(&tc).unwrap();
let _ = monero.init(fund_alice, fund_bob).await;
let address_bob = monero

View File

@ -34,7 +34,7 @@ mod tests {
.set_default();
let cli = Cli::default();
let (monero, _container) = Monero::new(&cli);
let (monero, _container) = Monero::new(&cli).unwrap();
let bitcoind = init_bitcoind(&cli).await;
let (
@ -108,7 +108,7 @@ mod tests {
.set_default();
let cli = Cli::default();
let (monero, _container) = Monero::new(&cli);
let (monero, _container) = Monero::new(&cli).unwrap();
let bitcoind = init_bitcoind(&cli).await;
let (
@ -184,7 +184,7 @@ mod tests {
.set_default();
let cli = Cli::default();
let (monero, _container) = Monero::new(&cli);
let (monero, _container) = Monero::new(&cli).unwrap();
let bitcoind = init_bitcoind(&cli).await;
let (
@ -249,7 +249,7 @@ mod tests {
.set_default();
let cli = Cli::default();
let (monero, _container) = Monero::new(&cli);
let (monero, _container) = Monero::new(&cli).unwrap();
let bitcoind = init_bitcoind(&cli).await;
let alice_db_dir = tempfile::tempdir().unwrap();

View File

@ -164,7 +164,7 @@ async fn swap_as_bob(
#[tokio::test]
async fn on_chain_happy_path() {
let cli = Cli::default();
let (monero, _container) = Monero::new(&cli);
let (monero, _container) = Monero::new(&cli).unwrap();
let bitcoind = init_bitcoind(&cli).await;
let (alice_state0, bob_state0, mut alice_node, mut bob_node, initial_balances, swap_amounts) =