use crate::Syslog; #[test] fn call_init_twice() { // 0. First test that a first call to initialize works. if Syslog::builder().build().init().is_err() { panic!("First request to initialize Syslog failed."); } // 1. Then assure that another attempt fails, as there can only be one logger. // // Reminder to self: Since this init() already returns a Result, there is no real need to // deal with implementing a separate check with `std::sync::Once`. The log crate practically // covers our ass here. if Syslog::builder().build().init().is_ok() { panic!("Second request to initialize Syslog unexpectedly succeeded."); } } // NOTE It would be neat to have higher test coverage, including some of the actual logging, but // the question is how to obtain that in a meaningful way. Stubbing out `libsyslog-sys` would // make the tests mostly pointless, right? Targetting the host's `/dev/log` through `syslog()` // practically leads to nothing but unverifiable side-effects. Running with a custom syslogd // look-a-like, in a chroot or by other conceivable means, would require priviledges not // suitable for unit testing. // // On a more practical level, multiple tests can not run in threads of the same test runner // due to the singleton nature of Syslog, plus the fact that the foreign functions openlog() // and closelog() should really only be called once per process. // // There is https://github.com/rust-lang/rust/issues/47506 which points out this problem, but // the closest to a solution mentioned is a recommendation to use the `rusty-fork` crate. A // crate that seems to need a bit more love than anyone appears to be able to give it. By the // look of things, https://github.com/AltSysrq/rusty-fork/pull/10 is approaching three years // of waiting in a believed-to-be-mergable state.