Make `Interval` immediately release at the first interval (#44)

* wip

* fix tests
This commit is contained in:
Youngjoon Lee 2024-11-09 09:35:54 +07:00 committed by GitHub
parent f97136bda6
commit fbb37a4317
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 11 deletions

View File

@ -67,16 +67,18 @@ mod tests {
let (update_sender, update_receiver) = channel::unbounded(); let (update_sender, update_receiver) = channel::unbounded();
let mut interval = CounterInterval::new(Duration::from_secs(1), update_receiver); let mut interval = CounterInterval::new(Duration::from_secs(1), update_receiver);
update_sender.send(Duration::from_secs(0)).unwrap();
assert_eq!(interval.poll_next_unpin(&mut cx), Poll::Ready(Some(0)));
update_sender.send(Duration::from_secs(0)).unwrap(); update_sender.send(Duration::from_secs(0)).unwrap();
assert_eq!(interval.poll_next_unpin(&mut cx), Poll::Pending); assert_eq!(interval.poll_next_unpin(&mut cx), Poll::Pending);
update_sender.send(Duration::from_millis(999)).unwrap(); update_sender.send(Duration::from_millis(999)).unwrap();
assert_eq!(interval.poll_next_unpin(&mut cx), Poll::Pending); assert_eq!(interval.poll_next_unpin(&mut cx), Poll::Pending);
update_sender.send(Duration::from_millis(1)).unwrap(); 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))); assert_eq!(interval.poll_next_unpin(&mut cx), Poll::Ready(Some(1)));
update_sender.send(Duration::from_secs(3)).unwrap(); update_sender.send(Duration::from_secs(1)).unwrap();
assert_eq!(interval.poll_next_unpin(&mut cx), Poll::Ready(Some(2))); assert_eq!(interval.poll_next_unpin(&mut cx), Poll::Ready(Some(2)));
update_sender.send(Duration::from_secs(3)).unwrap();
assert_eq!(interval.poll_next_unpin(&mut cx), Poll::Ready(Some(3)));
} }
#[test] #[test]
@ -87,17 +89,19 @@ mod tests {
let (update_sender, update_receiver) = channel::unbounded(); let (update_sender, update_receiver) = channel::unbounded();
let mut slot = Slot::new(3, Duration::from_secs(1), update_receiver); let mut slot = Slot::new(3, Duration::from_secs(1), update_receiver);
update_sender.send(Duration::from_secs(0)).unwrap();
assert_eq!(slot.poll_next_unpin(&mut cx), Poll::Ready(Some(0)));
update_sender.send(Duration::from_secs(0)).unwrap(); update_sender.send(Duration::from_secs(0)).unwrap();
assert_eq!(slot.poll_next_unpin(&mut cx), Poll::Pending); assert_eq!(slot.poll_next_unpin(&mut cx), Poll::Pending);
update_sender.send(Duration::from_millis(999)).unwrap(); update_sender.send(Duration::from_millis(999)).unwrap();
assert_eq!(slot.poll_next_unpin(&mut cx), Poll::Pending); assert_eq!(slot.poll_next_unpin(&mut cx), Poll::Pending);
update_sender.send(Duration::from_millis(1)).unwrap(); update_sender.send(Duration::from_millis(1)).unwrap();
assert_eq!(slot.poll_next_unpin(&mut cx), Poll::Ready(Some(1)));
update_sender.send(Duration::from_secs(1)).unwrap();
assert_eq!(slot.poll_next_unpin(&mut cx), Poll::Ready(Some(2)));
update_sender.send(Duration::from_secs(3)).unwrap();
assert_eq!(slot.poll_next_unpin(&mut cx), Poll::Ready(Some(0))); assert_eq!(slot.poll_next_unpin(&mut cx), Poll::Ready(Some(0)));
update_sender.send(Duration::from_secs(1)).unwrap(); update_sender.send(Duration::from_secs(1)).unwrap();
assert_eq!(slot.poll_next_unpin(&mut cx), Poll::Ready(Some(1))); 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

@ -15,7 +15,7 @@ impl Interval {
pub fn new(duration: Duration, update_time: channel::Receiver<Duration>) -> Self { pub fn new(duration: Duration, update_time: channel::Receiver<Duration>) -> Self {
Self { Self {
duration, duration,
current_elapsed: Duration::from_secs(0), current_elapsed: duration, // to immediately release at the interval 0
update_time, update_time,
} }
} }
@ -104,7 +104,7 @@ mod tests {
let (_tx, rx) = channel::unbounded(); let (_tx, rx) = channel::unbounded();
let mut interval = Interval::new(Duration::from_secs(2), rx); let mut interval = Interval::new(Duration::from_secs(2), rx);
assert!(!interval.update(Duration::from_secs(0))); assert!(interval.update(Duration::from_secs(0)));
assert!(!interval.update(Duration::from_secs(1))); assert!(!interval.update(Duration::from_secs(1)));
assert!(interval.update(Duration::from_secs(1))); assert!(interval.update(Duration::from_secs(1)));
assert!(interval.update(Duration::from_secs(3))); assert!(interval.update(Duration::from_secs(3)));
@ -118,6 +118,8 @@ mod tests {
let (tx, rx) = channel::unbounded(); let (tx, rx) = channel::unbounded();
let mut interval = Interval::new(Duration::from_secs(2), rx); let mut interval = Interval::new(Duration::from_secs(2), rx);
tx.send(Duration::from_secs(0)).unwrap();
assert_eq!(interval.poll_next_unpin(&mut cx), Poll::Ready(Some(())));
tx.send(Duration::from_secs(0)).unwrap(); tx.send(Duration::from_secs(0)).unwrap();
assert_eq!(interval.poll_next_unpin(&mut cx), Poll::Pending); assert_eq!(interval.poll_next_unpin(&mut cx), Poll::Pending);
tx.send(Duration::from_secs(1)).unwrap(); tx.send(Duration::from_secs(1)).unwrap();
@ -132,7 +134,7 @@ mod tests {
fn temporal_release_update() { fn temporal_release_update() {
let (_tx, rx) = channel::unbounded(); let (_tx, rx) = channel::unbounded();
let mut temporal_release = let mut temporal_release =
TemporalRelease::new(rand_chacha::ChaCha8Rng::from_entropy(), rx, (1, 2)); TemporalRelease::new(rand_chacha::ChaCha8Rng::from_entropy(), rx, (1, 1));
assert!(!temporal_release.update(Duration::from_secs(0))); assert!(!temporal_release.update(Duration::from_secs(0)));
assert!(!temporal_release.update(Duration::from_millis(999))); assert!(!temporal_release.update(Duration::from_millis(999)));
@ -147,7 +149,7 @@ mod tests {
let (tx, rx) = channel::unbounded(); let (tx, rx) = channel::unbounded();
let mut temporal_release = let mut temporal_release =
TemporalRelease::new(rand_chacha::ChaCha8Rng::from_entropy(), rx, (1, 2)); TemporalRelease::new(rand_chacha::ChaCha8Rng::from_entropy(), rx, (1, 1));
tx.send(Duration::from_secs(0)).unwrap(); tx.send(Duration::from_secs(0)).unwrap();
assert_eq!(temporal_release.poll_next_unpin(&mut cx), Poll::Pending); assert_eq!(temporal_release.poll_next_unpin(&mut cx), Poll::Pending);