Skip to content

Commit 8ca11b2

Browse files
committed
test: add TestConfig wrapper object
- `TestConfig` wraps `Config` and other fields, specifically a log-related field that can be over-written on a per-test basis. - With `TestConfig`, we can maintain the general testing setup/APIs as is and only modify fields based on specific features we want to test.
1 parent 5f0ab5d commit 8ca11b2

File tree

4 files changed

+58
-18
lines changed

4 files changed

+58
-18
lines changed

tests/common/mod.rs

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
#![cfg(any(test, cln_test, vss_test))]
99
#![allow(dead_code)]
1010

11-
use ldk_node::config::{Config, EsploraSyncConfig};
11+
use ldk_node::config::{
12+
Config, EsploraSyncConfig, DEFAULT_LOG_FILENAME, DEFAULT_LOG_LEVEL, DEFAULT_STORAGE_DIR_PATH,
13+
};
1214
use ldk_node::io::sqlite_store::SqliteStore;
13-
use ldk_node::logger::LogLevel;
15+
use ldk_node::logger::{LogLevel, LogWriter};
1416
use ldk_node::payment::{PaymentDirection, PaymentKind, PaymentStatus};
1517
use ldk_node::{
1618
Builder, CustomTlvRecord, Event, LightningBalance, Node, NodeError, PendingSweepBalance,
@@ -215,7 +217,7 @@ pub(crate) fn random_node_alias() -> Option<NodeAlias> {
215217
Some(NodeAlias(bytes))
216218
}
217219

218-
pub(crate) fn random_config(anchor_channels: bool) -> Config {
220+
pub(crate) fn random_config(anchor_channels: bool) -> TestConfig {
219221
let mut config = Config::default();
220222

221223
if !anchor_channels {
@@ -237,7 +239,7 @@ pub(crate) fn random_config(anchor_channels: bool) -> Config {
237239
println!("Setting random LDK node alias: {:?}", alias);
238240
config.node_alias = alias;
239241

240-
config
242+
TestConfig { node_config: config, log_writer: TestLogWriter::default() }
241243
}
242244

243245
#[cfg(feature = "uniffi")]
@@ -251,6 +253,34 @@ pub(crate) enum TestChainSource<'a> {
251253
BitcoindRpc(&'a BitcoinD),
252254
}
253255

256+
#[derive(Clone)]
257+
pub(crate) enum TestLogWriter {
258+
FileWriter { file_path: String, max_log_level: LogLevel },
259+
LogFacade { max_log_level: LogLevel },
260+
Custom(Arc<dyn LogWriter>),
261+
}
262+
263+
impl Default for TestLogWriter {
264+
fn default() -> Self {
265+
TestLogWriter::FileWriter {
266+
file_path: format!("{}/{}", DEFAULT_STORAGE_DIR_PATH, DEFAULT_LOG_FILENAME),
267+
max_log_level: DEFAULT_LOG_LEVEL,
268+
}
269+
}
270+
}
271+
272+
#[derive(Clone)]
273+
pub(crate) struct TestConfig {
274+
pub node_config: Config,
275+
pub log_writer: TestLogWriter,
276+
}
277+
278+
impl Default for TestConfig {
279+
fn default() -> Self {
280+
Self { node_config: Config::default(), log_writer: TestLogWriter::default() }
281+
}
282+
}
283+
254284
macro_rules! setup_builder {
255285
($builder: ident, $config: expr) => {
256286
#[cfg(feature = "uniffi")]
@@ -273,10 +303,11 @@ pub(crate) fn setup_two_nodes(
273303
println!("\n== Node B ==");
274304
let mut config_b = random_config(anchor_channels);
275305
if allow_0conf {
276-
config_b.trusted_peers_0conf.push(node_a.node_id());
306+
config_b.node_config.trusted_peers_0conf.push(node_a.node_id());
277307
}
278308
if anchor_channels && anchors_trusted_no_reserve {
279309
config_b
310+
.node_config
280311
.anchor_channels_config
281312
.as_mut()
282313
.unwrap()
@@ -288,9 +319,9 @@ pub(crate) fn setup_two_nodes(
288319
}
289320

290321
pub(crate) fn setup_node(
291-
chain_source: &TestChainSource, config: Config, seed_bytes: Option<Vec<u8>>,
322+
chain_source: &TestChainSource, config: TestConfig, seed_bytes: Option<Vec<u8>>,
292323
) -> TestNode {
293-
setup_builder!(builder, config);
324+
setup_builder!(builder, config.node_config);
294325
match chain_source {
295326
TestChainSource::Esplora(electrsd) => {
296327
let esplora_url = format!("http://{}", electrsd.esplora_url.as_ref().unwrap());
@@ -309,14 +340,23 @@ pub(crate) fn setup_node(
309340
},
310341
}
311342

312-
let log_file_path = format!("{}/{}", config.storage_dir_path, "ldk_node.log");
313-
builder.set_filesystem_logger(Some(log_file_path), Some(LogLevel::Gossip));
343+
match &config.log_writer {
344+
TestLogWriter::FileWriter { file_path, max_log_level } => {
345+
builder.set_filesystem_logger(Some(file_path.clone()), Some(*max_log_level));
346+
},
347+
TestLogWriter::LogFacade { max_log_level } => {
348+
builder.set_log_facade_logger(Some(*max_log_level));
349+
},
350+
TestLogWriter::Custom(custom_log_writer) => {
351+
builder.set_custom_logger(Arc::clone(custom_log_writer));
352+
},
353+
}
314354

315355
if let Some(seed) = seed_bytes {
316356
builder.set_entropy_seed_bytes(seed).unwrap();
317357
}
318358

319-
let test_sync_store = Arc::new(TestSyncStore::new(config.storage_dir_path.into()));
359+
let test_sync_store = Arc::new(TestSyncStore::new(config.node_config.storage_dir_path.into()));
320360
let node = builder.build_with_store(test_sync_store).unwrap();
321361
node.start().unwrap();
322362
assert!(node.status().is_running);

tests/integration_tests_cln.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fn test_cln() {
4545

4646
// Setup LDK Node
4747
let config = common::random_config(true);
48-
let mut builder = Builder::from_config(config);
48+
let mut builder = Builder::from_config(config.node_config);
4949
builder.set_chain_source_esplora("http://127.0.0.1:3002".to_string(), None);
5050

5151
let node = builder.build().unwrap();

tests/integration_tests_rust.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ fn multi_hop_sending() {
126126
let mut sync_config = EsploraSyncConfig::default();
127127
sync_config.onchain_wallet_sync_interval_secs = 100000;
128128
sync_config.lightning_wallet_sync_interval_secs = 100000;
129-
setup_builder!(builder, config);
129+
setup_builder!(builder, config.node_config);
130130
builder.set_chain_source_esplora(esplora_url.clone(), Some(sync_config));
131131
let node = builder.build().unwrap();
132132
node.start().unwrap();
@@ -217,12 +217,12 @@ fn start_stop_reinit() {
217217
let esplora_url = format!("http://{}", electrsd.esplora_url.as_ref().unwrap());
218218

219219
let test_sync_store: Arc<dyn KVStore + Sync + Send> =
220-
Arc::new(TestSyncStore::new(config.storage_dir_path.clone().into()));
220+
Arc::new(TestSyncStore::new(config.node_config.storage_dir_path.clone().into()));
221221

222222
let mut sync_config = EsploraSyncConfig::default();
223223
sync_config.onchain_wallet_sync_interval_secs = 100000;
224224
sync_config.lightning_wallet_sync_interval_secs = 100000;
225-
setup_builder!(builder, config);
225+
setup_builder!(builder, config.node_config);
226226
builder.set_chain_source_esplora(esplora_url.clone(), Some(sync_config));
227227

228228
let node = builder.build_with_store(Arc::clone(&test_sync_store)).unwrap();
@@ -246,7 +246,7 @@ fn start_stop_reinit() {
246246
node.sync_wallets().unwrap();
247247
assert_eq!(node.list_balances().spendable_onchain_balance_sats, expected_amount.to_sat());
248248

249-
let log_file = format!("{}/ldk_node.log", config.clone().storage_dir_path);
249+
let log_file = format!("{}/ldk_node.log", config.node_config.clone().storage_dir_path);
250250
assert!(std::path::Path::new(&log_file).exists());
251251

252252
node.stop().unwrap();
@@ -259,7 +259,7 @@ fn start_stop_reinit() {
259259
assert_eq!(node.stop(), Err(NodeError::NotRunning));
260260
drop(node);
261261

262-
setup_builder!(builder, config);
262+
setup_builder!(builder, config.node_config);
263263
builder.set_chain_source_esplora(esplora_url.clone(), Some(sync_config));
264264

265265
let reinitialized_node = builder.build_with_store(Arc::clone(&test_sync_store)).unwrap();

tests/integration_tests_vss.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn channel_full_cycle_with_vss_store() {
1818
println!("== Node A ==");
1919
let esplora_url = format!("http://{}", electrsd.esplora_url.as_ref().unwrap());
2020
let config_a = common::random_config(true);
21-
let mut builder_a = Builder::from_config(config_a);
21+
let mut builder_a = Builder::from_config(config_a.node_config);
2222
builder_a.set_chain_source_esplora(esplora_url.clone(), None);
2323
let vss_base_url = std::env::var("TEST_VSS_BASE_URL").unwrap();
2424
let node_a = builder_a
@@ -32,7 +32,7 @@ fn channel_full_cycle_with_vss_store() {
3232

3333
println!("\n== Node B ==");
3434
let config_b = common::random_config(true);
35-
let mut builder_b = Builder::from_config(config_b);
35+
let mut builder_b = Builder::from_config(config_b.node_config);
3636
builder_b.set_chain_source_esplora(esplora_url.clone(), None);
3737
let node_b = builder_b
3838
.build_with_vss_store_and_fixed_headers(

0 commit comments

Comments
 (0)