Skip to content

Commit 62d69e0

Browse files
author
Antoine Riard
committed
Add assert_log method to Logger trait to harden test
Extend mock Node with logger.
1 parent 9310813 commit 62d69e0

File tree

5 files changed

+16
-0
lines changed

5 files changed

+16
-0
lines changed

fuzz/src/full_stack.rs

+2
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,8 @@ mod tests {
553553
*self.lines.lock().unwrap().entry((record.module_path.to_string(), format!("{}", record.args))).or_insert(0) += 1;
554554
println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args);
555555
}
556+
557+
fn assert_log(&self, _module: String, _line: String, _count: usize) {}
556558
}
557559

558560
#[test]

fuzz/src/utils/test_logger.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ impl Logger for TestLogger {
2020
#[cfg(not(test))]
2121
let _ = format!("{}", record.args);
2222
}
23+
fn assert_log(&self, _module: String, _line: String, _count: usize) {}
2324
}

lightning/src/ln/functional_test_utils.rs

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ pub struct Node {
6666
pub node_seed: [u8; 32],
6767
pub network_payment_count: Rc<RefCell<u8>>,
6868
pub network_chan_count: Rc<RefCell<u32>>,
69+
pub logger: Arc<Logger>
6970
}
7071
impl Drop for Node {
7172
fn drop(&mut self) {
@@ -857,6 +858,7 @@ pub fn create_network(node_count: usize, node_config: &[Option<UserConfig>]) ->
857858
network_payment_count: payment_count.clone(),
858859
network_chan_count: chan_count.clone(),
859860
block_notifier,
861+
logger
860862
});
861863
}
862864

lightning/src/util/logger.rs

+2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ impl<'a> Record<'a> {
119119
pub trait Logger: Sync + Send {
120120
/// Logs the `Record`
121121
fn log(&self, record: &Record);
122+
/// Assert log count (used to harden test)
123+
fn assert_log(&self, module: String, line: String, count: usize);
122124
}
123125

124126
pub(crate) struct LogHolder<'a> { pub(crate) logger: &'a Arc<Logger> }

lightning/src/util/test_utils.rs

+9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use secp256k1::{SecretKey, PublicKey};
2222
use std::time::{SystemTime, UNIX_EPOCH};
2323
use std::sync::{Arc,Mutex};
2424
use std::{mem};
25+
use std::collections::HashMap;
2526

2627
pub struct TestVecWriter(pub Vec<u8>);
2728
impl Writer for TestVecWriter {
@@ -190,6 +191,7 @@ impl msgs::RoutingMessageHandler for TestRoutingMessageHandler {
190191
pub struct TestLogger {
191192
level: Level,
192193
id: String,
194+
pub lines: Mutex<HashMap<(String, String), usize>>,
193195
}
194196

195197
impl TestLogger {
@@ -200,6 +202,7 @@ impl TestLogger {
200202
TestLogger {
201203
level: Level::Trace,
202204
id,
205+
lines: Mutex::new(HashMap::new())
203206
}
204207
}
205208
pub fn enable(&mut self, level: Level) {
@@ -209,10 +212,16 @@ impl TestLogger {
209212

210213
impl Logger for TestLogger {
211214
fn log(&self, record: &Record) {
215+
*self.lines.lock().unwrap().entry((record.module_path.to_string(), format!("{}", record.args))).or_insert(0) += 1;
212216
if self.level >= record.level {
213217
println!("{:<5} {} [{} : {}, {}] {}", record.level.to_string(), self.id, record.module_path, record.file, record.line, record.args);
214218
}
215219
}
220+
221+
fn assert_log(&self, module: String, line: String, count: usize) {
222+
let log_entries = self.lines.lock().unwrap();
223+
assert_eq!(log_entries.get(&(module, line)), Some(&count));
224+
}
216225
}
217226

218227
pub struct TestKeysInterface {

0 commit comments

Comments
 (0)