Restructure InnerMockDriver

Failing test for overallocation of alarms
This commit is contained in:
Chris Price 2024-01-09 11:36:47 +00:00
parent 8dab88f96d
commit fdd7acd484

View File

@ -43,7 +43,7 @@ impl MockDriver {
/// Resets the internal state of the mock driver /// Resets the internal state of the mock driver
/// This will clear and deallocate all alarms, and reset the current time to 0. /// This will clear and deallocate all alarms, and reset the current time to 0.
fn reset(&self) { pub fn reset(&self) {
critical_section::with(|cs| { critical_section::with(|cs| {
self.0.borrow(cs).replace(InnerMockDriver::new()); self.0.borrow(cs).replace(InnerMockDriver::new());
}); });
@ -56,16 +56,12 @@ impl MockDriver {
critical_section::with(|cs| { critical_section::with(|cs| {
let mut inner = self.0.borrow_ref_mut(cs); let mut inner = self.0.borrow_ref_mut(cs);
// TODO: store as Instant? inner.now = inner.now + duration;
let now = (Instant::from_ticks(inner.now) + duration).as_ticks();
if inner.alarm.timestamp <= inner.now.as_ticks() {
inner.alarm.timestamp = u64::MAX;
inner.now = now; Some((inner.alarm.callback, inner.alarm.ctx))
if inner.alarm <= now {
inner.alarm = u64::MAX;
Some((inner.callback, inner.ctx))
} else { } else {
None None
} }
@ -80,7 +76,7 @@ impl MockDriver {
impl Driver for MockDriver { impl Driver for MockDriver {
fn now(&self) -> u64 { fn now(&self) -> u64 {
critical_section::with(|cs| self.0.borrow_ref(cs).now) critical_section::with(|cs| self.0.borrow_ref(cs).now).as_ticks()
} }
unsafe fn allocate_alarm(&self) -> Option<AlarmHandle> { unsafe fn allocate_alarm(&self) -> Option<AlarmHandle> {
@ -91,8 +87,8 @@ impl Driver for MockDriver {
critical_section::with(|cs| { critical_section::with(|cs| {
let mut inner = self.0.borrow_ref_mut(cs); let mut inner = self.0.borrow_ref_mut(cs);
inner.callback = callback; inner.alarm.callback = callback;
inner.ctx = ctx; inner.alarm.ctx = ctx;
}); });
} }
@ -100,10 +96,10 @@ impl Driver for MockDriver {
critical_section::with(|cs| { critical_section::with(|cs| {
let mut inner = self.0.borrow_ref_mut(cs); let mut inner = self.0.borrow_ref_mut(cs);
if timestamp <= inner.now { if timestamp <= inner.now.as_ticks() {
false false
} else { } else {
inner.alarm = timestamp; inner.alarm.timestamp = timestamp;
true true
} }
}) })
@ -111,17 +107,29 @@ impl Driver for MockDriver {
} }
struct InnerMockDriver { struct InnerMockDriver {
now: u64, now: Instant,
alarm: u64, alarm: AlarmState,
callback: fn(*mut ()),
ctx: *mut (),
} }
impl InnerMockDriver { impl InnerMockDriver {
const fn new() -> Self { const fn new() -> Self {
Self { Self {
now: 0, now: Instant::from_ticks(0),
alarm: u64::MAX, alarm: AlarmState::new(),
}
}
}
struct AlarmState {
timestamp: u64,
callback: fn(*mut ()),
ctx: *mut (),
}
impl AlarmState {
const fn new() -> Self {
Self {
timestamp: u64::MAX,
callback: Self::noop, callback: Self::noop,
ctx: core::ptr::null_mut(), ctx: core::ptr::null_mut(),
} }
@ -130,7 +138,7 @@ impl InnerMockDriver {
fn noop(_ctx: *mut ()) {} fn noop(_ctx: *mut ()) {}
} }
unsafe impl Send for InnerMockDriver {} unsafe impl Send for AlarmState {}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@ -163,4 +171,11 @@ mod tests {
driver.advance(Duration::from_secs(1)); driver.advance(Duration::from_secs(1));
assert_eq!(true, unsafe { CALLBACK_CALLED }); assert_eq!(true, unsafe { CALLBACK_CALLED });
} }
#[test]
fn test_allocate_alarm() {
let driver = MockDriver::get();
assert!(unsafe { driver.allocate_alarm() }.is_some());
assert!(unsafe { driver.allocate_alarm() }.is_none());
}
} }