embedded-hal-mock 0.11.1

A collection of mocked devices that implement the embedded-hal traits
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
//! Delay mock implementations, implementing both sync and async
//! [`DelayNs`](https://docs.rs/embedded-hal/latest/embedded_hal/delay/trait.DelayNs.html)
//! traits.
//!
//! ## Choosing a Delay Implementation
//!
//! There are three implementations available depending on your use case:
//!
//! - If you want **no actual delay**, create a
//!   [`NoopDelay`](struct.NoopDelay.html) stub. It will always return
//!   immediately, without a delay. This is useful for fast tests, where you
//!   don't actually need to wait for the hardware.
//! - If you do want the **real delay behavior** when running your tests, use
//!   [`StdSleep`](struct.StdSleep.html) stub implementation, which uses
//!   [`std::thread::sleep`](https://doc.rust-lang.org/std/thread/fn.sleep.html)
//!   to implement the delay.
//! - For a **configurable delay** implementation that supports expectations,
//!   use the [`CheckedDelay`](type.CheckedDelay.html) mock. By default it
//!   doesn't perform an actual delay, but allows the user to enable them
//!   individually for each expected call.
//!
//! ## Usage
//!
//! ```
//! # use eh1 as embedded_hal;
//! use std::time::Duration;
//!
//! use embedded_hal::delay::DelayNs;
//! use embedded_hal_mock::eh1::delay::{CheckedDelay, NoopDelay, StdSleep, Transaction};
//!
//! // No actual delay
//!
//! let mut delay = NoopDelay::new();
//! delay.delay_ms(50); // Returns immediately
//!
//! // Real delay
//!
//! let mut delay = StdSleep::new();
//! delay.delay_ms(50); // Will sleep for 50 ms
//!
//! // Configurable mock
//!
//! let transactions = vec![
//!     Transaction::delay_ns(50_000_000),
//!     Transaction::delay_us(60_000),
//!     Transaction::delay_ms(70).wait(),
//! ];
//!
//! let mut delay = CheckedDelay::new(&transactions);
//!
//! delay.delay_ms(50); // Conversion to nanoseconds
//! delay.delay_ms(60); // Conversion to microseconds
//! delay.delay_ms(70); // This will actually delay
//! delay.done();
//!
//! let mut delay = NoopDelay::new();
//! delay.delay_ms(50); // No checks are performed
//! ```

use std::{thread, time::Duration};

use eh1 as embedded_hal;
use embedded_hal::delay;

use crate::common::Generic;

/// Delay transaction
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Transaction {
    /// Kind is the transaction kind (and data) expected
    kind: TransactionKind,
    real_delay: bool,
}

/// Nanoseconds per microsecond
const NANOS_PER_US: u64 = 1_000;
/// Nanoseconds per millisecond
const NANOS_PER_MS: u64 = 1_000_000;

impl Transaction {
    /// Create a new delay transaction
    pub fn new(kind: TransactionKind) -> Transaction {
        Transaction {
            kind,
            real_delay: false,
        }
    }

    /// Create a new delay_ns transaction
    pub fn delay_ns(ns: u32) -> Transaction {
        Transaction::new(TransactionKind::DelayNs(ns.into()))
    }

    /// Create a new delay_us transaction
    pub fn delay_us(us: u32) -> Transaction {
        Transaction::new(TransactionKind::DelayNs(us as u64 * NANOS_PER_US))
    }

    /// Create a new delay_ms transaction
    pub fn delay_ms(ms: u32) -> Transaction {
        Transaction::new(TransactionKind::DelayNs(ms as u64 * NANOS_PER_MS))
    }

    /// Create a new blocking delay_ns transaction
    pub fn blocking_delay_ns(ns: u32) -> Transaction {
        Transaction::new(TransactionKind::BlockingDelayNs(ns.into()))
    }

    /// Create a new blocking delay_us transaction
    pub fn blocking_delay_us(us: u32) -> Transaction {
        Transaction::new(TransactionKind::BlockingDelayNs(us as u64 * NANOS_PER_US))
    }

    /// Create new blocking delay_ms transaction
    pub fn blocking_delay_ms(ms: u32) -> Transaction {
        Transaction::new(TransactionKind::BlockingDelayNs(ms as u64 * NANOS_PER_MS))
    }

    /// Create a new async delay_ns transaction
    #[cfg(feature = "embedded-hal-async")]
    pub fn async_delay_ns(ns: u32) -> Transaction {
        Transaction::new(TransactionKind::AsyncDelayNs(ns.into()))
    }

    /// Create a new async delay_us transaction
    #[cfg(feature = "embedded-hal-async")]
    pub fn async_delay_us(us: u32) -> Transaction {
        Transaction::new(TransactionKind::AsyncDelayNs(us as u64 * NANOS_PER_US))
    }

