From d23a838732077122aee71530f15d3c0bf69f11bb Mon Sep 17 00:00:00 2001 From: Mattia Pitossi Date: Wed, 15 Oct 2025 21:09:46 +0200 Subject: [PATCH] runtime: add tests for spawn local on multi and current runtimes (#7687) --- tokio/tests/rt_local.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tokio/tests/rt_local.rs b/tokio/tests/rt_local.rs index 4eb88d48a..3393482c3 100644 --- a/tokio/tests/rt_local.rs +++ b/tokio/tests/rt_local.rs @@ -111,6 +111,28 @@ fn test_spawn_local_from_guard_other_thread() { spawn_local(async {}); } +#[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(); + + rt.block_on(async move { + spawn_local(async {}); + }) +} + +#[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(); + + rt.block_on(async move { + spawn_local(async {}); + }) +} + fn rt() -> tokio::runtime::LocalRuntime { tokio::runtime::Builder::new_current_thread() .enable_all()