-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Droppable Future instead of TaskMetadata (#4)
- TaskMetadata does not Drop when using tokio::select - Added tokio integration tests for join! and select!
- Loading branch information
Showing
5 changed files
with
174 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use std::{future::Future, pin::Pin}; | ||
|
||
use pin_project::{pin_project, pinned_drop}; | ||
|
||
#[pin_project(PinnedDrop)] | ||
pub struct DroppableFuture<F, D> | ||
where | ||
F: Future, | ||
D: Fn(), | ||
{ | ||
#[pin] | ||
future: F, | ||
on_drop: D, | ||
} | ||
|
||
impl<F, D> DroppableFuture<F, D> | ||
where | ||
F: Future, | ||
D: Fn(), | ||
{ | ||
pub fn new(future: F, on_drop: D) -> Self { | ||
Self { future, on_drop } | ||
} | ||
} | ||
|
||
impl<F, D> Future for DroppableFuture<F, D> | ||
where | ||
F: Future, | ||
D: Fn(), | ||
{ | ||
type Output = F::Output; | ||
|
||
fn poll( | ||
self: std::pin::Pin<&mut Self>, | ||
cx: &mut std::task::Context<'_>, | ||
) -> std::task::Poll<Self::Output> { | ||
let this = self.project(); | ||
this.future.poll(cx) | ||
} | ||
} | ||
|
||
#[pinned_drop] | ||
impl<F, D> PinnedDrop for DroppableFuture<F, D> | ||
where | ||
F: Future, | ||
D: Fn(), | ||
{ | ||
fn drop(self: Pin<&mut Self>) { | ||
(self.on_drop)(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
mod droppable_future; | ||
use droppable_future::*; | ||
|
||
mod task_identifier; | ||
pub use task_identifier::*; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use ticked_async_executor::TickedAsyncExecutor; | ||
|
||
#[test] | ||
fn test_tokio_join() { | ||
let executor = TickedAsyncExecutor::default(); | ||
|
||
let (tx1, mut rx1) = tokio::sync::mpsc::channel::<usize>(1); | ||
let (tx2, mut rx2) = tokio::sync::mpsc::channel::<usize>(1); | ||
executor | ||
.spawn("ThreadedFuture", async move { | ||
let (a, b) = tokio::join!(rx1.recv(), rx2.recv()); | ||
assert_eq!(a.unwrap(), 10); | ||
assert_eq!(b.unwrap(), 20); | ||
}) | ||
.detach(); | ||
|
||
let (tx3, mut rx3) = tokio::sync::mpsc::channel::<usize>(1); | ||
let (tx4, mut rx4) = tokio::sync::mpsc::channel::<usize>(1); | ||
executor | ||
.spawn("LocalFuture", async move { | ||
let (a, b) = tokio::join!(rx3.recv(), rx4.recv()); | ||
assert_eq!(a.unwrap(), 10); | ||
assert_eq!(b.unwrap(), 20); | ||
}) | ||
.detach(); | ||
|
||
tx1.try_send(10).unwrap(); | ||
tx3.try_send(10).unwrap(); | ||
for _ in 0..10 { | ||
executor.tick(); | ||
} | ||
tx2.try_send(20).unwrap(); | ||
tx4.try_send(20).unwrap(); | ||
|
||
while executor.num_tasks() != 0 { | ||
executor.tick(); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_tokio_select() { | ||
let executor = TickedAsyncExecutor::default(); | ||
|
||
let (tx1, mut rx1) = tokio::sync::mpsc::channel::<usize>(1); | ||
let (_tx2, mut rx2) = tokio::sync::mpsc::channel::<usize>(1); | ||
executor | ||
.spawn("ThreadedFuture", async move { | ||
tokio::select! { | ||
data = rx1.recv() => { | ||
assert_eq!(data.unwrap(), 10); | ||
} | ||
_ = rx2.recv() => {} | ||
} | ||
}) | ||
.detach(); | ||
|
||
let (tx3, mut rx3) = tokio::sync::mpsc::channel::<usize>(1); | ||
let (_tx4, mut rx4) = tokio::sync::mpsc::channel::<usize>(1); | ||
executor | ||
.spawn("LocalFuture", async move { | ||
tokio::select! { | ||
data = rx3.recv() => { | ||
assert_eq!(data.unwrap(), 10); | ||
} | ||
_ = rx4.recv() => {} | ||
} | ||
}) | ||
.detach(); | ||
|
||
for _ in 0..10 { | ||
executor.tick(); | ||
} | ||
|
||
tx1.try_send(10).unwrap(); | ||
tx3.try_send(10).unwrap(); | ||
while executor.num_tasks() != 0 { | ||
executor.tick(); | ||
} | ||
} |