Add timer tests

This commit is contained in:
Alejandro Cabeza Romero 2024-11-07 05:57:42 +01:00
parent ff2c1b4271
commit 36dc5ef02c
No known key found for this signature in database
GPG Key ID: DA3D14AE478030FD
2 changed files with 112 additions and 0 deletions

View File

@ -54,3 +54,48 @@ impl Stream for Slot {
self.interval.poll_next_unpin(cx) self.interval.poll_next_unpin(cx)
} }
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn counter_interval() {
let waker = futures::task::noop_waker();
let mut cx = futures::task::Context::from_waker(&waker);
let (update_sender, update_receiver) = channel::unbounded();
let mut interval = CounterInterval::new(Duration::from_secs(1), update_receiver);
assert_eq!(interval.poll_next_unpin(&mut cx), Poll::Pending);
update_sender.send(Duration::from_millis(999)).unwrap();
assert_eq!(interval.poll_next_unpin(&mut cx), Poll::Pending);
update_sender.send(Duration::from_millis(1)).unwrap();
assert_eq!(interval.poll_next_unpin(&mut cx), Poll::Ready(Some(0)));
update_sender.send(Duration::from_secs(1)).unwrap();
assert_eq!(interval.poll_next_unpin(&mut cx), Poll::Ready(Some(1)));
update_sender.send(Duration::from_secs(3)).unwrap();
assert_eq!(interval.poll_next_unpin(&mut cx), Poll::Ready(Some(2)));
}
#[test]
fn slot_interval() {
let waker = futures::task::noop_waker();
let mut cx = futures::task::Context::from_waker(&waker);
let (update_sender, update_receiver) = channel::unbounded();
let mut slot = Slot::new(3, Duration::from_secs(1), update_receiver);
assert_eq!(slot.poll_next_unpin(&mut cx), Poll::Pending);
update_sender.send(Duration::from_millis(999)).unwrap();
assert_eq!(slot.poll_next_unpin(&mut cx), Poll::Pending);
update_sender.send(Duration::from_millis(1)).unwrap();
assert_eq!(slot.poll_next_unpin(&mut cx), Poll::Ready(Some(0)));
update_sender.send(Duration::from_secs(1)).unwrap();
assert_eq!(slot.poll_next_unpin(&mut cx), Poll::Ready(Some(1)));
update_sender.send(Duration::from_secs(3)).unwrap();
assert_eq!(slot.poll_next_unpin(&mut cx), Poll::Ready(Some(2)));
update_sender.send(Duration::from_secs(1)).unwrap();
assert_eq!(slot.poll_next_unpin(&mut cx), Poll::Ready(Some(0)));
}
}

View File

@ -92,3 +92,70 @@ impl Stream for TemporalRelease {
Poll::Pending Poll::Pending
} }
} }
#[cfg(test)]
mod tests {
use super::*;
use futures::StreamExt;
use rand_chacha::rand_core::SeedableRng;
#[test]
fn interval_update() {
let (_tx, rx) = channel::unbounded();
let mut interval = Interval::new(Duration::from_secs(2), rx);
assert_eq!(interval.update(Duration::from_secs(0)), false);
assert_eq!(interval.update(Duration::from_secs(1)), false);
assert_eq!(interval.update(Duration::from_secs(1)), true);
assert_eq!(interval.update(Duration::from_secs(3)), false);
}
#[test]
fn interval_polling() {
let waker = futures::task::noop_waker();
let mut cx = futures::task::Context::from_waker(&waker);
let (tx, rx) = channel::unbounded();
let mut interval = Interval::new(Duration::from_secs(2), rx);
assert_eq!(interval.poll_next_unpin(&mut cx), Poll::Pending);
tx.send(Duration::from_secs(1)).unwrap();
assert_eq!(interval.poll_next_unpin(&mut cx), Poll::Pending);
tx.send(Duration::from_secs(1)).unwrap();
assert_eq!(interval.poll_next_unpin(&mut cx), Poll::Ready(Some(())));
tx.send(Duration::from_secs(3)).unwrap();
assert_eq!(interval.poll_next_unpin(&mut cx), Poll::Ready(Some(())));
}
#[test]
fn temporal_release_update() {
let (_tx, rx) = channel::unbounded();
let mut temporal_release =
TemporalRelease::new(rand_chacha::ChaCha8Rng::from_entropy(), rx, (1, 2));
assert_eq!(temporal_release.update(Duration::from_secs(0)), false);
assert_eq!(temporal_release.update(Duration::from_millis(999)), false);
assert_eq!(temporal_release.update(Duration::from_secs(1)), true);
assert_eq!(temporal_release.update(Duration::from_secs(3)), true);
}
#[test]
fn temporal_release_polling() {
let waker = futures::task::noop_waker();
let mut cx = futures::task::Context::from_waker(&waker);
let (tx, rx) = channel::unbounded();
let mut temporal_release =
TemporalRelease::new(rand_chacha::ChaCha8Rng::from_entropy(), rx, (1, 2));
assert_eq!(temporal_release.poll_next_unpin(&mut cx), Poll::Pending);
tx.send(Duration::from_millis(999)).unwrap();
assert_eq!(temporal_release.poll_next_unpin(&mut cx), Poll::Pending);
tx.send(Duration::from_secs(1)).unwrap();
assert_eq!(
temporal_release.poll_next_unpin(&mut cx),
Poll::Ready(Some(()))
);
tx.send(Duration::from_secs(3)).unwrap();
assert_eq!(
temporal_release.poll_next_unpin(&mut cx),
Poll::Ready(Some(()))
);
}
}