Skip to content

Commit

Permalink
handle task drop
Browse files Browse the repository at this point in the history
  • Loading branch information
stepantubanov committed Jun 9, 2024
1 parent b42b9c0 commit bfdb991
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/enabled/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ pin_project! {
},
Done,
}

impl<'a, F> PinnedDrop for ParcheckTaskFuture<'a, F> {
fn drop(this: Pin<&mut Self>) {
let proj = this.project();
if let ParcheckTaskFutureProj::Controlled { task, .. } = proj {
task.send_event(TaskEvent::TaskFinished);
}
}
}
}

#[cfg(feature = "tracing")]
Expand Down
33 changes: 32 additions & 1 deletion tests/examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ use std::collections::HashMap;
use std::future;
use std::mem::size_of_val;
use std::sync::Mutex;
use std::time::Duration;

use parcheck::Trace;
use tokio::sync::oneshot;

struct Observer {
trace: Mutex<String>,
Expand Down Expand Up @@ -136,7 +138,7 @@ async fn does_not_double_panic() {

#[tokio::test]
#[should_panic(
expected = "operation 'outer' already in progress for task 'reentrant' (operation at tests/examples/basic.rs:146)"
expected = "operation 'outer' already in progress for task 'reentrant' (operation at tests/examples/basic.rs:148)"
)]
async fn detects_reentrant_task() {
parcheck::runner()
Expand Down Expand Up @@ -219,3 +221,32 @@ async fn futures_arent_too_large() {
"task future size: {task_size} -> {wrapped_task_size}"
);
}

#[tokio::test]
async fn handles_cancellation() {
tokio::time::timeout(Duration::from_millis(100), async {
parcheck::runner()
.run(["dropped_task"], || async move {
let (tx, rx) = oneshot::channel();
let task = parcheck::task!("dropped_task", {
async {
parcheck::operation!("op", {
async {
tx.send(()).unwrap();
future::pending::<()>().await;
}
})
.await;
}
});

tokio::select! {
_ = task => unreachable!(),
_ = rx => {},
}
})
.await;
})
.await
.unwrap();
}

0 comments on commit bfdb991

Please sign in to comment.