summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Samuelsson <msamuelsson@storvix.eu>2023-05-28 14:09:02 +0200
committerMartin Samuelsson <msamuelsson@storvix.eu>2023-05-28 14:09:02 +0200
commit25f76d406a3517caf84d1a80ae2cf991861fd81c (patch)
treee39d9803d9abf4d73b9302fa8de87a22598dca14
parent622d4c36030538f2760622a2d7b4ac26cfd37991 (diff)
downloadlibsyslog-rs-topic/tests.zip
Add unit tests and repair doc-teststopic/tests
-rw-r--r--libsyslog/src/lib.rs7
-rw-r--r--libsyslog/src/tests.rs35
2 files changed, 41 insertions, 1 deletions
diff --git a/libsyslog/src/lib.rs b/libsyslog/src/lib.rs
index 7c253ae..c154ec5 100644
--- a/libsyslog/src/lib.rs
+++ b/libsyslog/src/lib.rs
@@ -13,7 +13,8 @@
//! Typical use: (A.k.a. recommended snippet to copy'n'paste to the start of
//! `main()` of your daemon.)
//! ```
-//! # fn main() -> Result<()> {
+//! # use log::error;
+//! # fn main() -> Result<(), log::SetLoggerError> {
//! libsyslog::Syslog::builder()
//! .module_level(std::module_path!(), log::LevelFilter::Info)
//! .level(log::LevelFilter::Off)
@@ -21,6 +22,7 @@
//! .init()?;
//!
//! error!("Hello libsyslog. I've failed you.");
+//! # Ok(())
//! # }
//! ```
//!
@@ -57,6 +59,9 @@ mod facility;
mod logopt;
mod syslog;
+#[cfg(test)]
+mod tests;
+
#[cfg(feature = "bitflags")]
pub use logopt::*;
diff --git a/libsyslog/src/tests.rs b/libsyslog/src/tests.rs
new file mode 100644
index 0000000..f3eeca7
--- /dev/null
+++ b/libsyslog/src/tests.rs
@@ -0,0 +1,35 @@
+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 probem, 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.