feat: make graceful shutdown clone

This commit is contained in:
Matthias Seitz
2024-07-11 21:30:32 +02:00
parent c31d69683d
commit ee531cd7c0
2 changed files with 30 additions and 0 deletions

View File

@@ -712,6 +712,30 @@ mod tests {
handle.block_on(shutdown);
}
#[test]
fn test_manager_graceful_shutdown_clone() {
let runtime = tokio::runtime::Runtime::new().unwrap();
let handle = runtime.handle().clone();
let manager = TaskManager::new(handle);
let executor = manager.executor();
let val = Arc::new(AtomicBool::new(false));
let c = val.clone();
executor.spawn_critical_with_graceful_shutdown_signal(
"grace clone",
|shutdown| async move {
let guard2 = shutdown.clone();
let _guard = shutdown.await;
tokio::time::sleep(Duration::from_millis(200)).await;
drop(guard2);
c.store(true, Ordering::Relaxed);
},
);
manager.graceful_shutdown();
assert!(val.load(Ordering::Relaxed));
}
#[test]
fn test_manager_graceful_shutdown() {
let runtime = tokio::runtime::Runtime::new().unwrap();

View File

@@ -69,6 +69,12 @@ impl Drop for GracefulShutdownGuard {
}
}
impl Clone for GracefulShutdownGuard {
fn clone(&self) -> Self {
Self::new(Arc::clone(&self.0))
}
}
/// A Future that resolves when the shutdown event has been fired.
#[derive(Debug, Clone)]
pub struct Shutdown(Shared<oneshot::Receiver<()>>);