Skip to content

Commit

Permalink
test: add TestConfig wrapper object
Browse files Browse the repository at this point in the history
- `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.
  • Loading branch information
enigbe committed Feb 20, 2025
1 parent 4780f94 commit 7ca8ab9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 18 deletions.
60 changes: 50 additions & 10 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
#![cfg(any(test, cln_test, vss_test))]
#![allow(dead_code)]

use ldk_node::config::{Config, EsploraSyncConfig};
use ldk_node::config::{
Config, EsploraSyncConfig, DEFAULT_LOG_FILENAME, DEFAULT_LOG_LEVEL, DEFAULT_STORAGE_DIR_PATH,
};
use ldk_node::io::sqlite_store::SqliteStore;
use ldk_node::logger::LogLevel;
use ldk_node::logger::{LogLevel, LogWriter};
use ldk_node::payment::{PaymentDirection, PaymentKind, PaymentStatus};
use ldk_node::{
Builder, CustomTlvRecord, Event, LightningBalance, Node, NodeError, PendingSweepBalance,
Expand Down Expand Up @@ -215,7 +217,7 @@ pub(crate) fn random_node_alias() -> Option<NodeAlias> {
Some(NodeAlias(bytes))
}

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

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

config
TestConfig { node_config: config, log_writer: TestLogWriter::default() }
}

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

#[derive(Clone)]
pub(crate) enum TestLogWriter {
FileWriter { file_path: String, max_log_level: LogLevel },
LogFacade { max_log_level: LogLevel },
Custom(Arc<dyn LogWriter>),
}

impl Default for TestLogWriter {
fn default() -> Self {
TestLogWriter::FileWriter {
file_path: format!("{}/{}", DEFAULT_STORAGE_DIR_PATH, DEFAULT_LOG_FILENAME),
max_log_level: DEFAULT_LOG_LEVEL,
}
}
}

#[derive(Clone)]
pub(crate) struct TestConfig {
pub node_config: Config,
pub log_writer: TestLogWriter,
}

impl Default for TestConfig {
fn default() -> Self {
Self { node_config: Config::default(), log_writer: TestLogWriter::default() }
}
}

macro_rules! setup_builder {
($builder: ident, $config: expr) => {
#[cfg(feature = "uniffi")]
Expand All @@ -273,10 +303,11 @@ pub(crate) fn setup_two_nodes(
println!("\n== Node B ==");
let mut config_b = random_config(anchor_channels);
if allow_0conf {
config_b.trusted_peers_0conf.push(node_a.node_id());
config_b.node_config.trusted_peers_0conf.push(node_a.node_id());
}
if anchor_channels && anchors_trusted_no_reserve {
config_b
.node_config
.anchor_channels_config
.as_mut()
.unwrap()
Expand All @@ -288,9 +319,9 @@ pub(crate) fn setup_two_nodes(
}

pub(crate) fn setup_node(
chain_source: &TestChainSource, config: Config, seed_bytes: Option<Vec<u8>>,
chain_source: &TestChainSource, config: TestConfig, seed_bytes: Option<Vec<u8>>,
) -> TestNode {
setup_builder!(builder, config);
setup_builder!(builder, config.node_config);
match chain_source {
TestChainSource::Esplora(electrsd) => {
let esplora_url = format!("http://{}", electrsd.esplora_url.as_ref().unwrap());
Expand All @@ -309,14 +340,23 @@ pub(crate) fn setup_node(
},
}

let log_file_path = format!("{}/{}", config.storage_dir_path, "ldk_node.log");
builder.set_filesystem_logger(Some(log_file_path), Some(LogLevel::Gossip));
match &config.log_writer {
TestLogWriter::FileWriter { file_path, max_log_level } => {
builder.set_filesystem_logger(Some(file_path.clone()), Some(*max_log_level));
},
TestLogWriter::LogFacade { max_log_level } => {
builder.set_log_facade_logger(Some(*max_log_level));
},
TestLogWriter::Custom(custom_log_writer) => {
builder.set_custom_logger(Arc::clone(custom_log_writer));
},
}

if let Some(seed) = seed_bytes {
builder.set_entropy_seed_bytes(seed).unwrap();
}

let test_sync_store = Arc::new(TestSyncStore::new(config.storage_dir_path.into()));
let test_sync_store = Arc::new(TestSyncStore::new(config.node_config.storage_dir_path.into()));
let node = builder.build_with_store(test_sync_store).unwrap();
node.start().unwrap();
assert!(node.status().is_running);
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_tests_cln.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn test_cln() {

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

let node = builder.build().unwrap();
Expand Down
10 changes: 5 additions & 5 deletions tests/integration_tests_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ fn multi_hop_sending() {
let mut sync_config = EsploraSyncConfig::default();
sync_config.onchain_wallet_sync_interval_secs = 100000;
sync_config.lightning_wallet_sync_interval_secs = 100000;
setup_builder!(builder, config);
setup_builder!(builder, config.node_config);
builder.set_chain_source_esplora(esplora_url.clone(), Some(sync_config));
let node = builder.build().unwrap();
node.start().unwrap();
Expand Down Expand Up @@ -221,12 +221,12 @@ fn start_stop_reinit() {
let esplora_url = format!("http://{}", electrsd.esplora_url.as_ref().unwrap());

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

let mut sync_config = EsploraSyncConfig::default();
sync_config.onchain_wallet_sync_interval_secs = 100000;
sync_config.lightning_wallet_sync_interval_secs = 100000;
setup_builder!(builder, config);
setup_builder!(builder, config.node_config);
builder.set_chain_source_esplora(esplora_url.clone(), Some(sync_config));

let node = builder.build_with_store(Arc::clone(&test_sync_store)).unwrap();
Expand All @@ -250,7 +250,7 @@ fn start_stop_reinit() {
node.sync_wallets().unwrap();
assert_eq!(node.list_balances().spendable_onchain_balance_sats, expected_amount.to_sat());

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

node.stop().unwrap();
Expand All @@ -263,7 +263,7 @@ fn start_stop_reinit() {
assert_eq!(node.stop(), Err(NodeError::NotRunning));
drop(node);

setup_builder!(builder, config);
setup_builder!(builder, config.node_config);
builder.set_chain_source_esplora(esplora_url.clone(), Some(sync_config));

let reinitialized_node = builder.build_with_store(Arc::clone(&test_sync_store)).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions tests/integration_tests_vss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn channel_full_cycle_with_vss_store() {
println!("== Node A ==");
let esplora_url = format!("http://{}", electrsd.esplora_url.as_ref().unwrap());
let config_a = common::random_config(true);
let mut builder_a = Builder::from_config(config_a);
let mut builder_a = Builder::from_config(config_a.node_config);
builder_a.set_chain_source_esplora(esplora_url.clone(), None);
let vss_base_url = std::env::var("TEST_VSS_BASE_URL").unwrap();
let node_a = builder_a
Expand All @@ -32,7 +32,7 @@ fn channel_full_cycle_with_vss_store() {

println!("\n== Node B ==");
let config_b = common::random_config(true);
let mut builder_b = Builder::from_config(config_b);
let mut builder_b = Builder::from_config(config_b.node_config);
builder_b.set_chain_source_esplora(esplora_url.clone(), None);
let node_b = builder_b
.build_with_vss_store_and_fixed_headers(
Expand Down

0 comments on commit 7ca8ab9

Please sign in to comment.