    /// Create a new async delay_ms transaction
    #[cfg(feature = "embedded-hal-async")]
    pub fn async_delay_ms(ms: u32) -> Transaction {
        Transaction::new(TransactionKind::AsyncDelayNs(ms as u64 * NANOS_PER_MS))
    }

    /// Perform an actual delay for this transaction
    pub fn wait(mut self) -> Transaction {
        self.real_delay = true;
        self
    }
}

/// MockDelay transaction kind.
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum TransactionKind {
    /// Expect any type of delay in nanoseconds.
    ///
    /// The delay may be either blocking or async. In most cases, this is what you'll want to use.
    DelayNs(u64),
    /// Expect a blocking delay in nanoseconds
    ///
    /// The delay must be blocking. Expectation will fail for async delays.
    BlockingDelayNs(u64),
    /// Expect an async delay in nanoseconds
    ///
    /// The delay must be async. Expectation will fail for blocking delays.
    AsyncDelayNs(u64),
}

/// Mock Delay implementation with checked calls
///
/// This supports the specification and checking of expectations to allow
/// automated testing of delay based drivers. Mismatches between expected and
/// real delay transactions will cause runtime assertions to assist with locating
/// faults.
///
/// See the usage section in the module level docs for an example.
pub type CheckedDelay = Generic<Transaction>;

impl delay::DelayNs for CheckedDelay {
    fn delay_ns(&mut self, ns: u32) {
        let transaction = self.next().expect("no expectation for delay call");

        match transaction.kind {
            TransactionKind::BlockingDelayNs(n) => assert_eq!(n, ns.into(), "wrong delay value"),
            TransactionKind::DelayNs(n) => assert_eq!(n, ns.into(), "wrong delay value"),
            _ => panic!(
                "Wrong kind of delay. Expected DelayNs or BlockingDelayNs got {:?}",
                transaction.kind
            ),
        }

        if transaction.real_delay {
            thread::sleep(Duration::from_nanos(ns as u64));
        }
    }

    fn delay_us(&mut self, us: u32) {
        let transaction = self.next().expect("no expectation for delay call");
        match transaction.kind {
            TransactionKind::BlockingDelayNs(n) => {
                assert_eq!(n, us as u64 * NANOS_PER_US, "wrong delay value")
            }
            TransactionKind::DelayNs(n) => {
                assert_eq!(n, us as u64 * NANOS_PER_US, "wrong delay value")
            }
            _ => panic!(
                "Wrong kind of delay. Expected DelayNs or BlockingDelayNs got {:?}",
                transaction.kind
            ),
        }
        if transaction.real_delay {
            thread::sleep(Duration::from_micros(us as u64));
        }
    }

    fn delay_ms(&mut self, ms: u32) {
        let transaction = self.next().expect("no expectation for delay call");
        match transaction.kind {
            TransactionKind::BlockingDelayNs(n) => {
                assert_eq!(n, ms as u64 * NANOS_PER_MS, "wrong delay value")
            }
            TransactionKind::DelayNs(n) => {
                assert_eq!(n, ms as u64 * NANOS_PER_MS, "wrong delay value")
            }
            _ => panic!(
                "Wrong kind of delay. Expected DelayNs or BlockingDelayNs got {:?}",
                transaction.kind
            ),
        }

        if transaction.real_delay {
            thread::sleep(Duration::from_millis(ms as u64));
        }
    }
}

#[cfg(feature = "embedded-hal-async")]
impl embedded_hal_async::delay::DelayNs for CheckedDelay {
    async fn delay_ns(&mut self, ns: u32) {
        let transaction = self.next().expect("no expectation for delay call");

        match transaction.kind {
            TransactionKind::AsyncDelayNs(n) => assert_eq!(n, ns.into(), "delay unexpected value"),
            TransactionKind::DelayNs(n) => assert_eq!(n, ns.into(), "delay unexpected value"),
            _ => panic!(
                "Wrong kind of delay. Expected DelayNs or AsyncDelayNs got {:?}",
                transaction.kind
            ),
        }

        if transaction.real_delay {
            thread::sleep(Duration::from_nanos(ns as u64));
        }
    }

    async fn delay_us(&mut self, us: u32) {
        let transaction = self.next().expect("no expectation for delay call");
        match transaction.kind {
            TransactionKind::AsyncDelayNs(n) => {
                assert_eq!(n, us as u64 * NANOS_PER_US, "wrong delay value")
            }
            TransactionKind::DelayNs(n) => {
                assert_eq!(n, us as u64 * NANOS_PER_US, "wrong delay value")
            }
            _ => panic!(
                "Wrong kind of delay. Expected DelayNs or AsyncDelayNs got {:?}",
                transaction.kind
            ),
        }

        if transaction.real_delay {
            thread::sleep(Duration::from_micros(us as u64));
        }
    }

