1
#[allow(unused_imports)]
2
pub use super::*;
3
#[cfg(all(test, feature = "std"))]
4
mod tests {
5
    use super::*;
6

            
7
    // Uses the workspace-wide lock: see `diagnostics::test_lock`.
8

            
9
    #[test]
10
1
    fn a_recorded_diagnostic_is_findable_and_clearable() {
11
1
        let _g = test_lock().lock();
12
1
        clear();
13
1
        emit("[azul][test-lint] hello".to_string());
14
1
        assert!(any_contains("test-lint"));
15
1
        assert!(!any_contains("absent-lint"));
16
1
        clear();
17
1
        assert!(!any_contains("test-lint"), "clear() must empty the ring");
18
1
    }
19

            
20
    #[test]
21
1
    fn the_ring_is_bounded() {
22
1
        let _g = test_lock().lock();
23
1
        clear();
24
306
        for i in 0..(CAPACITY + 50) {
25
306
            record(format!("msg {i}"));
26
306
        }
27
1
        assert_eq!(
28
1
            recorded().len(),
29
            CAPACITY,
30
            "an every-frame lint must not grow without bound — the point is to catch churn, not \
31
             become it"
32
        );
33
1
        assert!(
34
1
            !any_contains("msg 0"),
35
            "the oldest entries are dropped first"
36
        );
37
1
        clear();
38
1
    }
39
}
40

            
41
/// A diagnostic must never take the process down because its destination
42
/// closed. `eprintln!` panics on a failed write, so piping a running app into
43
/// `head` killed it the moment `head` exited - see `write_diagnostic`'s own
44
/// doc for the captured backtrace.
45
#[test]
46
1
fn a_diagnostic_survives_a_destination_that_is_gone() {
47
    use std::io::{Error, ErrorKind, Write};
48

            
49
    struct ClosedPipe;
50
    impl Write for ClosedPipe {
51
1
        fn write(&mut self, _: &[u8]) -> std::io::Result<usize> {
52
1
            Err(Error::new(ErrorKind::BrokenPipe, "broken pipe"))
53
1
        }
54
        fn flush(&mut self) -> std::io::Result<()> {
55
            Err(Error::new(ErrorKind::BrokenPipe, "broken pipe"))
56
        }
57
    }
58

            
59
    // The assertion is that this RETURNS. With `writeln!(..).unwrap()` or
60
    // `eprintln!` behind it, this test panics instead.
61
1
    super::write_diagnostic(ClosedPipe, "a warning nobody can hear");
62
1
}