From 8faafcfb6f6c94667b2aa9c4e1d56259437238c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lex?= Date: Fri, 8 Nov 2024 06:25:27 +0100 Subject: [PATCH] Add tests to timer streams and interval (#29) * Add timer tests * Add tests for timers and intervals. --------- Co-authored-by: Daniel Sanchez <3danimanimal@gmail.com> --- .gitignore | 5 +- .../src/node/mix/consensus_streams.rs | 47 ++++++++++++ simlib/mixnet-sims/src/node/mix/scheduler.rs | 73 +++++++++++++++++++ 3 files changed, 124 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 7265eeb..03f3409 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,10 @@ __pycache__/ *$py.class *.so simulation -simlib/**/target +simlib/netrunner/target .idea/ +target/ +Cargo.lock +simlib/**/target simlib/**/Cargo.lock simlib/test.json diff --git a/simlib/mixnet-sims/src/node/mix/consensus_streams.rs b/simlib/mixnet-sims/src/node/mix/consensus_streams.rs index b31a41a..33c52f7 100644 --- a/simlib/mixnet-sims/src/node/mix/consensus_streams.rs +++ b/simlib/mixnet-sims/src/node/mix/consensus_streams.rs @@ -54,3 +54,50 @@ 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 = Context::from_waker(&waker); + + let (update_sender, update_receiver) = channel::unbounded(); + 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::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 = Context::from_waker(&waker); + + let (update_sender, update_receiver) = channel::unbounded(); + 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::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..aef1c57 100644 --- a/simlib/mixnet-sims/src/node/mix/scheduler.rs +++ b/simlib/mixnet-sims/src/node/mix/scheduler.rs @@ -92,3 +92,76 @@ 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!(!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))); + } + + #[test] + fn interval_polling() { + let waker = futures::task::noop_waker(); + let mut cx = Context::from_waker(&waker); + + let (tx, rx) = channel::unbounded(); + 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::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!(!temporal_release.update(Duration::from_secs(0))); + assert!(!temporal_release.update(Duration::from_millis(999))); + assert!(temporal_release.update(Duration::from_secs(1))); + assert!(temporal_release.update(Duration::from_secs(3))); + } + + #[test] + fn temporal_release_polling() { + let waker = futures::task::noop_waker(); + let mut cx = Context::from_waker(&waker); + + let (tx, rx) = channel::unbounded(); + let mut temporal_release = + TemporalRelease::new(rand_chacha::ChaCha8Rng::from_entropy(), rx, (1, 2)); + + 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(); + 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(())) + ); + } +}