Skip to content

Commit b8318fa

Browse files
authored
task: add tests for spawn_local in panic scenarios (#7694)
Signed-off-by: ADD-SP <[email protected]>
1 parent acfdb87 commit b8318fa

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

tokio-util/tests/task_join_map.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,24 @@ async fn duplicate_keys_drop() {
438438
mod spawn_local {
439439
use super::*;
440440

441+
#[test]
442+
#[should_panic(
443+
expected = "`spawn_local` called from outside of a `task::LocalSet` or `runtime::LocalRuntime`"
444+
)]
445+
fn panic_outside_any_runtime() {
446+
let mut map = JoinMap::new();
447+
map.spawn_local((), async {});
448+
}
449+
450+
#[tokio::test(flavor = "multi_thread")]
451+
#[should_panic(
452+
expected = "`spawn_local` called from outside of a `task::LocalSet` or `runtime::LocalRuntime`"
453+
)]
454+
async fn panic_in_multi_thread_runtime() {
455+
let mut map = JoinMap::new();
456+
map.spawn_local((), async {});
457+
}
458+
441459
#[cfg(tokio_unstable)]
442460
mod local_runtime {
443461
use super::*;

tokio-util/tests/task_join_queue.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,3 +359,21 @@ async fn test_join_queue_try_join_next_with_id_disabled_coop() {
359359
assert_eq!(count, TASK_NUM);
360360
assert_eq!(joined, spawned);
361361
}
362+
363+
#[test]
364+
#[should_panic(
365+
expected = "`spawn_local` called from outside of a `task::LocalSet` or `runtime::LocalRuntime`"
366+
)]
367+
fn spawn_local_panic_outside_any_runtime() {
368+
let mut queue = JoinQueue::new();
369+
queue.spawn_local(async {});
370+
}
371+
372+
#[tokio::test(flavor = "multi_thread")]
373+
#[should_panic(
374+
expected = "`spawn_local` called from outside of a `task::LocalSet` or `runtime::LocalRuntime`"
375+
)]
376+
async fn spawn_local_panic_in_multi_thread_runtime() {
377+
let mut queue = JoinQueue::new();
378+
queue.spawn_local(async {});
379+
}

tokio-util/tests/task_tracker.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,24 @@ mod spawn {
213213
mod spawn_local {
214214
use super::*;
215215

216+
#[test]
217+
#[should_panic(
218+
expected = "`spawn_local` called from outside of a `task::LocalSet` or `runtime::LocalRuntime`"
219+
)]
220+
fn panic_outside_any_runtime() {
221+
let tracker = TaskTracker::new();
222+
tracker.spawn_local(async {});
223+
}
224+
225+
#[tokio::test(flavor = "multi_thread")]
226+
#[should_panic(
227+
expected = "`spawn_local` called from outside of a `task::LocalSet` or `runtime::LocalRuntime`"
228+
)]
229+
async fn panic_in_multi_thread_runtime() {
230+
let tracker = TaskTracker::new();
231+
tracker.spawn_local(async {});
232+
}
233+
216234
/// Spawn several tasks, and then close the [`TaskTracker`].
217235
#[tokio::test(flavor = "local")]
218236
async fn spawn_then_close() {

tokio/tests/task_join_set.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,24 @@ async fn try_join_next_with_id() {
407407
mod spawn_local {
408408
use super::*;
409409

410+
#[test]
411+
#[should_panic(
412+
expected = "`spawn_local` called from outside of a `task::LocalSet` or `runtime::LocalRuntime`"
413+
)]
414+
fn panic_outside_any_runtime() {
415+
let mut set = JoinSet::new();
416+
set.spawn_local(async {});
417+
}
418+
419+
#[tokio::test(flavor = "multi_thread")]
420+
#[should_panic(
421+
expected = "`spawn_local` called from outside of a `task::LocalSet` or `runtime::LocalRuntime`"
422+
)]
423+
async fn panic_in_multi_thread_runtime() {
424+
let mut set = JoinSet::new();
425+
set.spawn_local(async {});
426+
}
427+
410428
#[cfg(tokio_unstable)]
411429
mod local_runtime {
412430
use super::*;

0 commit comments

Comments
 (0)