Module delay

Source
Available on crate feature eh1 only.
Expand description

Delay mock implementations, implementing both sync and async DelayNs traits.

§Choosing a Delay Implementation

There are three implementations available depending on your use case:

  • If you want no actual delay, create a NoopDelay 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 stub implementation, which uses std::thread::sleep to implement the delay.
  • For a configurable delay implementation that supports expectations, use the CheckedDelay mock. By default it doesn’t perform an actual delay, but allows the user to enable them individually for each expected call.

§Usage

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

Structs§

NoopDelay
A Delay implementation that does not actually block.
StdSleep
A Delay implementation that uses std::thread::sleep.
Transaction
Delay transaction

Enums§

TransactionKind
MockDelay transaction kind.

Type Aliases§

CheckedDelay
Mock Delay implementation with checked calls