下面显示的是英文原文。
Testing an embedded app
testing::embedded::TestApp runs a device service's EmbeddedApp on the host, with every broker connected in process and the clock the app reads in the test's hands.
The test hands the app function its brokers, as the device's task does, and drives the app with the same calls a tokio test makes: broker, settle, advance and shutdown.
The app runs inside the test's own future, so a test is a plain #[test] that blocks on one future, and one TestApp runs at a time in a process.
The harness is the test build's embassy-time driver: the clock starts at zero and moves only when the test moves it.
embassy-time's own mock-driver is a second driver of the same clock, and a build links only one of the two.
With the testing feature on, every broker an embedded app registers implements the in-process mode, InProcess.
An app that registers a broker without it does not build in a test build.
embedded::TestApp::start returns once the app is running.
A broker that waits on the clock to connect, or a subscription that waits to open, is waited for: the clock moves from one armed deadline to the next, and a startup nothing wakes returns TestError::Stalled.
shutdown lets the work the app holds run to its end: the clock moves from one armed deadline to the next until the app has stopped.
A handler or a hook that waits on a timer finishes, the shutdown hooks run and every broker shuts down.
Work that waits on something nothing wakes makes shutdown return TestError::Stalled, and an app that stopped on its own error returns it as TestError::Failed.
What the harness adds
Out slot records
tb.out::<M>() opens the record of the Out slot marked M: every message a handler sent through it, with its destination and headers.
It reads the same way as on tokio, as the out page describes.
A slot publish is also recorded on its destination, so published::<T>(name) on the broker it went to sees it.
Hooks
on_call and on_publish take the same async closures as on tokio.
A hook runs inside the test's own future: the dispatch that recorded the event waits for it, and the step that drives the app polls it beside the app.
A hook runs on the test's own thread, and a hook that waits on the clock finishes once the test advances it or shuts the app down.
A hook on a destination the test itself publishes to runs in that publish, up to where it waits on the clock.
A panic inside a hook, a failed assertion included, fails the publish, settle, advance or shutdown that ran it.
A state the test builds
TestApp::with_state(app, build) starts the app with the state build returns in place of the one the app was built with: a value read from the board's flash, a driver of its hardware.
The app function names its state in its type, EmbeddedApp<impl Mounts<State = Limits>>, so the test can build one.
The harness connects every broker first, then calls build with the brokers, so brokers.broker::<B>() returns the connected form of the app's broker of type B, as on tokio.
Everything else of startup runs as in TestApp::start.
A build without a heap
The harness follows the build: without the framework's alloc feature the same TestApp drives the fixed form of the app, with FixedHeaders, slice codecs, the continuation room and the board's static bus.
A test binary can install assert_no_alloc::AllocDisabler as its global allocator, and the harness arms it around every poll of the app, so a test fails on an allocation the framework makes.
The harness's own bookkeeping, a call record or a settlement note, runs where the allocator lets it through.
The state of a service in this build holds no heap type either: a static counter replaces an Rc, because the app drops its state inside a poll.
Example
Out slot records
- Macros
- Manual
#[test]
fn an_out_slot_publish_is_recorded_with_its_slot() -> Result<(), Box<dyn Error>> {
block_on(async {
let tb = TestApp::start(app(Bus::new())).await?;
tb.broker::<Bus>()
.message(&Sent(b"high"))
.to("sensors/level")
.publish()
.await?;
tb.out::<Alerts>().assert_called_once().with_raw(b"high");
tb.shutdown().await?;
Ok(())
})
}
#[test]
fn an_alarm_goes_through_its_slot() -> Result<(), Box<dyn Error>> {
block_on(async {
let tb = TestApp::with_state(tank(Bus::new()), |_brokers| Limits { alarm: 50 }).await?;
tb.broker::<Bus>()
.message(&Sent(b"\x64"))
.to("tank/level")
.publish()
.await?;
tb.out::<Alerts>().assert_called_once().with_raw(b"\x64");
tb.shutdown().await?;
Ok(())
})
}
Hooks
- Macros
- Manual
#[test]
fn a_slot_publish_hook_runs_in_the_publish_that_caused_it() -> Result<(), Box<dyn Error>> {
block_on(async {
let tb = TestApp::start(app(Bus::new())).await?;
let hooked = Arc::new(AtomicUsize::new(0));
let seen = Arc::clone(&hooked);
tb.out::<Alerts>().on_publish(async move |alerts| {
alerts.assert_called_once().with_raw(b"high");
seen.fetch_add(1, Ordering::SeqCst);
});
tb.broker::<Bus>()
.message(&Sent(b"high"))
.to("sensors/level")
.publish()
.await?;
assert_eq!(hooked.load(Ordering::SeqCst), 1);
tb.shutdown().await?;
Ok(())
})
}
#[test]
fn a_hook_checks_each_alarm_as_it_goes_out() -> Result<(), Box<dyn Error>> {
block_on(async {
let tb = TestApp::with_state(tank(Bus::new()), |_brokers| Limits { alarm: 50 }).await?;
let hooked = Arc::new(AtomicUsize::new(0));
let seen = Arc::clone(&hooked);
tb.out::<Alerts>().on_publish(async move |alerts| {
alerts.assert_called_once().with_raw(b"\x64");
seen.fetch_add(1, Ordering::SeqCst);
});
tb.broker::<Bus>()
.message(&Sent(b"\x64"))
.to("tank/level")
.publish()
.await?;
assert_eq!(hooked.load(Ordering::SeqCst), 1);
tb.shutdown().await?;
Ok(())
})
}
A state the test builds
- Macros
- Manual
#[test]
fn the_test_hands_the_app_its_state() -> Result<(), Box<dyn Error>> {
block_on(async {
let tb = TestApp::with_state(tank(Bus::new()), |brokers| Limits {
alarm: 50,
echo: Some(brokers.broker::<Bus>().publisher()),
})
.await?;
tb.broker::<Bus>()
.message(&Sent(b"\x64"))
.to("tank/level")
.publish()
.await?;
tb.out::<Alerts>().assert_called_once().with_raw(b"\x64");
tb.broker::<Bus>()
.published::<()>("tank/echo")
.assert_called_once()
.with_raw(b"\x64");
tb.shutdown().await?;
Ok(())
})
}
#[test]
fn the_board_alarms_from_the_tests_limit() -> Result<(), Box<dyn Error>> {
block_on(async {
let tb = TestApp::start(tank(Bus::new())).await?;
tb.broker::<Bus>()
.message(&Sent(b"\x64"))
.to("tank/level")
.publish()
.await?;
tb.out::<Alerts>().assert_not_called();
tb.shutdown().await?;
let tb = TestApp::with_state(tank(Bus::new()), |_brokers| Limits { alarm: 50 }).await?;
tb.broker::<Bus>()
.message(&Sent(b"\x64"))
.to("tank/level")
.publish()
.await?;
tb.out::<Alerts>().assert_called_once();
tb.shutdown().await?;
Ok(())
})
}
A build without a heap
The allocator the test binary installs, and a test of a retry on the mock clock against the service of the Embedded runtime page:
/// Fails an allocation made while the harness polls the app.
#[global_allocator]
static ALLOCATOR: AllocDisabler = AllocDisabler;
#[test]
fn a_retry_waits_for_its_delay() -> Result<(), Box<dyn Error>> {
block_on(async {
let tb = TestApp::start(retrying_app(Bus::new(&BUS))).await?;
let reading = Humidity {
sensor: 1,
percent: 40,
};
tb.broker::<Bus>().message(&reading).publish().await?;
tb.advance(Duration::from_secs(60)).await?;
tb.broker::<Bus>()
.subscriber("sensors/humidity")
.assert_called(2);
tb.shutdown().await?;
Ok(())
})
}