2023-04-10 10:52:21 +02:00
|
|
|
//! A test that verifies the correctness of the [`TimerQueue`].
|
|
|
|
//!
|
|
|
|
//! To run this test, you need to activate the `critical-section/std` feature.
|
|
|
|
|
2023-04-10 13:05:36 +02:00
|
|
|
use std::{
|
|
|
|
fmt::Debug,
|
|
|
|
task::{Poll, Waker},
|
|
|
|
};
|
2023-04-10 10:34:00 +02:00
|
|
|
|
2023-04-10 13:05:36 +02:00
|
|
|
use cassette::{pin_mut, Cassette};
|
2023-04-10 10:34:00 +02:00
|
|
|
use parking_lot::Mutex;
|
|
|
|
use rtic_time::{Monotonic, TimerQueue};
|
|
|
|
|
2023-04-10 13:05:36 +02:00
|
|
|
static NOW: Mutex<Option<Instant>> = Mutex::new(None);
|
2023-04-10 10:34:00 +02:00
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)]
|
2023-04-10 13:05:36 +02:00
|
|
|
pub struct Duration(u64);
|
|
|
|
|
|
|
|
impl Duration {
|
|
|
|
pub fn from_ticks(millis: u64) -> Self {
|
|
|
|
Self(millis)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_ticks(&self) -> u64 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl core::ops::Add<Duration> for Duration {
|
|
|
|
type Output = Duration;
|
|
|
|
|
|
|
|
fn add(self, rhs: Duration) -> Self::Output {
|
|
|
|
Self(self.0 + rhs.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Duration> for Instant {
|
|
|
|
fn from(value: Duration) -> Self {
|
|
|
|
Instant(value.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static WAKERS: Mutex<Vec<Waker>> = Mutex::new(Vec::new());
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)]
|
|
|
|
pub struct Instant(u64);
|
2023-04-10 10:34:00 +02:00
|
|
|
|
|
|
|
impl Instant {
|
2023-04-10 13:05:36 +02:00
|
|
|
const ZERO: Self = Self(0);
|
|
|
|
|
|
|
|
pub fn tick() -> bool {
|
|
|
|
// If we've never ticked before, initialize the clock.
|
|
|
|
if NOW.lock().is_none() {
|
|
|
|
*NOW.lock() = Some(Instant::ZERO);
|
|
|
|
}
|
|
|
|
// We've ticked before, add one to the clock
|
|
|
|
else {
|
|
|
|
let now = Instant::now();
|
|
|
|
let new_time = now + Duration(1);
|
|
|
|
*NOW.lock() = Some(new_time);
|
|
|
|
}
|
|
|
|
|
|
|
|
let had_wakers = !WAKERS.lock().is_empty();
|
|
|
|
// Wake up all things waiting for a specific time to happen.
|
|
|
|
for waker in WAKERS.lock().drain(..) {
|
|
|
|
waker.wake_by_ref();
|
|
|
|
}
|
|
|
|
|
|
|
|
let had_interrupt = TestMono::tick(false);
|
|
|
|
|
|
|
|
had_interrupt || had_wakers
|
2023-04-10 10:34:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn now() -> Self {
|
2023-04-10 13:05:36 +02:00
|
|
|
NOW.lock().clone().unwrap_or(Instant::ZERO)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn elapsed(&self) -> Duration {
|
|
|
|
Duration(Self::now().0 - self.0)
|
|
|
|
}
|
|
|
|
|
2023-04-10 13:09:50 +02:00
|
|
|
pub async fn wait_until(time: Instant) {
|
2023-04-10 13:05:36 +02:00
|
|
|
core::future::poll_fn(|ctx| {
|
|
|
|
if Instant::now() >= time {
|
|
|
|
Poll::Ready(())
|
|
|
|
} else {
|
|
|
|
WAKERS.lock().push(ctx.waker().clone());
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<u64> for Instant {
|
|
|
|
fn from(value: u64) -> Self {
|
2023-04-10 13:09:50 +02:00
|
|
|
Self(value)
|
2023-04-10 10:34:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl core::ops::Add<Duration> for Instant {
|
|
|
|
type Output = Instant;
|
|
|
|
|
|
|
|
fn add(self, rhs: Duration) -> Self::Output {
|
2023-04-10 13:05:36 +02:00
|
|
|
Self(self.0 + rhs.0)
|
2023-04-10 10:34:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl core::ops::Sub<Duration> for Instant {
|
|
|
|
type Output = Instant;
|
|
|
|
|
|
|
|
fn sub(self, rhs: Duration) -> Self::Output {
|
2023-04-10 13:05:36 +02:00
|
|
|
Self(self.0 - rhs.0)
|
2023-04-10 10:34:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl core::ops::Sub<Instant> for Instant {
|
|
|
|
type Output = Duration;
|
|
|
|
|
|
|
|
fn sub(self, rhs: Instant) -> Self::Output {
|
2023-04-10 13:05:36 +02:00
|
|
|
Duration(self.0 - rhs.0)
|
2023-04-10 10:34:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-10 13:05:36 +02:00
|
|
|
static COMPARE: Mutex<Option<Instant>> = Mutex::new(None);
|
|
|
|
static TIMER_QUEUE: TimerQueue<TestMono> = TimerQueue::new();
|
2023-04-10 10:34:00 +02:00
|
|
|
|
2023-04-10 13:05:36 +02:00
|
|
|
pub struct TestMono;
|
2023-04-10 10:34:00 +02:00
|
|
|
|
2023-04-10 13:05:36 +02:00
|
|
|
impl TestMono {
|
|
|
|
pub fn tick(force_interrupt: bool) -> bool {
|
|
|
|
let now = Instant::now();
|
2023-04-10 10:34:00 +02:00
|
|
|
|
2023-04-10 13:05:36 +02:00
|
|
|
let compare_reached = Some(now) == Self::compare();
|
|
|
|
let interrupt = compare_reached || force_interrupt;
|
2023-04-10 10:34:00 +02:00
|
|
|
|
2023-04-10 13:05:36 +02:00
|
|
|
if interrupt {
|
|
|
|
unsafe {
|
|
|
|
TestMono::queue().on_monotonic_interrupt();
|
2023-04-10 10:34:00 +02:00
|
|
|
}
|
2023-04-10 13:05:36 +02:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2023-04-10 10:34:00 +02:00
|
|
|
|
2023-04-10 13:05:36 +02:00
|
|
|
/// Initialize the monotonic.
|
|
|
|
pub fn init() {
|
|
|
|
Self::queue().initialize(Self);
|
2023-04-10 10:34:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Used to access the underlying timer queue
|
2023-04-10 13:05:36 +02:00
|
|
|
pub fn queue() -> &'static TimerQueue<TestMono> {
|
2023-04-10 10:34:00 +02:00
|
|
|
&TIMER_QUEUE
|
|
|
|
}
|
2023-04-10 13:05:36 +02:00
|
|
|
|
|
|
|
pub fn compare() -> Option<Instant> {
|
|
|
|
COMPARE.lock().clone()
|
|
|
|
}
|
2023-04-10 10:34:00 +02:00
|
|
|
}
|
|
|
|
|
2023-04-10 13:05:36 +02:00
|
|
|
impl Monotonic for TestMono {
|
|
|
|
const ZERO: Self::Instant = Instant::ZERO;
|
2023-04-10 10:34:00 +02:00
|
|
|
|
|
|
|
type Instant = Instant;
|
|
|
|
|
|
|
|
type Duration = Duration;
|
|
|
|
|
|
|
|
fn now() -> Self::Instant {
|
|
|
|
Instant::now()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_compare(instant: Self::Instant) {
|
2023-04-10 13:05:36 +02:00
|
|
|
let _ = COMPARE.lock().insert(instant);
|
2023-04-10 10:34:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn clear_compare_flag() {}
|
|
|
|
|
|
|
|
fn pend_interrupt() {
|
2023-04-10 13:05:36 +02:00
|
|
|
Self::tick(true);
|
2023-04-10 10:34:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-10 13:05:36 +02:00
|
|
|
#[test]
|
|
|
|
fn timer_queue() {
|
|
|
|
TestMono::init();
|
|
|
|
let start = Instant::ZERO;
|
2023-04-10 10:34:00 +02:00
|
|
|
|
2023-04-10 13:05:36 +02:00
|
|
|
let build_delay_test = |pre_delay: Option<u64>, delay: u64| {
|
|
|
|
let delay = Duration::from_ticks(delay);
|
|
|
|
let pre_delay = pre_delay.map(Duration::from_ticks);
|
2023-04-10 10:34:00 +02:00
|
|
|
|
|
|
|
let total = if let Some(pre_delay) = pre_delay {
|
|
|
|
pre_delay + delay
|
|
|
|
} else {
|
|
|
|
delay
|
|
|
|
};
|
2023-04-10 13:05:36 +02:00
|
|
|
let total_millis = total.as_ticks();
|
|
|
|
|
2023-04-10 10:34:00 +02:00
|
|
|
async move {
|
2023-04-10 13:09:50 +02:00
|
|
|
// A `pre_delay` simulates a delay in scheduling,
|
|
|
|
// without the `pre_delay` being present in the timer
|
|
|
|
// queue
|
2023-04-10 10:34:00 +02:00
|
|
|
if let Some(pre_delay) = pre_delay {
|
2023-04-10 13:09:50 +02:00
|
|
|
Instant::wait_until(start + pre_delay).await;
|
2023-04-10 10:34:00 +02:00
|
|
|
}
|
|
|
|
|
2023-04-10 13:05:36 +02:00
|
|
|
TestMono::queue().delay(delay).await;
|
2023-04-10 10:34:00 +02:00
|
|
|
|
2023-04-10 13:05:36 +02:00
|
|
|
let elapsed = start.elapsed().as_ticks();
|
|
|
|
println!("{total_millis} ticks delay reached after {elapsed} ticks");
|
2023-04-10 10:34:00 +02:00
|
|
|
|
2023-04-10 13:05:36 +02:00
|
|
|
if elapsed != total_millis {
|
|
|
|
panic!(
|
|
|
|
"{total_millis} ticks delay was not on time ({elapsed} ticks passed instead)"
|
|
|
|
);
|
2023-04-10 10:34:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-04-10 13:05:36 +02:00
|
|
|
let d1 = build_delay_test(Some(100), 100);
|
|
|
|
pin_mut!(d1);
|
|
|
|
let mut d1 = Cassette::new(d1);
|
|
|
|
|
|
|
|
let d2 = build_delay_test(None, 300);
|
|
|
|
pin_mut!(d2);
|
|
|
|
let mut d2 = Cassette::new(d2);
|
|
|
|
|
|
|
|
let d3 = build_delay_test(None, 400);
|
|
|
|
pin_mut!(d3);
|
|
|
|
let mut d3 = Cassette::new(d3);
|
|
|
|
|
|
|
|
macro_rules! try_poll {
|
|
|
|
($fut:ident) => {
|
|
|
|
if !$fut.is_done() {
|
|
|
|
$fut.poll_on();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do an initial poll to set up all of the waiting futures
|
|
|
|
try_poll!(d1);
|
|
|
|
try_poll!(d2);
|
|
|
|
try_poll!(d3);
|
|
|
|
|
|
|
|
for _ in 0..500 {
|
|
|
|
// We only poll the waiting futures if an
|
|
|
|
// interrupt occured or if an artificial delay
|
|
|
|
// has passed.
|
|
|
|
if Instant::tick() {
|
|
|
|
try_poll!(d1);
|
|
|
|
try_poll!(d2);
|
|
|
|
try_poll!(d3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if Instant::now() == 0.into() {
|
|
|
|
// First, we want to be waiting for our 300 tick delay
|
|
|
|
assert_eq!(TestMono::compare(), Some(300.into()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if Instant::now() == 100.into() {
|
|
|
|
// After 100 ticks, we enqueue a new delay that is supposed to last
|
|
|
|
// until the 200-tick-mark
|
|
|
|
assert_eq!(TestMono::compare(), Some(200.into()));
|
|
|
|
}
|
2023-04-10 10:34:00 +02:00
|
|
|
|
2023-04-10 13:05:36 +02:00
|
|
|
if Instant::now() == 200.into() {
|
|
|
|
// After 200 ticks, we dequeue the 200-tick-mark delay and
|
|
|
|
// requeue the 300 tick delay
|
|
|
|
assert_eq!(TestMono::compare(), Some(300.into()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if Instant::now() == 300.into() {
|
|
|
|
// After 300 ticks, we dequeue the 300-tick-mark delay and
|
|
|
|
// go to the 400 tick delay that is already enqueued
|
|
|
|
assert_eq!(TestMono::compare(), Some(400.into()));
|
|
|
|
}
|
|
|
|
}
|
2023-04-10 10:34:00 +02:00
|
|
|
|
2023-04-10 13:05:36 +02:00
|
|
|
assert!(d1.is_done() && d2.is_done() && d3.is_done());
|
2023-04-10 10:34:00 +02:00
|
|
|
}
|