Skip to content

Commit 0ac7790

Browse files
committed
✨ Add task::CloseAndWait::close_and_wait()
1 parent 672d2cf commit 0ac7790

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ ron = "0.8.1"
3131
serde = { version = "1.0.216", features = ["derive"] }
3232
thiserror = "2.0.6"
3333
tokio = { version = "1.42.0", features = ["rt"] }
34+
tokio-util = { version = "0.7.13", features = ["rt"] }
3435

3536
[dev-dependencies]
36-
tokio = { version = "1.42.0", features = ["macros"] }
37+
tokio = { version = "1.42.0", features = ["macros", "time"] }
3738

3839
[build-dependencies]
3940
version_check = "0.9.5"

README-CN.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
部分条目如下:
3333
- [`AnyRes`](https://docs.rs/est/latest/est/result/type.AnyRes.html)
3434
- [`collections::MapExt::replace_key()`](https://docs.rs/est/latest/est/collections/trait.MapExt.html#tymethod.replace_key)
35+
- [`task::CloseAndWait::close_and_wait()`](https://docs.rs/est/latest/est/task/trait.CloseAndWait.html#tymethod.close_and_wait)
3536
- [`task::TaskId`](https://docs.rs/est/latest/est/task/struct.TaskId.html)
3637
- [`thread::ThreadId`](https://docs.rs/est/latest/est/thread/struct.ThreadId.html)
3738

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Latest version: [v0.5.0](https://github.com/opensound-org/est/releases/tag/v0.5.
3232
Some of the items are as follows:
3333
- [`AnyRes`](https://docs.rs/est/latest/est/result/type.AnyRes.html)
3434
- [`collections::MapExt::replace_key()`](https://docs.rs/est/latest/est/collections/trait.MapExt.html#tymethod.replace_key)
35+
- [`task::CloseAndWait::close_and_wait()`](https://docs.rs/est/latest/est/task/trait.CloseAndWait.html#tymethod.close_and_wait)
3536
- [`task::TaskId`](https://docs.rs/est/latest/est/task/struct.TaskId.html)
3637
- [`thread::ThreadId`](https://docs.rs/est/latest/est/thread/struct.ThreadId.html)
3738

src/task.rs

+49
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use derive_more::Display;
22
use serde::{Deserialize, Serialize};
33
use std::num::NonZeroU64;
4+
use tokio_util::task::{task_tracker::TaskTrackerWaitFuture, TaskTracker};
45

56
/// A [`TaskId`](https://docs.rs/tokio/latest/tokio/task/struct.Id.html) that can be `serde`.
67
#[derive(Debug, Display, Serialize, Deserialize, Copy, Clone, Eq, PartialEq, Hash)]
@@ -13,6 +14,20 @@ impl From<tokio::task::Id> for TaskId {
1314
}
1415
}
1516

17+
/// Execute [`close`](https://docs.rs/tokio-util/latest/tokio_util/task/task_tracker/struct.TaskTracker.html#method.close)
18+
/// and [`wait`](https://docs.rs/tokio-util/latest/tokio_util/task/task_tracker/struct.TaskTracker.html#method.wait)
19+
/// for [`TaskTracker`](https://docs.rs/tokio-util/latest/tokio_util/task/task_tracker/struct.TaskTracker.html) at once.
20+
pub trait CloseAndWait {
21+
fn close_and_wait(&self) -> TaskTrackerWaitFuture;
22+
}
23+
24+
impl CloseAndWait for TaskTracker {
25+
fn close_and_wait(&self) -> TaskTrackerWaitFuture {
26+
self.close();
27+
self.wait()
28+
}
29+
}
30+
1631
#[cfg(test)]
1732
mod tests {
1833
use super::*;
@@ -22,4 +37,38 @@ mod tests {
2237
let id = tokio::spawn(async { tokio::task::id() }).await.unwrap();
2338
assert_eq!(id.to_string(), TaskId::from(id).to_string());
2439
}
40+
41+
fn tracker_spawn() -> TaskTracker {
42+
let tracker = TaskTracker::new();
43+
44+
for i in 0..3 {
45+
tracker.spawn(async move { i });
46+
}
47+
48+
tracker
49+
}
50+
51+
#[tokio::test]
52+
async fn close_and_wait() {
53+
use std::time::Duration;
54+
use tokio::time::timeout;
55+
56+
let tracker = tracker_spawn();
57+
assert!(timeout(Duration::from_secs_f64(1.5), tracker.wait())
58+
.await
59+
.is_err());
60+
61+
let tracker = tracker_spawn();
62+
tracker.close();
63+
assert!(timeout(Duration::from_secs_f64(1.5), tracker.wait())
64+
.await
65+
.is_ok());
66+
67+
let tracker = tracker_spawn();
68+
assert!(
69+
timeout(Duration::from_secs_f64(1.5), tracker.close_and_wait())
70+
.await
71+
.is_ok()
72+
);
73+
}
2574
}

0 commit comments

Comments
 (0)