From 56aeeb2903a458f77991fbc1bb00b0266e4da162 Mon Sep 17 00:00:00 2001 From: "M.Amin Rayej" Date: Fri, 16 Feb 2024 14:30:24 +0330 Subject: [PATCH 1/4] make the example deterministic --- tokio/src/runtime/runtime.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tokio/src/runtime/runtime.rs b/tokio/src/runtime/runtime.rs index a8a95428095..5c5b8f68ef3 100644 --- a/tokio/src/runtime/runtime.rs +++ b/tokio/src/runtime/runtime.rs @@ -367,12 +367,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() { @@ -382,7 +383,13 @@ 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); + /// + /// // When a `Runtime` instance is dropped, a shutdown signal will be + /// // delivered to all tasks. So the spawned task may get canceled before + /// // it gets to print the message. To force a deterministic output for this + /// // example, we will wait until the task is finished. + /// while !handle.is_finished() {} /// } /// ``` pub fn enter(&self) -> EnterGuard<'_> { From 0a9ee2d641a444b0e426dbb5fbdf28c0dedfd157 Mon Sep 17 00:00:00 2001 From: "M.Amin Rayej" Date: Fri, 16 Feb 2024 18:07:49 +0330 Subject: [PATCH 2/4] use block_on --- tokio/src/runtime/runtime.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio/src/runtime/runtime.rs b/tokio/src/runtime/runtime.rs index 5c5b8f68ef3..4fe88e38680 100644 --- a/tokio/src/runtime/runtime.rs +++ b/tokio/src/runtime/runtime.rs @@ -389,7 +389,7 @@ impl Runtime { /// // delivered to all tasks. So the spawned task may get canceled before /// // it gets to print the message. To force a deterministic output for this /// // example, we will wait until the task is finished. - /// while !handle.is_finished() {} + /// let _ = rt.block_on(handle); /// } /// ``` pub fn enter(&self) -> EnterGuard<'_> { From d4d40ab3ca2947032d8953c54e7bc90fb9a141d9 Mon Sep 17 00:00:00 2001 From: "M.Amin Rayej" Date: Sun, 10 Mar 2024 00:28:43 +0330 Subject: [PATCH 3/4] Update tokio/src/runtime/runtime.rs Co-authored-by: Alice Ryhl --- tokio/src/runtime/runtime.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio/src/runtime/runtime.rs b/tokio/src/runtime/runtime.rs index 4fe88e38680..36ee37c0dd6 100644 --- a/tokio/src/runtime/runtime.rs +++ b/tokio/src/runtime/runtime.rs @@ -389,7 +389,7 @@ impl Runtime { /// // delivered to all tasks. So the spawned task may get canceled before /// // it gets to print the message. To force a deterministic output for this /// // example, we will wait until the task is finished. - /// let _ = rt.block_on(handle); + /// rt.block_on(handle).unwrap(); /// } /// ``` pub fn enter(&self) -> EnterGuard<'_> { From a7cdf70a691b9ff4091645502d78e75a939badff Mon Sep 17 00:00:00 2001 From: "M.Amin Rayej" Date: Sun, 10 Mar 2024 00:28:50 +0330 Subject: [PATCH 4/4] Update tokio/src/runtime/runtime.rs Co-authored-by: Alice Ryhl --- tokio/src/runtime/runtime.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tokio/src/runtime/runtime.rs b/tokio/src/runtime/runtime.rs index 36ee37c0dd6..bf3f5ea8838 100644 --- a/tokio/src/runtime/runtime.rs +++ b/tokio/src/runtime/runtime.rs @@ -385,10 +385,7 @@ impl Runtime { /// let _guard = rt.enter(); /// let handle = function_that_spawns(s); /// - /// // When a `Runtime` instance is dropped, a shutdown signal will be - /// // delivered to all tasks. So the spawned task may get canceled before - /// // it gets to print the message. To force a deterministic output for this - /// // example, we will wait until the task is finished. + /// // Wait for the task before we end the test. /// rt.block_on(handle).unwrap(); /// } /// ```