diff --git a/tokio-util/tests/task_join_map.rs b/tokio-util/tests/task_join_map.rs index 3896f7617..70c33d8a9 100644 --- a/tokio-util/tests/task_join_map.rs +++ b/tokio-util/tests/task_join_map.rs @@ -438,6 +438,24 @@ async fn duplicate_keys_drop() { mod spawn_local { use super::*; + #[test] + #[should_panic( + expected = "`spawn_local` called from outside of a `task::LocalSet` or `runtime::LocalRuntime`" + )] + fn panic_outside_any_runtime() { + let mut map = JoinMap::new(); + map.spawn_local((), async {}); + } + + #[tokio::test(flavor = "multi_thread")] + #[should_panic( + expected = "`spawn_local` called from outside of a `task::LocalSet` or `runtime::LocalRuntime`" + )] + async fn panic_in_multi_thread_runtime() { + let mut map = JoinMap::new(); + map.spawn_local((), async {}); + } + #[cfg(tokio_unstable)] mod local_runtime { use super::*; diff --git a/tokio-util/tests/task_join_queue.rs b/tokio-util/tests/task_join_queue.rs index cf29260bb..a61a968f2 100644 --- a/tokio-util/tests/task_join_queue.rs +++ b/tokio-util/tests/task_join_queue.rs @@ -359,3 +359,21 @@ async fn test_join_queue_try_join_next_with_id_disabled_coop() { assert_eq!(count, TASK_NUM); assert_eq!(joined, spawned); } + +#[test] +#[should_panic( + expected = "`spawn_local` called from outside of a `task::LocalSet` or `runtime::LocalRuntime`" +)] +fn spawn_local_panic_outside_any_runtime() { + let mut queue = JoinQueue::new(); + queue.spawn_local(async {}); +} + +#[tokio::test(flavor = "multi_thread")] +#[should_panic( + expected = "`spawn_local` called from outside of a `task::LocalSet` or `runtime::LocalRuntime`" +)] +async fn spawn_local_panic_in_multi_thread_runtime() { + let mut queue = JoinQueue::new(); + queue.spawn_local(async {}); +} diff --git a/tokio-util/tests/task_tracker.rs b/tokio-util/tests/task_tracker.rs index b51dcd233..26c935217 100644 --- a/tokio-util/tests/task_tracker.rs +++ b/tokio-util/tests/task_tracker.rs @@ -213,6 +213,24 @@ mod spawn { mod spawn_local { use super::*; + #[test] + #[should_panic( + expected = "`spawn_local` called from outside of a `task::LocalSet` or `runtime::LocalRuntime`" + )] + fn panic_outside_any_runtime() { + let tracker = TaskTracker::new(); + tracker.spawn_local(async {}); + } + + #[tokio::test(flavor = "multi_thread")] + #[should_panic( + expected = "`spawn_local` called from outside of a `task::LocalSet` or `runtime::LocalRuntime`" + )] + async fn panic_in_multi_thread_runtime() { + let tracker = TaskTracker::new(); + tracker.spawn_local(async {}); + } + /// Spawn several tasks, and then close the [`TaskTracker`]. #[tokio::test(flavor = "local")] async fn spawn_then_close() { diff --git a/tokio/tests/task_join_set.rs b/tokio/tests/task_join_set.rs index b2b0463e2..0c2de0969 100644 --- a/tokio/tests/task_join_set.rs +++ b/tokio/tests/task_join_set.rs @@ -407,6 +407,24 @@ async fn try_join_next_with_id() { mod spawn_local { use super::*; + #[test] + #[should_panic( + expected = "`spawn_local` called from outside of a `task::LocalSet` or `runtime::LocalRuntime`" + )] + fn panic_outside_any_runtime() { + let mut set = JoinSet::new(); + set.spawn_local(async {}); + } + + #[tokio::test(flavor = "multi_thread")] + #[should_panic( + expected = "`spawn_local` called from outside of a `task::LocalSet` or `runtime::LocalRuntime`" + )] + async fn panic_in_multi_thread_runtime() { + let mut set = JoinSet::new(); + set.spawn_local(async {}); + } + #[cfg(tokio_unstable)] mod local_runtime { use super::*;