summaryrefslogtreecommitdiffstats
path: root/third_party/rust/signal-hook-registry/tests/unregister_signal.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/signal-hook-registry/tests/unregister_signal.rs
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/signal-hook-registry/tests/unregister_signal.rs')
-rw-r--r--third_party/rust/signal-hook-registry/tests/unregister_signal.rs59
1 files changed, 59 insertions, 0 deletions
diff --git a/third_party/rust/signal-hook-registry/tests/unregister_signal.rs b/third_party/rust/signal-hook-registry/tests/unregister_signal.rs
new file mode 100644
index 0000000000..6e13c08584
--- /dev/null
+++ b/third_party/rust/signal-hook-registry/tests/unregister_signal.rs
@@ -0,0 +1,59 @@
+//! Tests for the [unregister_signal] function.
+//!
+//! As a separate integration level test, so it doesn't clash with other tests on the signals.
+
+// The unregister_signal itself is deprecated. But we still want to test it, so it's not deprecated
+// and broken at the same time.
+#![allow(deprecated)]
+
+extern crate libc;
+extern crate signal_hook_registry;
+
+use std::sync::atomic::{AtomicUsize, Ordering};
+use std::sync::Arc;
+
+use libc::{SIGINT, SIGTERM}; // We'll use these here because SIGUSR1 is not available on Windows.
+use signal_hook_registry::{register, unregister_signal};
+
+#[test]
+fn register_unregister() {
+ let called = Arc::new(AtomicUsize::new(0));
+
+ let hook = {
+ let called = Arc::clone(&called);
+ move || {
+ called.fetch_add(1, Ordering::Relaxed);
+ }
+ };
+
+ unsafe {
+ register(SIGTERM, hook.clone()).unwrap();
+ register(SIGTERM, hook.clone()).unwrap();
+ register(SIGINT, hook.clone()).unwrap();
+
+ libc::raise(SIGTERM);
+ }
+
+ // The closure is run twice.
+ assert_eq!(2, called.load(Ordering::Relaxed));
+
+ assert!(unregister_signal(SIGTERM));
+
+ unsafe { libc::raise(SIGTERM) };
+ // Second one unregisters nothing.
+ assert!(!unregister_signal(SIGTERM));
+
+ // After unregistering (both), it is no longer run at all.
+ assert_eq!(2, called.load(Ordering::Relaxed));
+
+ // The SIGINT one is not disturbed.
+ unsafe { libc::raise(SIGINT) };
+ assert_eq!(3, called.load(Ordering::Relaxed));
+
+ // But it's possible to register it again just fine.
+ unsafe {
+ register(SIGTERM, hook).unwrap();
+ libc::raise(SIGTERM);
+ }
+ assert_eq!(4, called.load(Ordering::Relaxed));
+}