From abed699d1ac1a1bfbd95669b025eb55cc9afa7df Mon Sep 17 00:00:00 2001 From: Youngjoon Lee <5462944+youngjoon-lee@users.noreply.github.com> Date: Fri, 8 Nov 2024 20:00:56 +0700 Subject: [PATCH] wip --- .../src/node/mix/consensus_streams.rs | 16 +++++++++------- simlib/mixnet-sims/src/node/mix/scheduler.rs | 9 +++++++-- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/simlib/mixnet-sims/src/node/mix/consensus_streams.rs b/simlib/mixnet-sims/src/node/mix/consensus_streams.rs index 33c52f7..09d1f0b 100644 --- a/simlib/mixnet-sims/src/node/mix/consensus_streams.rs +++ b/simlib/mixnet-sims/src/node/mix/consensus_streams.rs @@ -67,16 +67,17 @@ mod tests { 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::Ready(Some(0))); update_sender.send(Duration::from_secs(0)).unwrap(); 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(); + update_sender.send(Duration::from_secs(1)).unwrap(); 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] @@ -87,17 +88,18 @@ mod tests { 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::Ready(Some(0))); update_sender.send(Duration::from_secs(0)).unwrap(); 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(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))); 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))); } } diff --git a/simlib/mixnet-sims/src/node/mix/scheduler.rs b/simlib/mixnet-sims/src/node/mix/scheduler.rs index aef1c57..6865e4b 100644 --- a/simlib/mixnet-sims/src/node/mix/scheduler.rs +++ b/simlib/mixnet-sims/src/node/mix/scheduler.rs @@ -15,7 +15,7 @@ impl Interval { pub fn new(duration: Duration, update_time: channel::Receiver) -> Self { Self { duration, - current_elapsed: Duration::from_secs(0), + current_elapsed: duration, // to immediately release at the interval 0 update_time, } } @@ -104,7 +104,7 @@ mod tests { let (_tx, rx) = channel::unbounded(); 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(3))); @@ -118,6 +118,7 @@ mod tests { let (tx, rx) = channel::unbounded(); let mut interval = Interval::new(Duration::from_secs(2), rx); + assert_eq!(interval.poll_next_unpin(&mut cx), Poll::Ready(Some(()))); tx.send(Duration::from_secs(0)).unwrap(); assert_eq!(interval.poll_next_unpin(&mut cx), Poll::Pending); tx.send(Duration::from_secs(1)).unwrap(); @@ -149,6 +150,10 @@ mod tests { let mut temporal_release = TemporalRelease::new(rand_chacha::ChaCha8Rng::from_entropy(), rx, (1, 2)); + assert_eq!( + temporal_release.poll_next_unpin(&mut cx), + Poll::Ready(Some(())) + ); tx.send(Duration::from_secs(0)).unwrap(); assert_eq!(temporal_release.poll_next_unpin(&mut cx), Poll::Pending); tx.send(Duration::from_millis(999)).unwrap();