Skip to content

Commit

Permalink
replace mutex with cell
Browse files Browse the repository at this point in the history
  • Loading branch information
untitaker committed Jan 15, 2022
1 parent 90bbadd commit 6c15d93
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
10 changes: 6 additions & 4 deletions src/testutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
//! Those tests should only test public API surface in general, with some exceptions as provided by
//! this module.
use crate::Reader;
use std::sync::Mutex;
use std::cell::Cell;

pub use crate::utils::State;

thread_local! {
/// Buffer of all debugging output logged internally by html5gum.
pub static OUTPUT: Mutex<String> = Default::default();
pub static OUTPUT: Cell<String> = Default::default();
}

/// Simple debug logger for tests.
Expand All @@ -20,15 +20,17 @@ thread_local! {
///
/// A noop version for non-test builds is implemented in src/lib.rs
pub fn trace_log(msg: String) {
OUTPUT.with(|lock| {
let mut buf = lock.lock().unwrap();
OUTPUT.with(|cell| {
let mut buf = cell.take();
buf.push_str(&msg);
buf.push('\n');

if buf.len() > 20 * 1024 * 1024 {
buf.clear();
buf.push_str("[truncated output]\n");
}

cell.set(buf);
});
}

Expand Down
5 changes: 3 additions & 2 deletions tests/html5lib_tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,11 @@ fn main() {
Err(e) => {
let mut msg = String::new();

OUTPUT.with(|lock| {
let mut buf = lock.lock().unwrap();
OUTPUT.with(|cell| {
let mut buf = cell.take();
msg.push_str(&buf);
buf.clear();
cell.set(buf);
});

msg.push_str("\n");
Expand Down

0 comments on commit 6c15d93

Please sign in to comment.