Skip to content

Commit

Permalink
Reorganize test folder for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Mar 23, 2023
1 parent ee9e4a8 commit fd396e3
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ mod logger;
mod payment_store;
mod peer_store;
#[cfg(test)]
mod tests;
mod test;
mod types;
mod wallet;

Expand Down
2 changes: 1 addition & 1 deletion src/payment_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion src/peer_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl TryFrom<String> 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;

Expand Down
44 changes: 12 additions & 32 deletions src/tests/functional_tests.rs → src/test/functional_tests.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -137,38 +135,17 @@ fn premine_and_distribute_funds(addrs: Vec<Address>, 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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion src/tests/mod.rs → src/test/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod functional_tests;
pub mod test_utils;
pub mod utils;
32 changes: 32 additions & 0 deletions src/tests/test_utils.rs → src/test/utils.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
}

0 comments on commit fd396e3

Please sign in to comment.