Skip to content

Commit

Permalink
add first unit tests for shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
glendc committed Sep 4, 2023
1 parent 7d0404d commit 3193694
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ tracing = "0.1"

[dev-dependencies]
hyper = { version = "0.14", features = [ "server", "tcp", "http1", "http2" ] }
tokio = { version = "1", features = ["net", "rt-multi-thread", "io-util"] }
tokio = { version = "1", features = ["net", "rt-multi-thread", "io-util", "test-util"] }
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ and get a sense on how it works, or at least its flow is.

> [examples/tokio_tcp.rs](https://github.com/plabayo/tokio-graceful/tree/main/examples/tokio_tcp.rs)
>
> ```
> ```bash
> RUST_LOG=trace cargo run --example tokio_tcp
> ```
Expand All @@ -48,7 +48,7 @@ telnet: `telnet 127.0.0.1 8080`. As you are in control of when to exit you can e
> [examples/hyper.rs](https://github.com/plabayo/tokio-graceful/tree/main/examples/hyper.rs)
>
> ```
> ```bash
> RUST_LOG=trace cargo run --example hyper
> ```
Expand Down
36 changes: 36 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,39 @@ mod shutdown;
pub use shutdown::Shutdown;

pub(crate) mod trigger;

#[cfg(test)]
mod tests {
use std::time::Duration;

use tokio::sync::oneshot;

use super::*;

#[tokio::test]
async fn test_shutdown_nope() {
let (tx, rx) = oneshot::channel::<()>();
let shutdown = Shutdown::new(async {
rx.await.unwrap();
});
tokio::spawn(async move {
tx.send(()).unwrap();
});
shutdown.shutdown().await;
}

#[tokio::test]
async fn test_shutdown_nope_limit() {
let (tx, rx) = oneshot::channel::<()>();
let shutdown = Shutdown::new(async {
rx.await.unwrap();
});
tokio::spawn(async move {
tx.send(()).unwrap();
});
shutdown
.shutdown_with_limit(Duration::from_secs(60))
.await
.unwrap();
}
}

0 comments on commit 3193694

Please sign in to comment.