aboutsummaryrefslogtreecommitdiff
path: root/embassy-time/src
diff options
context:
space:
mode:
authorChris Price <[email protected]>2024-01-09 11:36:47 +0000
committerChris Price <[email protected]>2024-01-09 15:17:25 +0000
commitfdd7acd48499aac6b3d64eb86d67b198d7ebf5ee (patch)
tree6a0206dd74741dfd4baf2be3ec89b39304bd677b /embassy-time/src
parent8dab88f96d0873851557281128aeb887a10d7c37 (diff)
Restructure InnerMockDriver
Failing test for overallocation of alarms
Diffstat (limited to 'embassy-time/src')
-rw-r--r--embassy-time/src/driver_mock.rs61
1 files changed, 38 insertions, 23 deletions
diff --git a/embassy-time/src/driver_mock.rs b/embassy-time/src/driver_mock.rs
index 128f48af9..d1c21ab28 100644
--- a/embassy-time/src/driver_mock.rs
+++ b/embassy-time/src/driver_mock.rs
@@ -43,7 +43,7 @@ impl MockDriver {
43 43
44 /// Resets the internal state of the mock driver 44 /// Resets the internal state of the mock driver
45 /// This will clear and deallocate all alarms, and reset the current time to 0. 45 /// This will clear and deallocate all alarms, and reset the current time to 0.
46 fn reset(&self) { 46 pub fn reset(&self) {
47 critical_section::with(|cs| { 47 critical_section::with(|cs| {
48 self.0.borrow(cs).replace(InnerMockDriver::new()); 48 self.0.borrow(cs).replace(InnerMockDriver::new());
49 }); 49 });
@@ -56,19 +56,15 @@ impl MockDriver {
56 critical_section::with(|cs| { 56 critical_section::with(|cs| {
57 let mut inner = self.0.borrow_ref_mut(cs); 57 let mut inner = self.0.borrow_ref_mut(cs);
58 58
59 // TODO: store as Instant? 59 inner.now = inner.now + duration;
60 let now = (Instant::from_ticks(inner.now) + duration).as_ticks();
61 60
61 if inner.alarm.timestamp <= inner.now.as_ticks() {
62 inner.alarm.timestamp = u64::MAX;
62 63
63 inner.now = now; 64 Some((inner.alarm.callback, inner.alarm.ctx))
64 65 } else {
65 if inner.alarm <= now { 66 None
66 inner.alarm = u64::MAX; 67 }
67
68 Some((inner.callback, inner.ctx))
69 } else {
70 None
71 }
72 }) 68 })
73 }; 69 };
74 70
@@ -80,7 +76,7 @@ impl MockDriver {
80 76
81impl Driver for MockDriver { 77impl Driver for MockDriver {
82 fn now(&self) -> u64 { 78 fn now(&self) -> u64 {
83 critical_section::with(|cs| self.0.borrow_ref(cs).now) 79 critical_section::with(|cs| self.0.borrow_ref(cs).now).as_ticks()
84 } 80 }
85 81
86 unsafe fn allocate_alarm(&self) -> Option<AlarmHandle> { 82 unsafe fn allocate_alarm(&self) -> Option<AlarmHandle> {
@@ -91,8 +87,8 @@ impl Driver for MockDriver {
91 critical_section::with(|cs| { 87 critical_section::with(|cs| {
92 let mut inner = self.0.borrow_ref_mut(cs); 88 let mut inner = self.0.borrow_ref_mut(cs);
93 89
94 inner.callback = callback; 90 inner.alarm.callback = callback;
95 inner.ctx = ctx; 91 inner.alarm.ctx = ctx;
96 }); 92 });
97 } 93 }
98 94
@@ -100,10 +96,10 @@ impl Driver for MockDriver {
100 critical_section::with(|cs| { 96 critical_section::with(|cs| {
101 let mut inner = self.0.borrow_ref_mut(cs); 97 let mut inner = self.0.borrow_ref_mut(cs);
102 98
103 if timestamp <= inner.now { 99 if timestamp <= inner.now.as_ticks() {
104 false 100 false
105 } else { 101 } else {
106 inner.alarm = timestamp; 102 inner.alarm.timestamp = timestamp;
107 true 103 true
108 } 104 }
109 }) 105 })
@@ -111,17 +107,29 @@ impl Driver for MockDriver {
111} 107}
112 108
113struct InnerMockDriver { 109struct InnerMockDriver {
114 now: u64, 110 now: Instant,
115 alarm: u64, 111 alarm: AlarmState,
112}
113
114impl InnerMockDriver {
115 const fn new() -> Self {
116 Self {
117 now: Instant::from_ticks(0),
118 alarm: AlarmState::new(),
119 }
120 }
121}
122
123struct AlarmState {
124 timestamp: u64,
116 callback: fn(*mut ()), 125 callback: fn(*mut ()),
117 ctx: *mut (), 126 ctx: *mut (),
118} 127}
119 128
120impl InnerMockDriver { 129impl AlarmState {
121 const fn new() -> Self { 130 const fn new() -> Self {
122 Self { 131 Self {
123 now: 0, 132 timestamp: u64::MAX,
124 alarm: u64::MAX,
125 callback: Self::noop, 133 callback: Self::noop,
126 ctx: core::ptr::null_mut(), 134 ctx: core::ptr::null_mut(),
127 } 135 }
@@ -130,7 +138,7 @@ impl InnerMockDriver {
130 fn noop(_ctx: *mut ()) {} 138 fn noop(_ctx: *mut ()) {}
131} 139}
132 140
133unsafe impl Send for InnerMockDriver {} 141unsafe impl Send for AlarmState {}
134 142
135#[cfg(test)] 143#[cfg(test)]
136mod tests { 144mod tests {
@@ -163,4 +171,11 @@ mod tests {
163 driver.advance(Duration::from_secs(1)); 171 driver.advance(Duration::from_secs(1));
164 assert_eq!(true, unsafe { CALLBACK_CALLED }); 172 assert_eq!(true, unsafe { CALLBACK_CALLED });
165 } 173 }
174
175 #[test]
176 fn test_allocate_alarm() {
177 let driver = MockDriver::get();
178 assert!(unsafe { driver.allocate_alarm() }.is_some());
179 assert!(unsafe { driver.allocate_alarm() }.is_none());
180 }
166} 181}