Skip to content

Commit c84115d

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 edb0560 commit c84115d

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,
@@ -221,7 +223,7 @@ pub(crate) fn random_node_alias() -> Option<NodeAlias> {
221223
Some(NodeAlias(bytes))
222224
}
223225

224-
pub(crate) fn random_config(anchor_channels: bool) -> Config {
226+
pub(crate) fn random_config(anchor_channels: bool) -> TestConfig {
225227
let mut config = Config::default();
226228

227229
if !anchor_channels {
@@ -243,7 +245,7 @@ pub(crate) fn random_config(anchor_channels: bool) -> Config {
243245
println!("Setting random LDK node alias: {:?}", alias);
244246
config.node_alias = alias;
245247

246-
config
248+
TestConfig { node_config: config, log_writer: TestLogWriter::default() }
247249
}
248250

249251
#[cfg(feature = "uniffi")]
@@ -257,6 +259,34 @@ pub(crate) enum TestChainSource<'a> {
257259
BitcoindRpc(&'a BitcoinD),
258260
}
259261

262+
#[derive(Clone)]
263+
pub(crate) enum TestLogWriter {
264+
FileWriter { file_path: String, max_log_level: LogLevel },
265+
LogFacade { max_log_level: LogLevel },
266+
Custom(Arc<dyn LogWriter>),
267+
}
268+
269+
impl Default for TestLogWriter {
270+
fn default() -> Self {
271+
TestLogWriter::FileWriter {
272+
file_path: format!("{}/{}", DEFAULT_STORAGE_DIR_PATH, DEFAULT_LOG_FILENAME),
273+
max_log_level: DEFAULT_LOG_LEVEL,
274+
}
275+
}
276+
}
277+
278+
#[derive(Clone)]
279+
pub(crate) struct TestConfig {
280+
pub node_config: Config,
281+
pub log_writer: TestLogWriter,
282+
}
283+
284+
impl Default for TestConfig {
285+
fn default() -> Self {
286+
Self { node_config: Config::default(), log_writer: TestLogWriter::default() }
287+
}
288+
}
289+
260290
macro_rules! setup_builder {
261291
($builder: ident, $config: expr) => {
262292
#[cfg(feature = "uniffi")]
@@ -279,10 +309,11 @@ pub(crate) fn setup_two_nodes(
279309
println!("\n== Node B ==");
280310
let mut config_b = random_config(anchor_channels);
281311
if allow_0conf {
282-
config_b.trusted_peers_0conf.push(node_a.node_id());
312+
config_b.node_config.trusted_peers_0conf.push(node_a.node_id());
283313
}
284314
if anchor_channels && anchors_trusted_no_reserve {
285315
config_b
316+
.node_config
286317
.anchor_channels_config
287318
.as_mut()
288319
.unwrap()
@@ -294,9 +325,9 @@ pub(crate) fn setup_two_nodes(
294325
}
295326

296327
pub(crate) fn setup_node(
297-
chain_source: &TestChainSource, config: Config, seed_bytes: Option<Vec<u8>>,
328+
chain_source: &TestChainSource, config: TestConfig, seed_bytes: Option<Vec<u8>>,
298329
) -> TestNode {
299-
setup_builder!(builder, config);
330+
setup_builder!(builder, config.node_config);
300331
match chain_source {
301332
TestChainSource::Esplora(electrsd) => {
302333
let esplora_url = format!("http://{}", electrsd.esplora_url.as_ref().unwrap());
@@ -315,14 +346,23 @@ pub(crate) fn setup_node(
315346
},
316347
}
317348

318-
let log_file_path = format!("{}/{}", config.storage_dir_path, "ldk_node.log");
319-
builder.set_filesystem_logger(Some(log_file_path), Some(LogLevel::Gossip));
349+
match &config.log_writer {
350+
TestLogWriter::FileWriter { file_path, max_log_level } => {
351+
builder.set_filesystem_logger(Some(file_path.clone()), Some(*max_log_level));
352+
},
353+
TestLogWriter::LogFacade { max_log_level } => {
354+
builder.set_log_facade_logger(Some(*max_log_level));
355+
},
356+
TestLogWriter::Custom(custom_log_writer) => {
357+
builder.set_custom_logger(Arc::clone(custom_log_writer));
358+
},
359+
}
320360

321361
if let Some(seed) = seed_bytes {
322362
builder.set_entropy_seed_bytes(seed).unwrap();
323363
}
324364

325-
let test_sync_store = Arc::new(TestSyncStore::new(config.storage_dir_path.into()));
365+
let test_sync_store = Arc::new(TestSyncStore::new(config.node_config.storage_dir_path.into()));
326366
let node = builder.build_with_store(test_sync_store).unwrap();
327367
node.start().unwrap();
328368
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
@@ -128,7 +128,7 @@ fn multi_hop_sending() {
128128
let mut sync_config = EsploraSyncConfig::default();
129129
sync_config.onchain_wallet_sync_interval_secs = 100000;
130130
sync_config.lightning_wallet_sync_interval_secs = 100000;
131-
setup_builder!(builder, config);
131+
setup_builder!(builder, config.node_config);
132132
builder.set_chain_source_esplora(esplora_url.clone(), Some(sync_config));
133133
let node = builder.build().unwrap();
134134
node.start().unwrap();
@@ -223,12 +223,12 @@ fn start_stop_reinit() {
223223
let esplora_url = format!("http://{}", electrsd.esplora_url.as_ref().unwrap());
224224

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

228228
let mut sync_config = EsploraSyncConfig::default();
229229
sync_config.onchain_wallet_sync_interval_secs = 100000;
230230
sync_config.lightning_wallet_sync_interval_secs = 100000;
231-
setup_builder!(builder, config);
231+
setup_builder!(builder, config.node_config);
232232
builder.set_chain_source_esplora(esplora_url.clone(), Some(sync_config));
233233

234234
let node = builder.build_with_store(Arc::clone(&test_sync_store)).unwrap();
@@ -252,7 +252,7 @@ fn start_stop_reinit() {
252252
node.sync_wallets().unwrap();
253253
assert_eq!(node.list_balances().spendable_onchain_balance_sats, expected_amount.to_sat());
254254

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

258258
node.stop().unwrap();
@@ -265,7 +265,7 @@ fn start_stop_reinit() {
265265
assert_eq!(node.stop(), Err(NodeError::NotRunning));
266266
drop(node);
267267

268-
setup_builder!(builder, config);
268+
setup_builder!(builder, config.node_config);
269269
builder.set_chain_source_esplora(esplora_url.clone(), Some(sync_config));
270270

271271
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)