Skip to content

Commit d25778f

Browse files
task: add tests for task::Builder::spawn_local (#7697)
1 parent b8318fa commit d25778f

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

tokio/src/task/builder.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,20 +118,23 @@ impl<'a> Builder<'a> {
118118
})
119119
}
120120

121-
/// Spawns `!Send` a task on the current [`LocalSet`] with this builder's
122-
/// settings.
121+
/// Spawns `!Send` a task on the current [`LocalSet`] or [`LocalRuntime`] with
122+
/// this builder's settings.
123123
///
124124
/// The spawned future will be run on the same thread that called `spawn_local`.
125-
/// This may only be called from the context of a [local task set][`LocalSet`].
125+
/// This may only be called from the context of a [local task set][`LocalSet`]
126+
/// or a [`LocalRuntime`].
126127
///
127128
/// # Panics
128129
///
129-
/// This function panics if called outside of a [local task set][`LocalSet`].
130+
/// This function panics if called outside of a [local task set][`LocalSet`]
131+
/// or a [`LocalRuntime`].
130132
///
131133
/// See [`task::spawn_local`] for more details.
132134
///
133135
/// [`task::spawn_local`]: crate::task::spawn_local
134136
/// [`LocalSet`]: crate::task::LocalSet
137+
/// [`LocalRuntime`]: crate::runtime::LocalRuntime
135138
#[track_caller]
136139
pub fn spawn_local<Fut>(self, future: Fut) -> io::Result<JoinHandle<Fut::Output>>
137140
where

tokio/src/task/join_set.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl<T: 'static> JoinSet<T> {
177177
///
178178
/// # Panics
179179
///
180-
/// This method panics if it is called outside of a `LocalSet`or `LocalRuntime`.
180+
/// This method panics if it is called outside of a `LocalSet` or `LocalRuntime`.
181181
///
182182
/// [`LocalSet`]: crate::task::LocalSet
183183
/// [`LocalRuntime`]: crate::runtime::LocalRuntime
@@ -748,18 +748,19 @@ impl<'a, T: 'static> Builder<'a, T> {
748748
.insert(self.builder.spawn_blocking_on(f, handle)?))
749749
}
750750

751-
/// Spawn the provided task on the current [`LocalSet`] with this builder's
752-
/// settings, and store it in the [`JoinSet`].
751+
/// Spawn the provided task on the current [`LocalSet`] or [`LocalRuntime`]
752+
/// with this builder's settings, and store it in the [`JoinSet`].
753753
///
754754
/// # Returns
755755
///
756756
/// An [`AbortHandle`] that can be used to remotely cancel the task.
757757
///
758758
/// # Panics
759759
///
760-
/// This method panics if it is called outside of a `LocalSet`.
760+
/// This method panics if it is called outside of a `LocalSet` or `LocalRuntime`.
761761
///
762762
/// [`LocalSet`]: crate::task::LocalSet
763+
/// [`LocalRuntime`]: crate::runtime::LocalRuntime
763764
/// [`AbortHandle`]: crate::task::AbortHandle
764765
#[track_caller]
765766
pub fn spawn_local<F>(self, future: F) -> std::io::Result<AbortHandle>

tokio/tests/task_builder.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,34 @@ async fn spawn_with_name() {
1414
assert_eq!(result.unwrap(), "task executed");
1515
}
1616

17+
#[tokio::test(flavor = "local")]
18+
async fn spawn_local_on_local_runtime() {
19+
let result = Builder::new()
20+
.spawn_local(async { "task executed" })
21+
.unwrap()
22+
.await;
23+
24+
assert_eq!(result.unwrap(), "task executed");
25+
}
26+
27+
#[tokio::test]
28+
#[should_panic = "`spawn_local` called from outside of a `task::LocalSet` or `runtime::LocalRuntime`"]
29+
async fn spawn_local_panics_outside_local_set_or_local_runtime() {
30+
let _ = Builder::new()
31+
.spawn_local(async { "task executed" })
32+
.unwrap()
33+
.await;
34+
}
35+
36+
#[tokio::test(flavor = "multi_thread")]
37+
#[should_panic = "`spawn_local` called from outside of a `task::LocalSet` or `runtime::LocalRuntime`"]
38+
async fn spawn_local_panics_in_multi_thread_runtime() {
39+
let _ = Builder::new()
40+
.spawn_local(async { "task executed" })
41+
.unwrap()
42+
.await;
43+
}
44+
1745
#[tokio::test]
1846
async fn spawn_blocking_with_name() {
1947
let result = Builder::new()

0 commit comments

Comments
 (0)