本页尚未翻译
下面显示的是英文原文。
Testing a compio service
The compio TestApp runs a service on the test's own compio runtime, with the broker connected in process.
The test is a plain #[test] that blocks on one future of a compio::runtime::Runtime, and the calls are those of the embedded harness.
The harness runs the app as the one core of the test's runtime, and MqttBroker::compio needs no runner of its own there.
The clock is the runner's virtual clock, which advance moves.
Example
The same job, answered on compio.
- Macros
- Manual
#[test]
fn a_job_is_answered_on_compio() -> Result<(), Box<dyn Error>> {
Runtime::new()?.block_on(async {
let tb = TestApp::start(app()).await?;
let jobs = tb.broker::<MqttBroker>();
jobs.message(&Job { id: 7 })
.to("jobs/run")
.publish()
.await?;
jobs.subscriber("jobs/run")
.assert_called_once()
.with(&Job { id: 7 })
.settled(HandlerOutcome::ack());
jobs.published::<Done>("jobs/done")
.assert_called_once()
.with(&Done { id: 7 });
tb.shutdown().await?;
Ok(())
})
}
#[test]
fn a_job_is_answered_on_compio() -> Result<(), Box<dyn Error>> {
Runtime::new()?.block_on(async {
let tb = TestApp::start(app()).await?;
let jobs = tb.broker::<MqttBroker>();
jobs.message(&Job { id: 7 })
.to("jobs/run")
.publish()
.await?;
jobs.subscriber("jobs/run")
.assert_called_once()
.with(&Job { id: 7 })
.settled(HandlerOutcome::ack());
jobs.published::<Done>("jobs/done")
.assert_called_once()
.with(&Done { id: 7 });
tb.shutdown().await?;
Ok(())
})
}