下面显示的是英文原文。
shutdown
TestApp::shutdown tears the service down gracefully and returns what run would return, so a fail-fast failure fails the test.
It is the service's own shutdown, step for step.
It runs on_shutdown, stops the dispatch loops, and waits for in-flight handlers and post-settle continuations.
It then shuts the brokers down in reverse order of registration and runs after_shutdown.
The wait for handlers and continuations is bounded by the app's shutdown timeout.
A failing shutdown hook is logged, and the shutdown goes on, as in the service.
The result is Ok for a healthy run and RustStreamError::Dispatch after a fail-fast failure, such as a handler panic under the default policy.
A broker that fails to shut down returns the service's error for it too.
run_result gives the same result while the harness is still running, and assert_shut_down asserts the failure.
shutdown takes the harness by value, so nothing can be published after it.
Example
- Macros
- Manual
#[tokio::test]
async fn a_healthy_run_shuts_down_cleanly() -> Result<(), Box<dyn Error>> {
let tb = TestApp::start(app()).await?;
tb.broker::<MemoryBroker>()
.message(&Order { id: 7 })
.publish()
.await?;
tb.shutdown().await?;
Ok(())
}
#[tokio::test]
async fn a_panic_is_reported_at_shutdown() -> Result<(), Box<dyn Error>> {
let tb = TestApp::start(app()).await?;
tb.broker::<MemoryBroker>()
.message(&Order { id: 0 })
.publish()
.await?;
assert!(matches!(
tb.shutdown().await,
Err(RustStreamError::Dispatch(_))
));
Ok(())
}
#[tokio::test]
async fn a_healthy_run_shuts_down_cleanly() -> Result<(), Box<dyn Error>> {
let tb = TestApp::start(app()).await?;
tb.broker::<MemoryBroker>()
.message(&Order { id: 7 })
.publish()
.await?;
tb.shutdown().await?;
Ok(())
}
#[tokio::test]
async fn a_panic_is_reported_at_shutdown() -> Result<(), Box<dyn Error>> {
let tb = TestApp::start(app()).await?;
tb.broker::<MemoryBroker>()
.message(&Order { id: 0 })
.publish()
.await?;
assert!(matches!(
tb.shutdown().await,
Err(RustStreamError::Dispatch(_))
));
Ok(())
}