//! # Drain
use atap::{RuntimeError, TaskHandle};
use std::{
thread,
time::{Duration, Instant},
};
/// Drains a bounded series, counting what it published
pub fn drain(handle: &TaskHandle<Duration>, patience: Duration) -> usize {
let deadline = Instant::now() + patience;
let mut seen = 0;
while Instant::now() < deadline {
match handle.try_take() {
Ok(_) => seen += 1,
// Between runs, or one still going
Err(RuntimeError::AlreadyTaken) | Err(RuntimeError::NotReady) => {
thread::sleep(Duration::from_millis(1))
}
// `Finished` and every other error are endings
Err(_) => break,
}
}
seen
}