diff --git a/simlib/mixnet-sims/src/node/mix/consensus_streams.rs b/simlib/mixnet-sims/src/node/mix/consensus_streams.rs index 8f534a7..eb764f3 100644 --- a/simlib/mixnet-sims/src/node/mix/consensus_streams.rs +++ b/simlib/mixnet-sims/src/node/mix/consensus_streams.rs @@ -54,3 +54,48 @@ impl Stream for Slot { 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))); + } +} diff --git a/simlib/mixnet-sims/src/node/mix/scheduler.rs b/simlib/mixnet-sims/src/node/mix/scheduler.rs index 75027eb..a33df0d 100644 --- a/simlib/mixnet-sims/src/node/mix/scheduler.rs +++ b/simlib/mixnet-sims/src/node/mix/scheduler.rs @@ -92,3 +92,70 @@ impl Stream for TemporalRelease { 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(())) + ); + } +}