From 3133af42e123b9469dad292ae3a090da915d23c5 Mon Sep 17 00:00:00 2001 From: "M.Amin Rayej" Date: Sun, 10 Mar 2024 14:07:18 +0330 Subject: [PATCH] runtime: make the `enter` example deterministic (#6351) --- tokio/src/runtime/runtime.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tokio/src/runtime/runtime.rs b/tokio/src/runtime/runtime.rs index 2bf1636d502..917c3f8ce91 100644 --- a/tokio/src/runtime/runtime.rs +++ b/tokio/src/runtime/runtime.rs @@ -368,12 +368,13 @@ impl Runtime { /// /// ``` /// use tokio::runtime::Runtime; + /// use tokio::task::JoinHandle; /// - /// fn function_that_spawns(msg: String) { + /// fn function_that_spawns(msg: String) -> JoinHandle<()> { /// // Had we not used `rt.enter` below, this would panic. /// tokio::spawn(async move { /// println!("{}", msg); - /// }); + /// }) /// } /// /// fn main() { @@ -383,7 +384,10 @@ impl Runtime { /// /// // By entering the context, we tie `tokio::spawn` to this executor. /// let _guard = rt.enter(); - /// function_that_spawns(s); + /// let handle = function_that_spawns(s); + /// + /// // Wait for the task before we end the test. + /// rt.block_on(handle).unwrap(); /// } /// ``` pub fn enter(&self) -> EnterGuard<'_> {