From: Antoine Riard Date: Mon, 30 Dec 2019 22:30:27 +0000 (-0500) Subject: Add assert_log method to TestLogger to harden test X-Git-Tag: v0.0.12~161^2~1 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=29ace598deac67f0bc9d6bad3d251d55af3a6ebd;p=rust-lightning Add assert_log method to TestLogger to harden test Extend mock Node with logger. --- diff --git a/lightning/src/ln/functional_test_utils.rs b/lightning/src/ln/functional_test_utils.rs index 45e04b946..e99e96097 100644 --- a/lightning/src/ln/functional_test_utils.rs +++ b/lightning/src/ln/functional_test_utils.rs @@ -66,6 +66,7 @@ pub struct Node { pub node_seed: [u8; 32], pub network_payment_count: Rc>, pub network_chan_count: Rc>, + pub logger: Arc } impl Drop for Node { fn drop(&mut self) { @@ -835,7 +836,8 @@ pub fn create_network(node_count: usize, node_config: &[Option]) -> let payment_count = Rc::new(RefCell::new(0)); for i in 0..node_count { - let logger: Arc = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i))); + let test_logger = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i))); + let logger = &(Arc::clone(&test_logger) as Arc); let feeest = Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 }); let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger))); let block_notifier = Arc::new(chaininterface::BlockNotifier::new(chain_monitor.clone())); @@ -857,6 +859,7 @@ pub fn create_network(node_count: usize, node_config: &[Option]) -> network_payment_count: payment_count.clone(), network_chan_count: chan_count.clone(), block_notifier, + logger: test_logger }); } diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index 6ea1265c3..4381e2176 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -22,6 +22,7 @@ use secp256k1::{SecretKey, PublicKey}; use std::time::{SystemTime, UNIX_EPOCH}; use std::sync::{Arc,Mutex}; use std::{mem}; +use std::collections::HashMap; pub struct TestVecWriter(pub Vec); impl Writer for TestVecWriter { @@ -190,6 +191,7 @@ impl msgs::RoutingMessageHandler for TestRoutingMessageHandler { pub struct TestLogger { level: Level, id: String, + pub lines: Mutex>, } impl TestLogger { @@ -200,15 +202,21 @@ impl TestLogger { TestLogger { level: Level::Trace, id, + lines: Mutex::new(HashMap::new()) } } pub fn enable(&mut self, level: Level) { self.level = level; } + pub fn assert_log(&self, module: String, line: String, count: usize) { + let log_entries = self.lines.lock().unwrap(); + assert_eq!(log_entries.get(&(module, line)), Some(&count)); + } } impl Logger for TestLogger { fn log(&self, record: &Record) { + *self.lines.lock().unwrap().entry((record.module_path.to_string(), format!("{}", record.args))).or_insert(0) += 1; if self.level >= record.level { println!("{:<5} {} [{} : {}, {}] {}", record.level.to_string(), self.id, record.module_path, record.file, record.line, record.args); }