    async fn delay_ms(&mut self, ms: u32) {
        let transaction = self.next().expect("no expectation for delay call");
        match transaction.kind {
            TransactionKind::AsyncDelayNs(n) => {
                assert_eq!(n, ms as u64 * NANOS_PER_MS, "wrong delay value")
            }
            TransactionKind::DelayNs(n) => {
                assert_eq!(n, ms as u64 * NANOS_PER_MS, "wrong delay value")
            }
            _ => panic!(
                "Wrong kind of delay. Expected DelayNs or AsyncDelayNs got {:?}",
                transaction.kind
            ),
        }

        if transaction.real_delay {
            thread::sleep(Duration::from_millis(ms as u64));
        }
    }
}

/// A `Delay` implementation that does not actually block.
pub struct NoopDelay;

impl NoopDelay {
    /// Create a new `NoopDelay` instance.
    pub fn new() -> Self {
        NoopDelay
    }
}

impl Default for NoopDelay {
    fn default() -> Self {
        Self::new()
    }
}

impl delay::DelayNs for NoopDelay {
    fn delay_ns(&mut self, _ns: u32) {
        // no-op
    }
}

#[cfg(feature = "embedded-hal-async")]
impl embedded_hal_async::delay::DelayNs for NoopDelay {
    async fn delay_ns(&mut self, _ns: u32) {
        // no-op
    }
}

/// A `Delay` implementation that uses `std::thread::sleep`.
pub struct StdSleep;

impl StdSleep {
    /// Create a new `StdSleep` instance.
    pub fn new() -> Self {
        StdSleep
    }
}

impl Default for StdSleep {
    fn default() -> Self {
        Self::new()
    }
}

impl delay::DelayNs for StdSleep {
    fn delay_ns(&mut self, ns: u32) {
        thread::sleep(Duration::from_nanos(ns as u64));
    }
}

#[cfg(feature = "embedded-hal-async")]
impl embedded_hal_async::delay::DelayNs for StdSleep {
    async fn delay_ns(&mut self, ns: u32) {
        thread::sleep(Duration::from_nanos(ns as u64));
    }
}

#[cfg(test)]
mod test {
    use std::time::Instant;

    use super::*;

    #[test]
    fn test_noop_sleep() {
        use embedded_hal::delay::DelayNs;

        let mut delay = NoopDelay::new();
        let now = Instant::now();
        delay.delay_ms(1000);
        assert!(now.elapsed().as_millis() < 100);
    }

    #[tokio::test]
    #[cfg(feature = "embedded-hal-async")]
    async fn test_noop_sleep_async() {
        use embedded_hal_async::delay::DelayNs;

        let mut delay = NoopDelay::new();
        let now = Instant::now();
        delay.delay_ms(1000).await;
        assert!(now.elapsed().as_millis() < 100);
    }

    #[test]
    fn test_std_sleep() {
        use embedded_hal::delay::DelayNs;

        let mut delay = StdSleep::new();
        let now = Instant::now();
        delay.delay_ms(1000);
        assert!(now.elapsed().as_millis() >= 1000);
    }

    #[tokio::test]
    #[cfg(feature = "embedded-hal-async")]
    async fn test_std_sleep_async() {
        use embedded_hal_async::delay::DelayNs;

        let mut delay = StdSleep::new();
        let now = Instant::now();
        delay.delay_ms(1000).await;
        assert!(now.elapsed().as_millis() >= 1000);
    }

    #[test]
    fn test_checked_sleep() {
        use embedded_hal::delay::DelayNs;

        let transactions = vec![
            Transaction::delay_ns(1000),
            Transaction::delay_ns(2000),
            Transaction::delay_ns(3000),
        ];

        let mut delay = CheckedDelay::new(&transactions);
        let now = Instant::now();
        delay.delay_ns(1000);
        delay.delay_ns(2000);
        delay.delay_ns(3000);
        assert!(now.elapsed().as_millis() < 100);
        delay.done();
    }

