Skip to content

Commit

Permalink
Add additional tests
Browse files Browse the repository at this point in the history
These tests make sure the signal handler functions as intended.

Signed-off-by: John Nunley <dev@notgull.net>
  • Loading branch information
notgull committed Sep 30, 2023
1 parent c8a7119 commit 1d9a51d
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ concurrent-queue = "2.2.0"

[dev-dependencies]
async-io = "1.12.0"
fastrand = "2.0.1"
futures-lite = "1.12.0"
signal-hook = "0.3.14"
32 changes: 32 additions & 0 deletions tests/sigint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use async_signal::{Signal, Signals};
use futures_lite::{future, prelude::*};

/// Send SIGINT to the current process.
#[cfg(unix)]
fn sigint() {
unsafe {
libc::raise(libc::SIGINT);
}
}

/// Send SIGINT to the current process.
#[cfg(windows)]
fn sigint() {
unsafe {
windows_sys::Win32::System::Console::GenerateConsoleCtrlEvent(
windows_sys::Win32::System::Console::CTRL_C_EVENT,
0,
);
}
}

#[test]
fn test_sigint() {
future::block_on(async {
let mut signals = Signals::new(Some(Signal::Int)).unwrap();
let mut next = signals.next();
assert!(future::poll_once(&mut next).await.is_none());
sigint();
assert_eq!(signals.next().await.unwrap().unwrap(), Signal::Int);
});
}
58 changes: 58 additions & 0 deletions tests/unix_signals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#![cfg(unix)]

use async_signal::{Signal, Signals};
use futures_lite::{future, prelude::*};

#[test]
fn endurance() {
let user_signals = [Signal::Usr1, Signal::Usr2];

future::block_on(async {
let mut signals = Signals::new(user_signals).unwrap();

for &signal in user_signals.iter().cycle().take(100_000) {
let mut next = signals.next();
assert!(future::poll_once(&mut next).await.is_none());
unsafe {
libc::raise(signal as libc::c_int);
}
assert_eq!(
future::poll_once(&mut next)
.await
.unwrap()
.unwrap()
.unwrap(),
signal
);
}
});
}

#[test]
fn distanced() {
let mut signals = Signals::new(Some(Signal::Alarm)).unwrap();

std::thread::spawn(|| {
future::block_on(async {
let mut rng = fastrand::Rng::new();

for _ in 0..1_000 {
unsafe {
libc::raise(Signal::Alarm as libc::c_int);
}

if rng.bool() {
async_io::Timer::after(std::time::Duration::from_millis(rng.u64(1..5))).await;
}
}
});
});

future::block_on(async {
for _ in 0..1_000 {
assert_eq!(signals.next().await.unwrap().unwrap(), Signal::Alarm);
}

assert!(future::poll_once(signals.next()).await.is_none());
});
}

0 comments on commit 1d9a51d

Please sign in to comment.