X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Ftypes.rs;h=0c6c9b2533a7404c556e4e3a15bfa673bd178bc5;hb=1c181eeae185d576c70592571550b3e4ba004446;hp=0b03081f35814eda15182f110bcf96cbf9cc6993;hpb=5eb833bb7ca47f76d8853e44121cfc785fe1a1c3;p=rapid-gossip-sync-server diff --git a/src/types.rs b/src/types.rs index 0b03081..0c6c9b2 100644 --- a/src/types.rs +++ b/src/types.rs @@ -14,8 +14,9 @@ pub(crate) type GossipPeerManager = Arc), + ChannelUpdate(ChannelUpdate, Option), } #[derive(Clone, Copy)] @@ -28,7 +29,7 @@ impl RGSSLogger { } impl Logger for RGSSLogger { - fn log(&self, record: &Record) { + fn log(&self, record: Record) { let threshold = config::log_level(); if record.level < threshold { return; @@ -36,3 +37,56 @@ impl Logger for RGSSLogger { println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args); } } + +#[cfg(test)] +pub mod tests { + use std::collections::HashMap; + use std::sync::{Mutex}; + use lightning::util::logger::{Level, Logger, Record}; + + pub struct TestLogger { + level: Level, + pub(crate) id: String, + pub lines: Mutex>, + } + + impl TestLogger { + pub fn new() -> TestLogger { + let id = crate::tests::db_test_schema(); + Self::with_id(id) + } + pub fn with_id(id: String) -> TestLogger { + TestLogger { + level: Level::Gossip, + 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)); + } + + /// Search for the number of occurrence of the logged lines which + /// 1. belongs to the specified module and + /// 2. contains `line` in it. + /// And asserts if the number of occurrences is the same with the given `count` + pub fn assert_log_contains(&self, module: &str, line: &str, count: usize) { + let log_entries = self.lines.lock().unwrap(); + let l: usize = log_entries.iter().filter(|&(&(ref m, ref l), _c)| { + m == module && l.contains(line) + }).map(|(_, c)| { c }).sum(); + assert_eq!(l, 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; + println!("{:<5} {} [{} : {}, {}] {}", record.level.to_string(), self.id, record.module_path, record.file, record.line, record.args); + } + } +}