-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
These tests make sure the signal handler functions as intended. Signed-off-by: John Nunley <dev@notgull.net>
- Loading branch information
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}); | ||
} |