    #[tokio::test]
    #[cfg(feature = "embedded-hal-async")]
    async fn test_checked_sleep_async() {
        use embedded_hal_async::delay::DelayNs;

        let transactions = vec![
            Transaction::async_delay_ns(1000),
            Transaction::async_delay_ns(2000),
            Transaction::async_delay_ns(3000),
            Transaction::blocking_delay_ns(4000),
            Transaction::delay_ns(5000),
            Transaction::delay_ns(6000),
        ];

        let mut delay = CheckedDelay::new(&transactions);
        let now = Instant::now();
        delay.delay_ns(1000).await;
        delay.delay_ns(2000).await;
        delay.delay_ns(3000).await;
        embedded_hal::delay::DelayNs::delay_ns(&mut delay, 4000);
        embedded_hal::delay::DelayNs::delay_ns(&mut delay, 5000);
        delay.delay_ns(6000).await;

        assert!(now.elapsed().as_millis() < 100);
        delay.done();
    }

    #[test]
    fn test_checked_sleep_conversions() {
        use embedded_hal::delay::DelayNs;

        let transactions = vec![
            Transaction::delay_ns(1000),
            Transaction::delay_us(1000),
            Transaction::delay_ms(1),
        ];

        let mut delay = CheckedDelay::new(&transactions);
        let now = Instant::now();
        delay.delay_us(1);
        delay.delay_ms(1);
        delay.delay_ns(1_000_000);
        assert!(now.elapsed().as_millis() < 100);
        delay.done();
    }

    #[tokio::test]
    #[cfg(feature = "embedded-hal-async")]
    async fn test_checked_sleep_conversions_async() {
        use embedded_hal_async::delay::DelayNs;

        let transactions = vec![
            Transaction::async_delay_ns(1000),
            Transaction::async_delay_us(1000),
            Transaction::async_delay_ms(1),
        ];

        let mut delay = CheckedDelay::new(&transactions);
        let now = Instant::now();
        delay.delay_us(1).await;
        delay.delay_ms(1).await;
        delay.delay_ns(1_000_000).await;
        assert!(now.elapsed().as_millis() < 100);
        delay.done();
    }

    #[test]
    fn test_checked_sleep_real_delay() {
        use embedded_hal::delay::DelayNs;

        let transactions = vec![
            Transaction::delay_ns(50_000_000).wait(),
            Transaction::delay_us(50_000).wait(),
            Transaction::delay_ms(50).wait(),
        ];

        let mut delay = CheckedDelay::new(&transactions);
        let now = Instant::now();
        delay.delay_ms(50);
        delay.delay_ms(50);
        delay.delay_ms(50);

        assert!(now.elapsed().as_millis() >= 150);
        assert!(now.elapsed().as_millis() < 1500);
        delay.done();
    }

    #[tokio::test]
    #[cfg(feature = "embedded-hal-async")]
    async fn test_checked_sleep_real_delay_async() {
        use embedded_hal_async::delay::DelayNs;

        let transactions = vec![
            Transaction::async_delay_ns(50_000_000).wait(),
            Transaction::async_delay_us(50_000).wait(),
            Transaction::async_delay_ms(50).wait(),
        ];

        let mut delay = CheckedDelay::new(&transactions);
        let now = Instant::now();
        delay.delay_ms(50).await;
        delay.delay_ms(50).await;
        delay.delay_ms(50).await;

        assert!(now.elapsed().as_millis() >= 150);
        assert!(now.elapsed().as_millis() < 1500);
        delay.done();
    }

    #[test]
    fn test_checked_sleep_overflow() {
        use embedded_hal::delay::DelayNs;

        let transactions = vec![
            Transaction::delay_us(4295000),
            Transaction::delay_ms(4295),
            Transaction::delay_us(4295000 * 100),
            Transaction::delay_ms(4295 * 100),
        ];

        let mut delay = CheckedDelay::new(&transactions);
        let now = Instant::now();
        delay.delay_us(4295000);
        delay.delay_ms(4295);
        delay.delay_us(4295000 * 100);
        delay.delay_ms(4295 * 100);
        assert!(now.elapsed().as_millis() < 100);
        delay.done();
    }

    #[test]
    #[should_panic(expected = "wrong delay value")]
    fn test_checked_sleep_overflow_err() {
        use embedded_hal::delay::DelayNs;

        let transactions = vec![Transaction::delay_us(4295)];

        let mut delay = CheckedDelay::new(&transactions);
        let now = Instant::now();
        delay.delay_ms(4);
        assert!(now.elapsed().as_millis() < 100);
        delay.done();
    }

    #[tokio::test]
    #[cfg(feature = "embedded-hal-async")]
    #[should_panic(expected = "wrong delay value")]
    async fn test_checked_sleep_overflow_async_err() {
        use embedded_hal_async::delay::DelayNs;

        let transactions = vec![Transaction::async_delay_us(4295)];

        let mut delay = CheckedDelay::new(&transactions);
        let now = Instant::now();
        delay.delay_ms(4).await;
        assert!(now.elapsed().as_millis() < 100);
        delay.done();
    }
}