Skip to content

Commit

Permalink
Add test for deadlock avoidance in events within events
Browse files Browse the repository at this point in the history
  • Loading branch information
mladedav committed May 22, 2024
1 parent 549acc7 commit 3b9bdc6
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,3 +697,51 @@ impl<L: Iterator<Item = T>, R: Iterator<Item = T>, T, U: PartialEq, F: Fn(&T) ->
}
}
}

#[cfg(test)]
mod tests {
use std::{io, str, sync::Mutex};

use tracing::subscriber::set_global_default;
use tracing_subscriber::{layer::SubscriberExt, registry};

use crate::HierarchicalLayer;

struct RecursiveWriter(Mutex<Vec<u8>>);

impl io::Write for &RecursiveWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.lock().unwrap().extend(buf);

tracing::error!("Nobody expects the Spanish Inquisition");

Ok(buf.len())
}

fn flush(&mut self) -> io::Result<()> {
tracing::error!("Nobody expects the Spanish Inquisition");
Ok(())
}
}

/// This test checks that if `tracing` events happen during processing of
/// `on_event`, the library does not deadlock.
#[test]
fn recursive_event() {
static WRITER: RecursiveWriter = RecursiveWriter(Mutex::new(Vec::new()));

let subscriber = registry().with(HierarchicalLayer::new(2).with_writer(|| &WRITER));
set_global_default(subscriber).unwrap();

tracing::error!("We can never expect the unexpected.");

let output = WRITER.0.lock().unwrap();
let output = str::from_utf8(&output).unwrap();

// If this test finished we're happy. Let's just also check that we did
// in fact log _something_ and that the logs from within the writer did
// not actually go through.
assert!(output.contains("We can never expect the unexpected."));
assert!(!output.contains("Nobody expects the Spanish Inquisition"));
}
}

0 comments on commit 3b9bdc6

Please sign in to comment.