let res = rt.block_on(asyncmove { rx.await.unwrap() });
assert_eq!(res, 5);
}
#[test] #[cfg_attr(target_family = "wasm", ignore)] // threads not supported fn test_spawn_from_guard_other_thread() { let (tx, rx) = std::sync::mpsc::channel();
std::thread::spawn(move || { let rt = rt(); let handle = rt.handle().clone();
tx.send(handle).unwrap();
});
let handle = rx.recv().unwrap();
let _guard = handle.enter();
tokio::spawn(async {});
}
#[test] #[should_panic = "Local tasks can only be spawned on a LocalRuntime from the thread the runtime was created on"] #[cfg_attr(target_family = "wasm", ignore)] // threads not supported fn test_spawn_local_from_guard_other_thread() { let (tx, rx) = std::sync::mpsc::channel();
std::thread::spawn(move || { let rt = rt(); let handle = rt.handle().clone();
tx.send(handle).unwrap();
});
let handle = rx.recv().unwrap();
let _guard = handle.enter();
spawn_local(async {});
}
// This test guarantees that **`tokio::task::spawn_local` panics** when it is invoked // from a thread that is *not* running the `LocalRuntime` / `LocalSet` to which // the task would belong. // The test creates a `LocalRuntime` and `LocalSet`, drives the `LocalSet` on the `LocalRuntime`'s thread, // then spawns a **separate OS thread** and tries to call // `tokio::task::spawn_local` there. `std::panic::catch_unwind` is then used // to capture the panic and to assert that it indeed occurs. #[test] #[cfg_attr(target_family = "wasm", ignore)] // threads not supported fn test_spawn_local_panic() { let rt = rt(); let local = LocalSet::new();
rt.block_on(local.run_until(async { let thread_result = std::thread::spawn(|| { let panic_result = panic::catch_unwind(|| { let _jh = tokio::task::spawn_local(async {
println!("you will never see this line");
});
});
assert!(panic_result.is_err(), "Expected panic, but none occurred");
})
.join();
assert!(thread_result.is_ok(), "Thread itself panicked unexpectedly");
}));
}
#[test] #[should_panic = "`spawn_local` called from outside of a `task::LocalSet` or `runtime::LocalRuntime`"] fn test_spawn_local_in_current_thread_runtime() { let rt = tokio::runtime::Builder::new_current_thread()
.build()
.unwrap();
#[test] #[should_panic = "`spawn_local` called from outside of a `task::LocalSet` or `runtime::LocalRuntime`"] fn test_spawn_local_in_multi_thread_runtime() { let rt = tokio::runtime::Builder::new_multi_thread().build().unwrap();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.