From 47489582189b5b4f637717554029a95e7e22f871 Mon Sep 17 00:00:00 2001 From: Max Audron Date: Wed, 26 May 2021 08:35:21 +0200 Subject: fix log breaking once buffer full the log_msg function was poping the newest message and replacing it with the newest message, it should be poping the oldest messages. --- src/hooks/sed.rs | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/hooks/sed.rs b/src/hooks/sed.rs index 095eb14..7416111 100644 --- a/src/hooks/sed.rs +++ b/src/hooks/sed.rs @@ -19,7 +19,7 @@ fn log_msg(msg: Message) -> Result<()> { LOG.with(|log_cell| { let mut log = log_cell.borrow_mut(); if log.len() >= LOG_MAX_SIZE { - let _ = log.pop(); + let _ = log.remove(0); } log.push((msg.source_nickname().unwrap().to_string(), text)) }); @@ -71,7 +71,7 @@ mod tests { "user".to_string(), "this is a long message which will be replaced".to_string(), )); - for _ in 0..LOG_MAX_SIZE-1 { + for _ in 0..LOG_MAX_SIZE - 1 { log.push(( "user".to_string(), "this is a long message which doesn't matter".to_string(), @@ -80,6 +80,37 @@ mod tests { }); } + #[test] + fn test_log_push_max() { + LOG.with(|log_cell| { + let mut log = log_cell.borrow_mut(); + log.push(("user".to_string(), "one".to_string())); + for _ in 0..LOG_MAX_SIZE - 2 { + log.push(("user".to_string(), "two".to_string())) + } + log.push(("user".to_string(), "three".to_string())); + + assert_eq!( + log[LOG_MAX_SIZE - 1], + ("user".to_string(), "three".to_string()) + ); + assert_eq!(log[0], ("user".to_string(), "one".to_string())); + }); + + log_msg(Message::new(Some("user!user@user.com"), "PRIVMSG", vec!["user", "four"]).unwrap()) + .unwrap(); + + LOG.with(|log_cell| { + let log = log_cell.borrow(); + + assert_eq!( + log[LOG_MAX_SIZE - 1], + ("user".to_string(), "four".to_string()) + ); + assert_eq!(log[0], ("user".to_string(), "two".to_string())); + }); + } + #[test] fn test_log_limit() { populate_log(); -- cgit v1.2.3