use std::future::pending;
use super::*;
use crate::test_runtime;
#[test]
fn test_single_pair() {
let (tx, rx) = new_pair();
let handle = test_runtime().spawn(async move { rx.is_shutdown().await });
tx.shutdown();
pollster::block_on(tx.await_shutdown());
pollster::block_on(handle).unwrap();
}
#[test]
fn test_multiple_tasks() {
let (tx, rx) = new_pair();
for _i in 0..100 {
let rx = rx.clone();
test_runtime().spawn(async move { rx.is_shutdown().await });
}
drop(rx);
tx.shutdown();
pollster::block_on(tx.await_shutdown());
}
#[test]
fn test_multiple_senders() {
let (tx, rx) = new_pair();
for _i in 0..100 {
let rx = rx.clone();
test_runtime().spawn(async move { rx.is_shutdown().await });
}
drop(rx);
let tx_clone = tx.clone();
tx.shutdown();
pollster::block_on(tx.await_shutdown());
pollster::block_on(tx_clone.await_shutdown());
}
#[test]
fn test_is_shutdown_now() {
let (tx, rx) = new_pair();
assert!(!rx.is_shutdown_now());
tx.shutdown();
assert!(rx.is_shutdown_now());
}
#[test]
fn test_is_shutdown_owned_not_capture_self() {
struct State {
rx: ShutdownRecv,
}
async fn run_state(_state: &mut State) {
pending::<()>().await;
}
let (tx, rx) = new_pair();
let mut state = State { rx };
test_runtime().spawn(async move {
let is_shutdown = state.rx.is_shutdown_owned();
tokio::select! {
_ = is_shutdown => (),
_ = run_state(&mut state) => (),
}
});
tx.shutdown();
pollster::block_on(tx.await_shutdown());
}