aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Audron <audron@cocaine.farm>2021-05-26 08:35:21 +0200
committerMax Audron <audron@cocaine.farm>2021-05-26 08:35:21 +0200
commit47489582189b5b4f637717554029a95e7e22f871 (patch)
tree80c4acbd4a3311f1ddaa5bfc0efea65e7714ffd1
parentadd deployment stuff (diff)
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.
Diffstat (limited to '')
-rw-r--r--src/hooks/sed.rs35
1 files changed, 33 insertions, 2 deletions
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(),
@@ -81,6 +81,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();