Skip to content

Commit

Permalink
runtime: make the enter example deterministic (#6351)
Browse files Browse the repository at this point in the history
  • Loading branch information
maminrayej committed Mar 10, 2024
1 parent 1f924f9 commit 3133af4
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions tokio/src/runtime/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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<'_> {
Expand Down

0 comments on commit 3133af4

Please sign in to comment.