Add tests for timers and intervals.

This commit is contained in:
Alejandro Cabeza Romero 2024-11-07 06:30:48 +01:00
parent 36dc5ef02c
commit 85a7a21f5b
No known key found for this signature in database
GPG Key ID: DA3D14AE478030FD
3 changed files with 23 additions and 13 deletions

2
.gitignore vendored
View File

@ -5,3 +5,5 @@ __pycache__/
simulation simulation
simlib/netrunner/target simlib/netrunner/target
.idea/ .idea/
target/
Cargo.lock

View File

@ -62,11 +62,12 @@ mod tests {
#[test] #[test]
fn counter_interval() { fn counter_interval() {
let waker = futures::task::noop_waker(); 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 (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::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);
@ -81,11 +82,12 @@ mod tests {
#[test] #[test]
fn slot_interval() { fn slot_interval() {
let waker = futures::task::noop_waker(); 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 (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::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);

View File

@ -103,19 +103,22 @@ mod tests {
fn interval_update() { fn interval_update() {
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_eq!(interval.update(Duration::from_secs(0)), false);
assert_eq!(interval.update(Duration::from_secs(1)), false); assert!(!interval.update(Duration::from_secs(0)));
assert_eq!(interval.update(Duration::from_secs(1)), true); assert!(!interval.update(Duration::from_secs(1)));
assert_eq!(interval.update(Duration::from_secs(3)), false); assert!(interval.update(Duration::from_secs(1)));
assert!(interval.update(Duration::from_secs(3)));
} }
#[test] #[test]
fn interval_polling() { fn interval_polling() {
let waker = futures::task::noop_waker(); 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 (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::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();
assert_eq!(interval.poll_next_unpin(&mut cx), Poll::Pending); assert_eq!(interval.poll_next_unpin(&mut cx), Poll::Pending);
@ -130,20 +133,23 @@ 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, 2));
assert_eq!(temporal_release.update(Duration::from_secs(0)), false);
assert_eq!(temporal_release.update(Duration::from_millis(999)), false); assert!(!temporal_release.update(Duration::from_secs(0)));
assert_eq!(temporal_release.update(Duration::from_secs(1)), true); assert!(!temporal_release.update(Duration::from_millis(999)));
assert_eq!(temporal_release.update(Duration::from_secs(3)), true); assert!(temporal_release.update(Duration::from_secs(1)));
assert!(temporal_release.update(Duration::from_secs(3)));
} }
#[test] #[test]
fn temporal_release_polling() { fn temporal_release_polling() {
let waker = futures::task::noop_waker(); 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 (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, 2));
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);
tx.send(Duration::from_millis(999)).unwrap(); tx.send(Duration::from_millis(999)).unwrap();
assert_eq!(temporal_release.poll_next_unpin(&mut cx), Poll::Pending); assert_eq!(temporal_release.poll_next_unpin(&mut cx), Poll::Pending);