test: allow shutdown_token when #[cfg(test)]

This commit is contained in:
Joonas Koivunen
2023-12-13 22:56:31 +00:00
parent 9660282c69
commit 9790a7c2e8

View File

@@ -558,9 +558,14 @@ pub async fn shutdown_watcher() {
/// cancelled. It can however be moved to other tasks, such as `tokio::task::spawn_blocking` or
/// `tokio::task::JoinSet::spawn`.
pub fn shutdown_token() -> CancellationToken {
SHUTDOWN_TOKEN
.try_with(|t| t.clone())
.expect("shutdown_token() called in an unexpected task or thread")
let res = SHUTDOWN_TOKEN.try_with(|t| t.clone());
if cfg!(test) {
res.unwrap_or_default()
} else {
// tests need to call the same paths which need to use get the shutdown token
res.expect("shutdown_token() called in an unexpected task or thread")
}
}
/// Has the current task been requested to shut down?