Skip to main content

The embedded TestApp

The embedded TestApp runs a device service on the host with the broker connected in process, and the test drives it with publish, advance and assertions.

The test hands the production app its broker, built over a static memory the test declares as the board does, and addresses the broker by its type: tb.broker::<MqttBroker>(). The runner is not needed, because the in-process transport replaces the connection. It is a model of the server: it keeps the subscriptions, the retained message of each topic and the share groups, matches filters with wildcards, and delivers at the lower of the two levels. message(..).publish() is a publish of another client of the server, at QoS 1 and not retained. The clock is the test's: advance moves it, and a retry_after copy comes back once its delay has passed.

The deliveries a test holds at once are bounded by the memory of the broker: a handler that has not settled a delivery keeps its receive buffer, as on the device. A persistent session and its redelivery, the receive window and the handshakes behind QoS 1 and 2 belong to the server, and the live stand tests them. The assertions on the options of a publish, such as with_options, read the steps and the retain flag of an Out slot.

Example​

A job is answered, and the answer is read from the topic it was published to.

#[test]
fn a_job_is_answered() {
static MEMORY: ConstStaticCell<MqttState> = ConstStaticCell::new(MqttState::new());
block_on(async {
let tb = TestApp::start(app(board(MEMORY.take())))
.await
.expect("the app starts");
let jobs = tb.broker::<MqttBroker>();
jobs.message(&Sent(b"mix"))
.to("jobs/run")
.publish()
.await
.expect("the harness publishes");

jobs.subscriber("jobs/run")
.assert_called_once()
.with_raw(b"mix")
.settled(HandlerOutcome::ack());
jobs.published::<()>("jobs/done")
.assert_called_once()
.with_raw(b"done");
tb.shutdown().await.expect("the app stops");
});
}

A retry on the mock clock​

#[test]
fn a_job_is_asked_for_again_when_its_delay_has_passed() {
static MEMORY: ConstStaticCell<MqttState> = ConstStaticCell::new(MqttState::new());
block_on(async {
let tb = TestApp::start(retrying_app(board(MEMORY.take())))
.await
.expect("the app starts");
let jobs = tb.broker::<MqttBroker>();
jobs.message(&Sent(b"mix"))
.to("jobs/later")
.publish()
.await
.expect("the harness publishes");

tb.advance(Duration::from_secs(4))
.await
.expect("the clock moves");
jobs.subscriber("jobs/later").assert_called_once();
tb.advance(Duration::from_secs(1))
.await
.expect("the clock moves");
let calls = jobs.subscriber("jobs/later").assert_called(2);
assert_eq!(calls.outcomes().last(), Some(&Outcome::Ack));
tb.shutdown().await.expect("the app stops");
});
}