Skip to main content

MockSpawner

Struct MockSpawner 

Source
pub struct MockSpawner { /* private fields */ }
Expand description

A mocked version of embassy_executor::Spawner that can be used in its place for unit tests.

This mocked version counts how many times Self::spawn() is called and can be checked that Self::spawn() was called the correct number of times using Self::done(). If Self::done() is not called then it asserts that Self::spawn() was called the correct number of times when dropped which causes a panic if incorrect.

§Panics

Panics if Self::spawn() called the wrong number of times and Self is dropped before calling Self::done().

§Examples

use embassy_mock::executor::{MockSpawner, MockSpawnerError, Spawner};

#[embassy_executor::task]
async fn example_task() {}

let spawner = MockSpawner::expect(4);
spawner.spawn(example_task()).unwrap();

let res = spawner.done();

let expected = Err(MockSpawnerError::WrongNumberOfTasks {
    expected: 4,
    actual: 1,
});
assert_eq!(res, expected);
use embassy_mock::executor::{MockSpawner, Spawner};

#[embassy_executor::task]
async fn example_task() {}

let spawner = MockSpawner::expect(1); // Expects `spawn()` to be called once.
spawner.spawn(example_task()).unwrap(); // `spawn()` is called once.

// `spawner` is dropped but doesn't panic.
use embassy_mock::executor::{MockSpawner, Spawner};

#[embassy_executor::task]
async fn example_task() {}

let spawner = MockSpawner::expect(2); // Expects `spawn()` to be called twice.
spawner.spawn(example_task()).unwrap(); // `spawn()` is called only once.

// `spawner` is dropped and will panic.

Implementations§

Source§

impl MockSpawner

Source

pub const fn expect(expected: usize) -> Self

Create a MockSpawner, providing the expected number of calls to Self::spawn().

§Examples
use embassy_mock::executor::MockSpawner;

let spawner = MockSpawner::expect(X); // Where `X` is the number of times `spawn()` should be called
Source

pub fn done(self) -> Result<(), MockSpawnerError>

Mark the MockSpawner as done and check if Self::spawn() was called the correct number of times.

This is a cleaner way of testing that Self::spawn() is called the correct number of times as MockSpawner doesn’t cause a panic when dropped if this method is called, it also returns a Result<(), MockSpawnerError> which allows checking the outcome of the mock.

§Examples
use embassy_mock::executor::{MockSpawner, Spawner};

#[embassy_executor::task]
async fn example_task() {}

let spawner = MockSpawner::expect(1);
spawner.spawn(example_task()).unwrap();

let res = spawner.done();

assert_eq!(res, Ok(()));
use embassy_mock::executor::{MockSpawner, MockSpawnerError, Spawner};

#[embassy_executor::task]
async fn example_task() {}

let spawner = MockSpawner::expect(4);
spawner.spawn(example_task()).unwrap();

let res = spawner.done();

let expected = Err(MockSpawnerError::WrongNumberOfTasks {
    expected: 4,
    actual: 1,
});
assert_eq!(res, expected);

// This doesn't panic when `spawner` is dropped as `spawner.done()` was called.

Trait Implementations§

Source§

impl Debug for MockSpawner

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for MockSpawner

Source§

fn drop(&mut self)

If Self::done() has not been called before being dropped then check that the number of times Self::spawn() was called is as expected.

Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Spawner for MockSpawner

Source§

fn spawn<S>(&self, token: SpawnToken<S>) -> Result<(), SpawnError>

Increment an internal counter of how many times this method is called.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.