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>
This commit is contained in:
parent
631c1a6916
commit
8faafcfb6f
|
@ -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
|
||||
|
|
|
@ -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)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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(()))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue