tracing/tracing-flame/tests/concurrent.rs
Eliza Weisman ad9ef79d73 flame: replace tempdir with tempfile
Similarly, `tracing-flame`'s tests also use the unmaintained `tempdir`
crate. This commit replaces it with `tempfile`.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2021-08-16 14:10:12 -07:00

44 lines
1.4 KiB
Rust

use std::thread::sleep;
use std::time::Duration;
use tracing::{span, Level};
use tracing_flame::FlameSubscriber;
use tracing_subscriber::{prelude::*, registry::Registry};
#[test]
fn capture_supported() {
let tmp_dir = tempfile::Builder::new()
.prefix("tracing-flamegraph-test-")
.tempdir()
.expect("failed to create tempdir");
let path = tmp_dir.path().join("tracing.folded");
let (flame_layer, flame_guard) = FlameSubscriber::with_file(&path).unwrap();
let subscriber = Registry::default().with(flame_layer);
tracing::collect::set_global_default(subscriber).expect("Could not set global default");
let span = span!(Level::ERROR, "main");
let _guard = span.enter();
let thread = span!(Level::ERROR, "outer").in_scope(|| {
sleep(Duration::from_millis(10));
let span = span!(Level::ERROR, "Inner");
let thread = std::thread::spawn(move || {
span.in_scope(|| {
sleep(Duration::from_millis(50));
});
});
sleep(Duration::from_millis(20));
thread
});
sleep(Duration::from_millis(100));
thread.join().unwrap();
flame_guard.flush().unwrap();
let traces = std::fs::read_to_string(&path).unwrap();
println!("{}", traces);
assert_eq!(5, traces.lines().count());
tmp_dir.close().expect("failed to delete tempdir");
}