From 85a7a21f5b105e6304f4f029d5840d6cf701fbda Mon Sep 17 00:00:00 2001 From: Alejandro Cabeza Romero Date: Thu, 7 Nov 2024 06:30:48 +0100 Subject: [PATCH] Add tests for timers and intervals. --- .gitignore | 4 ++- .../src/node/mix/consensus_streams.rs | 6 +++-- simlib/mixnet-sims/src/node/mix/scheduler.rs | 26 ++++++++++++------- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 34df463..b88a4cd 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ __pycache__/ *.so simulation simlib/netrunner/target -.idea/ \ No newline at end of file +.idea/ +target/ +Cargo.lock \ No newline at end of file diff --git a/simlib/mixnet-sims/src/node/mix/consensus_streams.rs b/simlib/mixnet-sims/src/node/mix/consensus_streams.rs index eb764f3..c05b5b8 100644 --- a/simlib/mixnet-sims/src/node/mix/consensus_streams.rs +++ b/simlib/mixnet-sims/src/node/mix/consensus_streams.rs @@ -62,11 +62,12 @@ mod tests { #[test] fn counter_interval() { let waker = futures::task::noop_waker(); - let mut cx = futures::task::Context::from_waker(&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); @@ -81,11 +82,12 @@ mod tests { #[test] fn slot_interval() { let waker = futures::task::noop_waker(); - let mut cx = futures::task::Context::from_waker(&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); diff --git a/simlib/mixnet-sims/src/node/mix/scheduler.rs b/simlib/mixnet-sims/src/node/mix/scheduler.rs index a33df0d..aef1c57 100644 --- a/simlib/mixnet-sims/src/node/mix/scheduler.rs +++ b/simlib/mixnet-sims/src/node/mix/scheduler.rs @@ -103,19 +103,22 @@ mod tests { 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); + + 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 = futures::task::Context::from_waker(&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); @@ -130,20 +133,23 @@ mod tests { 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); + + 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 = futures::task::Context::from_waker(&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);