Skip to content

Commit e97c480

Browse files
committed
Reorganize test folder for consistency
1 parent c1b9ef8 commit e97c480

File tree

7 files changed

+49
-37
lines changed

7 files changed

+49
-37
lines changed

src/event.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ where
554554
#[cfg(test)]
555555
mod tests {
556556
use super::*;
557-
use crate::tests::test_utils::TestPersister;
557+
use crate::test::utils::TestPersister;
558558

559559
#[test]
560560
fn event_queue_persistence() {

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ mod logger;
7474
mod payment_store;
7575
mod peer_store;
7676
#[cfg(test)]
77-
mod tests;
77+
mod test;
7878
mod types;
7979
mod wallet;
8080

src/payment_store.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ where
205205
#[cfg(test)]
206206
mod tests {
207207
use super::*;
208-
use crate::tests::test_utils::TestPersister;
208+
use crate::test::utils::TestPersister;
209209
use std::sync::Arc;
210210

211211
#[test]

src/peer_store.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ impl TryFrom<String> for PeerInfo {
173173
#[cfg(test)]
174174
mod tests {
175175
use super::*;
176-
use crate::tests::test_utils::TestPersister;
176+
use crate::test::utils::TestPersister;
177177
use proptest::prelude::*;
178178
use std::str::FromStr;
179179

src/tests/functional_tests.rs renamed to src/test/functional_tests.rs

+12-32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::tests::test_utils::expect_event;
2-
use crate::{Builder, Config, Error, Event, PaymentDirection, PaymentStatus};
1+
use crate::test::utils::{expect_event, random_config};
2+
use crate::{Builder, Error, Event, PaymentDirection, PaymentStatus};
33

44
use bitcoin::{Address, Amount, OutPoint, Txid};
55
use bitcoind::bitcoincore_rpc::RpcApi;
@@ -8,8 +8,6 @@ use electrsd::{bitcoind, bitcoind::BitcoinD, ElectrsD};
88
use electrum_client::ElectrumApi;
99

1010
use once_cell::sync::OnceCell;
11-
use rand::distributions::Alphanumeric;
12-
use rand::{thread_rng, Rng};
1311

1412
use std::env;
1513
use std::sync::Mutex;
@@ -137,38 +135,17 @@ fn premine_and_distribute_funds(addrs: Vec<Address>, amount: Amount) {
137135
generate_blocks_and_wait(1);
138136
}
139137

140-
fn rand_config() -> Config {
141-
let mut config = Config::default();
142-
143-
let esplora_url = get_electrsd().esplora_url.as_ref().unwrap();
144-
145-
println!("Setting esplora server URL: {}", esplora_url);
146-
config.esplora_server_url = format!("http://{}", esplora_url);
147-
148-
let mut rng = thread_rng();
149-
let rand_dir: String = (0..7).map(|_| rng.sample(Alphanumeric) as char).collect();
150-
let rand_path = format!("/tmp/{}", rand_dir);
151-
println!("Setting random LDK storage dir: {}", rand_dir);
152-
config.storage_dir_path = rand_path;
153-
154-
let rand_port: u16 = rng.gen_range(5000..8000);
155-
println!("Setting random LDK listening port: {}", rand_port);
156-
let listening_address = format!("127.0.0.1:{}", rand_port);
157-
config.listening_address = Some(listening_address);
158-
159-
config
160-
}
161-
162138
#[test]
163139
fn channel_full_cycle() {
164140
println!("== Node A ==");
165-
let config_a = rand_config();
141+
let esplora_url = get_electrsd().esplora_url.as_ref().unwrap();
142+
let config_a = random_config(esplora_url);
166143
let node_a = Builder::from_config(config_a).build();
167144
node_a.start().unwrap();
168145
let addr_a = node_a.new_funding_address().unwrap();
169146

170147
println!("\n== Node B ==");
171-
let config_b = rand_config();
148+
let config_b = random_config(esplora_url);
172149
let node_b = Builder::from_config(config_b).build();
173150
node_b.start().unwrap();
174151
let addr_b = node_b.new_funding_address().unwrap();
@@ -317,13 +294,14 @@ fn channel_full_cycle() {
317294
#[test]
318295
fn channel_open_fails_when_funds_insufficient() {
319296
println!("== Node A ==");
320-
let config_a = rand_config();
297+
let esplora_url = get_electrsd().esplora_url.as_ref().unwrap();
298+
let config_a = random_config(&esplora_url);
321299
let node_a = Builder::from_config(config_a).build();
322300
node_a.start().unwrap();
323301
let addr_a = node_a.new_funding_address().unwrap();
324302

325303
println!("\n== Node B ==");
326-
let config_b = rand_config();
304+
let config_b = random_config(&esplora_url);
327305
let node_b = Builder::from_config(config_b).build();
328306
node_b.start().unwrap();
329307
let addr_b = node_b.new_funding_address().unwrap();
@@ -344,7 +322,8 @@ fn channel_open_fails_when_funds_insufficient() {
344322

345323
#[test]
346324
fn connect_to_public_testnet_esplora() {
347-
let mut config = rand_config();
325+
let esplora_url = get_electrsd().esplora_url.as_ref().unwrap();
326+
let mut config = random_config(&esplora_url);
348327
config.esplora_server_url = "https://blockstream.info/testnet/api".to_string();
349328
config.network = bitcoin::Network::Testnet;
350329
let node = Builder::from_config(config).build();
@@ -355,7 +334,8 @@ fn connect_to_public_testnet_esplora() {
355334

356335
#[test]
357336
fn start_stop_reinit() {
358-
let config = rand_config();
337+
let esplora_url = get_electrsd().esplora_url.as_ref().unwrap();
338+
let config = random_config(&esplora_url);
359339
let node = Builder::from_config(config.clone()).build();
360340
let expected_node_id = node.node_id();
361341

Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
pub mod functional_tests;
2-
pub mod test_utils;
2+
pub mod utils;

src/tests/test_utils.rs renamed to src/test/utils.rs

+32
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use crate::io::KVStoreUnpersister;
2+
use crate::Config;
23
use lightning::util::persist::KVStorePersister;
34
use lightning::util::ser::Writeable;
45

6+
use rand::distributions::Alphanumeric;
7+
use rand::{thread_rng, Rng};
58
use std::collections::HashMap;
69
use std::sync::atomic::{AtomicBool, Ordering};
710
use std::sync::Mutex;
@@ -62,3 +65,32 @@ impl KVStoreUnpersister for TestPersister {
6265
Ok(persisted_bytes_lock.remove(key).is_some())
6366
}
6467
}
68+
69+
pub fn random_storage_path() -> String {
70+
let mut rng = thread_rng();
71+
let rand_dir: String = (0..7).map(|_| rng.sample(Alphanumeric) as char).collect();
72+
format!("/tmp/{}", rand_dir)
73+
}
74+
75+
pub fn random_port() -> u16 {
76+
let mut rng = thread_rng();
77+
rng.gen_range(5000..65535)
78+
}
79+
80+
pub fn random_config(esplora_url: &str) -> Config {
81+
let mut config = Config::default();
82+
83+
println!("Setting esplora server URL: {}", esplora_url);
84+
config.esplora_server_url = format!("http://{}", esplora_url);
85+
86+
let rand_dir = random_storage_path();
87+
println!("Setting random LDK storage dir: {}", rand_dir);
88+
config.storage_dir_path = rand_dir;
89+
90+
let rand_port = random_port();
91+
println!("Setting random LDK listening port: {}", rand_port);
92+
let listening_address = format!("127.0.0.1:{}", rand_port);
93+
config.listening_address = Some(listening_address);
94+
95+
config
96+
}

0 commit comments

Comments
 (0)