diff --git a/src/event.rs b/src/event.rs index c8ab989c7..82b6b5c4d 100644 --- a/src/event.rs +++ b/src/event.rs @@ -554,7 +554,7 @@ where #[cfg(test)] mod tests { use super::*; - use crate::tests::test_utils::TestPersister; + use crate::test::utils::TestPersister; #[test] fn event_queue_persistence() { diff --git a/src/lib.rs b/src/lib.rs index b6c8cb754..96758120a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -74,7 +74,7 @@ mod logger; mod payment_store; mod peer_store; #[cfg(test)] -mod tests; +mod test; mod types; mod wallet; diff --git a/src/payment_store.rs b/src/payment_store.rs index e46aa991b..c5b3d14f3 100644 --- a/src/payment_store.rs +++ b/src/payment_store.rs @@ -205,7 +205,7 @@ where #[cfg(test)] mod tests { use super::*; - use crate::tests::test_utils::TestPersister; + use crate::test::utils::TestPersister; use std::sync::Arc; #[test] diff --git a/src/peer_store.rs b/src/peer_store.rs index 6147dee81..ec2abfa0a 100644 --- a/src/peer_store.rs +++ b/src/peer_store.rs @@ -173,7 +173,7 @@ impl TryFrom for PeerInfo { #[cfg(test)] mod tests { use super::*; - use crate::tests::test_utils::TestPersister; + use crate::test::utils::TestPersister; use proptest::prelude::*; use std::str::FromStr; diff --git a/src/tests/functional_tests.rs b/src/test/functional_tests.rs similarity index 92% rename from src/tests/functional_tests.rs rename to src/test/functional_tests.rs index 588a4b8a7..1fe2e43fc 100644 --- a/src/tests/functional_tests.rs +++ b/src/test/functional_tests.rs @@ -1,5 +1,5 @@ -use crate::tests::test_utils::expect_event; -use crate::{Builder, Config, Error, Event, PaymentDirection, PaymentStatus}; +use crate::test::utils::{expect_event, random_config}; +use crate::{Builder, Error, Event, PaymentDirection, PaymentStatus}; use bitcoin::{Address, Amount, OutPoint, Txid}; use bitcoind::bitcoincore_rpc::RpcApi; @@ -8,8 +8,6 @@ use electrsd::{bitcoind, bitcoind::BitcoinD, ElectrsD}; use electrum_client::ElectrumApi; use once_cell::sync::OnceCell; -use rand::distributions::Alphanumeric; -use rand::{thread_rng, Rng}; use std::env; use std::sync::Mutex; @@ -137,38 +135,17 @@ fn premine_and_distribute_funds(addrs: Vec
, amount: Amount) { generate_blocks_and_wait(1); } -fn rand_config() -> Config { - let mut config = Config::default(); - - let esplora_url = get_electrsd().esplora_url.as_ref().unwrap(); - - println!("Setting esplora server URL: {}", esplora_url); - config.esplora_server_url = format!("http://{}", esplora_url); - - let mut rng = thread_rng(); - let rand_dir: String = (0..7).map(|_| rng.sample(Alphanumeric) as char).collect(); - let rand_path = format!("/tmp/{}", rand_dir); - println!("Setting random LDK storage dir: {}", rand_dir); - config.storage_dir_path = rand_path; - - let rand_port: u16 = rng.gen_range(5000..8000); - println!("Setting random LDK listening port: {}", rand_port); - let listening_address = format!("127.0.0.1:{}", rand_port); - config.listening_address = Some(listening_address); - - config -} - #[test] fn channel_full_cycle() { println!("== Node A =="); - let config_a = rand_config(); + let esplora_url = get_electrsd().esplora_url.as_ref().unwrap(); + let config_a = random_config(esplora_url); let node_a = Builder::from_config(config_a).build(); node_a.start().unwrap(); let addr_a = node_a.new_funding_address().unwrap(); println!("\n== Node B =="); - let config_b = rand_config(); + let config_b = random_config(esplora_url); let node_b = Builder::from_config(config_b).build(); node_b.start().unwrap(); let addr_b = node_b.new_funding_address().unwrap(); @@ -317,13 +294,14 @@ fn channel_full_cycle() { #[test] fn channel_open_fails_when_funds_insufficient() { println!("== Node A =="); - let config_a = rand_config(); + let esplora_url = get_electrsd().esplora_url.as_ref().unwrap(); + let config_a = random_config(&esplora_url); let node_a = Builder::from_config(config_a).build(); node_a.start().unwrap(); let addr_a = node_a.new_funding_address().unwrap(); println!("\n== Node B =="); - let config_b = rand_config(); + let config_b = random_config(&esplora_url); let node_b = Builder::from_config(config_b).build(); node_b.start().unwrap(); let addr_b = node_b.new_funding_address().unwrap(); @@ -344,7 +322,8 @@ fn channel_open_fails_when_funds_insufficient() { #[test] fn connect_to_public_testnet_esplora() { - let mut config = rand_config(); + let esplora_url = get_electrsd().esplora_url.as_ref().unwrap(); + let mut config = random_config(&esplora_url); config.esplora_server_url = "https://blockstream.info/testnet/api".to_string(); config.network = bitcoin::Network::Testnet; let node = Builder::from_config(config).build(); @@ -355,7 +334,8 @@ fn connect_to_public_testnet_esplora() { #[test] fn start_stop_reinit() { - let config = rand_config(); + let esplora_url = get_electrsd().esplora_url.as_ref().unwrap(); + let config = random_config(&esplora_url); let node = Builder::from_config(config.clone()).build(); let expected_node_id = node.node_id(); diff --git a/src/tests/mod.rs b/src/test/mod.rs similarity index 56% rename from src/tests/mod.rs rename to src/test/mod.rs index 5c32fa2af..f856f4878 100644 --- a/src/tests/mod.rs +++ b/src/test/mod.rs @@ -1,2 +1,2 @@ pub mod functional_tests; -pub mod test_utils; +pub mod utils; diff --git a/src/tests/test_utils.rs b/src/test/utils.rs similarity index 66% rename from src/tests/test_utils.rs rename to src/test/utils.rs index 1b9740183..aacc3c4fb 100644 --- a/src/tests/test_utils.rs +++ b/src/test/utils.rs @@ -1,7 +1,10 @@ use crate::io::KVStoreUnpersister; +use crate::Config; use lightning::util::persist::KVStorePersister; use lightning::util::ser::Writeable; +use rand::distributions::Alphanumeric; +use rand::{thread_rng, Rng}; use std::collections::HashMap; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Mutex; @@ -62,3 +65,32 @@ impl KVStoreUnpersister for TestPersister { Ok(persisted_bytes_lock.remove(key).is_some()) } } + +pub fn random_storage_path() -> String { + let mut rng = thread_rng(); + let rand_dir: String = (0..7).map(|_| rng.sample(Alphanumeric) as char).collect(); + format!("/tmp/{}", rand_dir) +} + +pub fn random_port() -> u16 { + let mut rng = thread_rng(); + rng.gen_range(5000..65535) +} + +pub fn random_config(esplora_url: &str) -> Config { + let mut config = Config::default(); + + println!("Setting esplora server URL: {}", esplora_url); + config.esplora_server_url = format!("http://{}", esplora_url); + + let rand_dir = random_storage_path(); + println!("Setting random LDK storage dir: {}", rand_dir); + config.storage_dir_path = rand_dir; + + let rand_port = random_port(); + println!("Setting random LDK listening port: {}", rand_port); + let listening_address = format!("127.0.0.1:{}", rand_port); + config.listening_address = Some(listening_address); + + config +}