Implement async-std 1.0 support

The implementation provides a wrapper using a async-std UnixStream as
the self pipe. The wrapper acts as an asynchronous stream of signal
numbers.
This commit is contained in:
Thomas Himmelstoss
2020-11-11 19:05:05 +01:00
parent b34f2bb326
commit 241632d6f6
9 changed files with 631 additions and 10 deletions
Generated
+388 -8
View File
@@ -1,5 +1,122 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "async-attributes"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "efd3d156917d94862e779f356c5acae312b08fd3121e792c857d7928c8088423"
dependencies = [
"quote",
"syn",
]
[[package]]
name = "async-channel"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59740d83946db6a5af71ae25ddf9562c2b176b2ca42cf99a455f09f4a220d6b9"
dependencies = [
"concurrent-queue",
"event-listener",
"futures-core",
]
[[package]]
name = "async-executor"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb877970c7b440ead138f6321a3b5395d6061183af779340b65e20c0fede9146"
dependencies = [
"async-task",
"concurrent-queue",
"fastrand",
"futures-lite",
"once_cell",
"vec-arena",
]
[[package]]
name = "async-global-executor"
version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73079b49cd26b8fd5a15f68fc7707fc78698dc2a3d61430f2a7a9430230dfa04"
dependencies = [
"async-executor",
"async-io",
"futures-lite",
"num_cpus",
"once_cell",
]
[[package]]
name = "async-io"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40a0b2bb8ae20fede194e779150fe283f65a4a08461b496de546ec366b174ad9"
dependencies = [
"concurrent-queue",
"fastrand",
"futures-lite",
"libc",
"log",
"nb-connect",
"once_cell",
"parking",
"polling",
"vec-arena",
"waker-fn",
"winapi 0.3.9",
]
[[package]]
name = "async-mutex"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "479db852db25d9dbf6204e6cb6253698f175c15726470f78af0d918e99d6156e"
dependencies = [
"event-listener",
]
[[package]]
name = "async-std"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7e82538bc65a25dbdff70e4c5439d52f068048ab97cdea0acd73f131594caa1"
dependencies = [
"async-attributes",
"async-global-executor",
"async-io",
"async-mutex",
"blocking",
"crossbeam-utils 0.8.0",
"futures-channel",
"futures-core",
"futures-io",
"futures-lite",
"gloo-timers",
"kv-log-macro",
"log",
"memchr",
"num_cpus",
"once_cell",
"pin-project-lite 0.1.11",
"pin-utils",
"slab",
"wasm-bindgen-futures",
]
[[package]]
name = "async-task"
version = "4.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e91831deabf0d6d7ec49552e489aed63b7456a7a3c46cff62adad428110b0af0"
[[package]]
name = "atomic-waker"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "065374052e7df7ee4047b1160cca5e1467a12351a40b3da123c870ba0b8eda2a"
[[package]]
name = "autocfg"
version = "1.0.1"
@@ -12,6 +129,26 @@ version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "blocking"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c5e170dbede1f740736619b776d7251cb1b9095c435c34d8ca9f57fcd2f335e9"
dependencies = [
"async-channel",
"async-task",
"atomic-waker",
"fastrand",
"futures-lite",
"once_cell",
]
[[package]]
name = "bumpalo"
version = "3.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e8c087f005730276d1096a652e92a8bacee2e2472bcc9715a74d2bec38b5820"
[[package]]
name = "byteorder"
version = "1.3.4"
@@ -34,6 +171,18 @@ version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0dcbc35f504eb6fc275a6d20e4ebcda18cf50d40ba6fabff8c711fa16cb3b16"
[[package]]
name = "cache-padded"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "631ae5198c9be5e753e5cc215e1bd73c2b466a3565173db433f52bb9d3e66dba"
[[package]]
name = "cc"
version = "1.0.65"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95752358c8f7552394baf48cd82695b345628ad3f170d607de3ca03b8dacca15"
[[package]]
name = "cfg-if"
version = "0.1.10"
@@ -64,6 +213,21 @@ dependencies = [
"bitflags",
]
[[package]]
name = "concurrent-queue"
version = "1.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30ed07550be01594c6026cff2a1d7fe9c8f683caa798e12b68694ac9e88286a3"
dependencies = [
"cache-padded",
]
[[package]]
name = "const_fn"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c478836e029dcef17fb47c89023448c64f781a046e0300e257ad8225ae59afab"
[[package]]
name = "crossbeam-deque"
version = "0.7.3"
@@ -71,7 +235,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f02af974daeee82218205558e51ec8768b48cf524bd01d550abe5573a608285"
dependencies = [
"crossbeam-epoch",
"crossbeam-utils",
"crossbeam-utils 0.7.2",
"maybe-uninit",
]
@@ -83,7 +247,7 @@ checksum = "058ed274caafc1f60c4997b5fc07bf7dc7cca454af7c6e81edffe5f33f70dace"
dependencies = [
"autocfg",
"cfg-if 0.1.10",
"crossbeam-utils",
"crossbeam-utils 0.7.2",
"lazy_static",
"maybe-uninit",
"memoffset",
@@ -97,7 +261,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "774ba60a54c213d409d5353bda12d49cd68d14e45036a285234c8d6f91f92570"
dependencies = [
"cfg-if 0.1.10",
"crossbeam-utils",
"crossbeam-utils 0.7.2",
"maybe-uninit",
]
@@ -112,6 +276,33 @@ dependencies = [
"lazy_static",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec91540d98355f690a86367e566ecad2e9e579f230230eb7c21398372be73ea5"
dependencies = [
"autocfg",
"cfg-if 1.0.0",
"const_fn",
"lazy_static",
]
[[package]]
name = "event-listener"
version = "2.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7531096570974c3a9dcf9e4b8e1cede1ec26cf5046219fb3b9d897503b9be59"
[[package]]
name = "fastrand"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca5faf057445ce5c9d4329e382b2ce7ca38550ef3b73a5348362d5f24e0c7fe3"
dependencies = [
"instant",
]
[[package]]
name = "fnv"
version = "1.0.7"
@@ -188,6 +379,21 @@ version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "611834ce18aaa1bd13c4b374f5d653e1027cf99b6b502584ff8c9a64413b30bb"
[[package]]
name = "futures-lite"
version = "1.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e6c079abfac3ab269e2927ec048dabc89d009ebfdda6b8ee86624f30c689658"
dependencies = [
"fastrand",
"futures-core",
"futures-io",
"memchr",
"parking",
"pin-project-lite 0.1.11",
"waker-fn",
]
[[package]]
name = "futures-macro"
version = "0.3.8"
@@ -235,6 +441,19 @@ dependencies = [
"slab",
]
[[package]]
name = "gloo-timers"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47204a46aaff920a1ea58b11d03dec6f704287d27561724a4631e450654a891f"
dependencies = [
"futures-channel",
"futures-core",
"js-sys",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "hermit-abi"
version = "0.1.17"
@@ -262,6 +481,15 @@ dependencies = [
"libc",
]
[[package]]
name = "js-sys"
version = "0.3.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca059e81d9486668f12d455a4ea6daa600bd408134cd17e3d3fb5a32d1f016f8"
dependencies = [
"wasm-bindgen",
]
[[package]]
name = "kernel32-sys"
version = "0.2.2"
@@ -272,6 +500,15 @@ dependencies = [
"winapi-build",
]
[[package]]
name = "kv-log-macro"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f"
dependencies = [
"log",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
@@ -397,6 +634,16 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "nb-connect"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8123a81538e457d44b933a02faf885d3fe8408806b23fa700e8f01c6c3a98998"
dependencies = [
"libc",
"winapi 0.3.9",
]
[[package]]
name = "net2"
version = "0.2.35"
@@ -433,6 +680,12 @@ version = "1.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13bd41f508810a131401606d54ac32a467c97172d74ba7662562ebba5ad07fa0"
[[package]]
name = "parking"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "427c3892f9e783d91cc128285287e70a59e206ca452770ece88a76f7a3eddd72"
[[package]]
name = "parking_lot"
version = "0.9.0"
@@ -529,6 +782,12 @@ dependencies = [
"syn",
]
[[package]]
name = "pin-project-lite"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c917123afa01924fc84bb20c4c03f004d9c38e5127e3c039bbf7f4b9c76a2f6b"
[[package]]
name = "pin-project-lite"
version = "0.2.0"
@@ -541,6 +800,19 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "polling"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2a7bc6b2a29e632e45451c941832803a18cce6781db04de8a04696cdca8bde4"
dependencies = [
"cfg-if 0.1.10",
"libc",
"log",
"wepoll-sys",
"winapi 0.3.9",
]
[[package]]
name = "proc-macro-hack"
version = "0.5.19"
@@ -638,6 +910,17 @@ dependencies = [
"signal-hook-registry 1.2.2",
]
[[package]]
name = "signal-hook-async-std"
version = "0.1.0"
dependencies = [
"async-std",
"futures 0.3.8",
"libc",
"serial_test",
"signal-hook",
]
[[package]]
name = "signal-hook-mio"
version = "0.1.0"
@@ -763,7 +1046,7 @@ dependencies = [
"mio 0.7.6",
"num_cpus",
"parking_lot 0.11.1",
"pin-project-lite",
"pin-project-lite 0.2.0",
"signal-hook-registry 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"slab",
"tokio-macros",
@@ -797,7 +1080,7 @@ version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb2d1b8f4548dbf5e1f7818512e9c406860678f29c300cdf0ebac72d1a3a1671"
dependencies = [
"crossbeam-utils",
"crossbeam-utils 0.7.2",
"futures 0.1.30",
]
@@ -840,7 +1123,7 @@ version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09bc590ec4ba8ba87652da2068d150dcada2cfa2e07faae270a5e0409aa51351"
dependencies = [
"crossbeam-utils",
"crossbeam-utils 0.7.2",
"futures 0.1.30",
"lazy_static",
"log",
@@ -885,7 +1168,7 @@ checksum = "df720b6581784c118f0eb4310796b12b1d242a7eb95f716a8367855325c25f89"
dependencies = [
"crossbeam-deque",
"crossbeam-queue",
"crossbeam-utils",
"crossbeam-utils 0.7.2",
"futures 0.1.30",
"lazy_static",
"log",
@@ -900,7 +1183,7 @@ version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93044f2d313c95ff1cb7809ce9a7a05735b012288a888b62d4434fd58c94f296"
dependencies = [
"crossbeam-utils",
"crossbeam-utils 0.7.2",
"futures 0.1.30",
"slab",
"tokio-executor",
@@ -945,6 +1228,103 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
[[package]]
name = "vec-arena"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eafc1b9b2dfc6f5529177b62cf806484db55b32dc7c9658a118e11bbeb33061d"
[[package]]
name = "waker-fn"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca"
[[package]]
name = "wasm-bindgen"
version = "0.2.68"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ac64ead5ea5f05873d7c12b545865ca2b8d28adfc50a49b84770a3a97265d42"
dependencies = [
"cfg-if 0.1.10",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.68"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f22b422e2a757c35a73774860af8e112bff612ce6cb604224e8e47641a9e4f68"
dependencies = [
"bumpalo",
"lazy_static",
"log",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-futures"
version = "0.4.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7866cab0aa01de1edf8b5d7936938a7e397ee50ce24119aef3e1eaa3b6171da"
dependencies = [
"cfg-if 0.1.10",
"js-sys",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.68"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b13312a745c08c469f0b292dd2fcd6411dba5f7160f593da6ef69b64e407038"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.68"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f249f06ef7ee334cc3b8ff031bfc11ec99d00f34d86da7498396dc1e3b1498fe"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.68"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d649a3145108d7d3fbcde896a468d1bd636791823c9921135218ad89be08307"
[[package]]
name = "web-sys"
version = "0.3.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bf6ef87ad7ae8008e15a355ce696bed26012b7caa21605188cfd8214ab51e2d"
dependencies = [
"js-sys",
"wasm-bindgen",
]
[[package]]
name = "wepoll-sys"
version = "3.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fcb14dea929042224824779fbc82d9fab8d2e6d3cbc0ac404de8edf489e77ff"
dependencies = [
"cc",
]
[[package]]
name = "winapi"
version = "0.2.8"
+1
View File
@@ -22,6 +22,7 @@ members = [
"signal-hook-registry",
"signal-hook-tokio",
"signal-hook-mio",
"signal-hook-async-std",
]
[dependencies]
+2 -2
View File
@@ -16,7 +16,7 @@ if [ "$RUST_VERSION" = 1.26.0 ] ; then
fi
rm -f Cargo.lock
cargo build --all
cargo build --all --exclude signal-hook-async-std
if [ "$RUST_VERSION" = 1.31.0 ] ; then
exit
@@ -26,7 +26,7 @@ if [ "$OS" = "windows-latest" ] ; then
# The async support crates rely on the iterator module
# which isn't available for windows. So exclude them
# from the build.
EXCLUDE_FROM_BUILD="--exclude signal-hook-mio --exclude signal-hook-tokio"
EXCLUDE_FROM_BUILD="--exclude signal-hook-mio --exclude signal-hook-tokio --exclude signal-hook-async-std"
else
EXCLUDE_FROM_BUILD=""
fi
+33
View File
@@ -0,0 +1,33 @@
[package]
name = "signal-hook-async-std"
version = "0.1.0"
authors = [
"Michal 'vorner' Vaner <vorner@vorner.cz>",
"Thomas Himmelstoss <thimm@posteo.de>",
]
description = "async-std support for signal-hook"
documentation = "https://docs.rs/signal-hook-async-std"
readme = "README.md"
repository = "https://github.com/vorner/signal-hook"
keywords = ["signal", "unix", "async-std"]
license = "Apache-2.0/MIT"
edition = "2018"
[badges]
travis-ci = { repository = "vorner/signal-hook" }
maintenance = { status = "actively-developed" }
[dependencies]
libc = "~0.2"
async-std = "~1"
futures = "~0.3"
signal-hook = { version = "~0.2", path = ".." }
[dev-dependencies]
async-std = { version = "~1", features = ["attributes"] }
serial_test = "~0.5"
[package.metadata.docs.rs]
all-features = true
+1
View File
@@ -0,0 +1 @@
../LICENSE-APACHE
+1
View File
@@ -0,0 +1 @@
../LICENSE-MIT
+23
View File
@@ -0,0 +1,23 @@
# Signal-hook-async-std
[![Travis Build Status](https://api.travis-ci.org/vorner/signal-hook.svg?branch=master)](https://travis-ci.org/vorner/signal-hook)
This is a async-std adapter crate for the
[signal-hook](https://crates.io/crates/signal-hook) crate. See the
[documentation](https://docs.rs/signal-hook-async-std) for further details.
## License
Licensed under either of
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your option.
### Contribution
Unless you explicitly state otherwise, any contribution intentionally
submitted for inclusion in the work by you, as defined in the Apache-2.0
license, shall be dual licensed as above, without any additional terms
or conditions.
+126
View File
@@ -0,0 +1,126 @@
#![doc(test(attr(deny(warnings))))]
#![warn(missing_docs)]
#![cfg_attr(docsrs, feature(doc_cfg))]
//! A module for integrating signal handling with the async-std runtime.
//!
//! This provides the [`Signals`] struct which acts as a
//! [`Stream`] of signals.
//!
//! # Example
//!
//! ```rust
//! use std::io::Error;
//!
//! use async_std;
//!
//! use futures::stream::StreamExt;
//!
//! use signal_hook;
//! use signal_hook_async_std::Signals;
//!
//! async fn handle_signals(signals: Signals) {
//! let mut signals = signals.fuse();
//! while let Some(signal) = signals.next().await {
//! match signal {
//! signal_hook::SIGHUP => {
//! // Reload configuration
//! // Reopen the log file
//! }
//! signal_hook::SIGTERM | signal_hook::SIGINT | signal_hook::SIGQUIT => {
//! // Shutdown the system;
//! },
//! _ => unreachable!(),
//! }
//! }
//! }
//!
//! #[async_std::main]
//! async fn main() -> Result<(), Error> {
//! let signals = Signals::new(&[
//! signal_hook::SIGHUP,
//! signal_hook::SIGTERM,
//! signal_hook::SIGINT,
//! signal_hook::SIGQUIT,
//! ])?;
//! let handle = signals.handle();
//!
//! let signals_task = async_std::task::spawn(handle_signals(signals));
//!
//! // Execute your main program logic
//!
//! // Terminate the signal stream.
//! handle.close();
//! signals_task.await;
//!
//! Ok(())
//! }
//! ```
use std::borrow::Borrow;
use std::io::Error;
use std::pin::Pin;
use libc::c_int;
pub use signal_hook::iterator::backend::Handle;
use signal_hook::iterator::backend::{PollResult, SignalDelivery, SignalIterator};
use async_std::os::unix::net::UnixStream;
use futures::stream::Stream;
use futures::task::{Context, Poll};
use futures::AsyncRead;
/// An asynchronous [`Stream`] of arriving signals.
///
/// The stream doesn't return the signals in the order they were recieved by
/// the process and may merge signals received multiple times.
pub struct Signals(SignalIterator<UnixStream>);
impl Signals {
/// Create a `Signals` instance.
///
/// This registers all the signals listed. The same restrictions (panics, errors) apply
/// as with [`Handle::add_signal`].
pub fn new<I, S>(signals: I) -> Result<Self, Error>
where
I: IntoIterator<Item = S>,
S: Borrow<c_int>,
{
let (read, write) = UnixStream::pair()?;
let inner = SignalDelivery::with_pipe(read, write, signals)?;
Ok(Self(SignalIterator::new(inner)))
}
/// Get a shareable [`Handle`] for this `Signals` instance.
///
/// This can be used to add further signals or close the [`Signals`] instance
/// which terminates the whole signal stream.
pub fn handle(&self) -> Handle {
self.0.handle()
}
}
impl Signals {
fn has_signals(read: &mut UnixStream, ctx: &mut Context<'_>) -> Result<bool, Error> {
match Pin::new(read).poll_read(ctx, &mut [0u8]) {
Poll::Pending => Ok(false),
Poll::Ready(Ok(num_read)) => Ok(num_read > 0),
Poll::Ready(Err(error)) => Err(error),
}
}
}
impl Stream for Signals {
type Item = c_int;
fn poll_next(mut self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
match self.0.poll_signal(&mut |read| Self::has_signals(read, ctx)) {
PollResult::Signal(sig) => Poll::Ready(Some(sig)),
PollResult::Closed => Poll::Ready(None),
PollResult::Pending => Poll::Pending,
PollResult::Err(error) => panic!("Unexpected error: {}", error),
}
}
}
+56
View File
@@ -0,0 +1,56 @@
use futures::stream::StreamExt;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;
use signal_hook_async_std::Signals;
use serial_test::serial;
fn send_sig(sig: libc::c_int) {
unsafe { libc::raise(sig) };
}
#[async_std::test]
#[serial]
async fn next_returns_recieved_signal() {
let mut signals = Signals::new(&[signal_hook::SIGUSR1]).unwrap();
send_sig(signal_hook::SIGUSR1);
let signal = signals.next().await;
assert_eq!(signal, Some(signal_hook::SIGUSR1));
}
#[async_std::test]
#[serial]
async fn close_signal_stream() {
let mut signals = Signals::new(&[signal_hook::SIGUSR1]).unwrap();
signals.handle().close();
let result = signals.next().await;
assert_eq!(result, None);
}
#[async_std::test]
#[serial]
async fn delayed() {
async fn get_signal(mut signals: Signals, recieved: Arc<AtomicBool>) {
signals.next().await;
recieved.store(true, Ordering::SeqCst);
}
let signals = Signals::new(&[signal_hook::SIGUSR1]).unwrap();
let recieved = Arc::new(AtomicBool::new(false));
let signals_task = async_std::task::spawn(get_signal(signals, Arc::clone(&recieved)));
async_std::task::sleep(Duration::from_millis(100)).await;
assert_eq!(recieved.load(Ordering::SeqCst), false);
send_sig(signal_hook::SIGUSR1);
signals_task.await;
assert_eq!(recieved.load(Ordering::SeqCst), true);
}