Skip to content

Commit

Permalink
hide task fn and use macro instead
Browse files Browse the repository at this point in the history
  • Loading branch information
stepantubanov committed May 31, 2024
1 parent d7ec779 commit ac10083
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 159 deletions.
21 changes: 0 additions & 21 deletions src/disabled/mod.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
pub(crate) mod task {
use std::future::Future;

#[inline(always)]
pub fn task<F: Future>(_name: &str, f: F) -> F {
f
}
}

pub(crate) mod operation {
use crate::ParcheckLock;
use std::future::Future;

#[inline(always)]
pub async fn operation<F: Future>(_locks: Vec<ParcheckLock>, f: F) -> F::Output {
f.await
}

pub struct OperationMetadata;
}

#[macro_export]
macro_rules! cfg_if {
($code:block) => {};
Expand Down
4 changes: 2 additions & 2 deletions src/enabled/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ macro_rules! cfg_if {

#[macro_export]
macro_rules! task {
($name:expr, $fut:expr) => {
$crate::task(&*$name, $fut)
($name:expr, { $fut:expr }) => {
$crate::private::task(&*$name, $fut)
};
}

Expand Down
12 changes: 5 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
#[cfg(feature = "enable")]
mod enabled;
#[cfg(feature = "enable")]
use enabled as api;

#[cfg(not(feature = "enable"))]
mod disabled;
#[cfg(not(feature = "enable"))]
use disabled as api;

#[cfg(feature = "enable")]
#[doc(hidden)]
pub mod private {
pub use super::api::operation::{operation, OperationMetadata};
pub use super::enabled::{
operation::{operation, OperationMetadata},
task::task,
};
}

pub use api::task::task;

#[cfg(feature = "enable")]
pub use enabled::runner::{runner, Runner, Trace};

Expand Down
84 changes: 46 additions & 38 deletions tests/examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,27 @@ impl Observer {
}

async fn execute(&self, process: &str) {
parcheck::task!(format!("execute:{process}"), async {
parcheck::operation!("append:1", {
async {
self.append(process);
}
})
.await;
parcheck::operation!("append:2", {
async {
self.append(process);
}
})
.await;
parcheck::operation!("append:3", {
async {
self.append(process);
}
})
.await;
parcheck::task!(format!("execute:{process}"), {
async {
parcheck::operation!("append:1", {
async {
self.append(process);
}
})
.await;
parcheck::operation!("append:2", {
async {
self.append(process);
}
})
.await;
parcheck::operation!("append:3", {
async {
self.append(process);
}
})
.await;
}
})
.await;
}
Expand Down Expand Up @@ -96,7 +98,7 @@ async fn no_tasks() {
async fn one_empty_task() {
parcheck::runner()
.run(["one"], || async move {
parcheck::task("one", async {}).await;
parcheck::task!("one", { async {} }).await;
})
.await;
}
Expand All @@ -106,8 +108,8 @@ async fn two_empty_tasks() {
parcheck::runner()
.run(["task_a", "task_b"], || async move {
tokio::join!(
parcheck::task("task_a", async {}),
parcheck::task("task_b", async {}),
parcheck::task!("task_a", { async {} }),
parcheck::task!("task_b", { async {} }),
);
})
.await;
Expand All @@ -119,8 +121,8 @@ async fn does_not_double_panic() {
parcheck::runner()
.run(["task_a", "task_b"], || async move {
tokio::join!(
parcheck::task("task_a", async {}),
parcheck::task("task_b", async {}),
parcheck::task!("task_a", { async {} }),
parcheck::task!("task_b", { async {} }),
);

panic!("some kind of test failure");
Expand All @@ -130,18 +132,20 @@ 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:139"
expected = "operation 'outer' already in progress for task 'reentrant' (operation at tests/examples/basic.rs:142)"
)]
async fn detects_reentrant_task() {
parcheck::runner()
.run(["reentrant"], || async move {
parcheck::task("reentrant", async {
parcheck::operation!("outer", {
async {
parcheck::operation!("inner", { async {} }).await;
}
})
.await;
parcheck::task!("reentrant", {
async {
parcheck::operation!("outer", {
async {
parcheck::operation!("inner", { async {} }).await;
}
})
.await;
}
})
.await;
})
Expand All @@ -154,9 +158,11 @@ async fn replays_prefix_trace() {
// only a prefix of an actual execution
.replay("0,0".parse().unwrap())
.run(["replays_prefix_trace"], || async {
parcheck::task("replays_prefix_trace", async {
for _ in 0..3 {
parcheck::operation!("op", { async {} }).await;
parcheck::task!("replays_prefix_trace", {
async {
for _ in 0..3 {
parcheck::operation!("op", { async {} }).await;
}
}
})
.await;
Expand All @@ -169,9 +175,11 @@ async fn replays_full_trace() {
parcheck::runner()
.replay("0,0,0".parse().unwrap())
.run(["replays_full_trace"], || async {
parcheck::task("replays_full_trace", async {
for _ in 0..3 {
parcheck::operation!("op", { async {} }).await;
parcheck::task!("replays_full_trace", {
async {
for _ in 0..3 {
parcheck::operation!("op", { async {} }).await;
}
}
})
.await;
Expand Down
48 changes: 26 additions & 22 deletions tests/examples/disabled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use parcheck::ParcheckLock;

#[tokio::test]
async fn works_when_disabled() {
let result = parcheck::task!("task", async {
parcheck::operation!("op", { async { 123 } }).await
let result = parcheck::task!("task", {
async { parcheck::operation!("op", { async { 123 } }).await }
})
.await;
assert_eq!(result, 123);

let result = parcheck::task("task", async {
parcheck::operation!("op", { async { 123 } }).await
let result = parcheck::task!("task", {
async { parcheck::operation!("op", { async { 123 } }).await }
})
.await;
assert_eq!(result, 123);
Expand All @@ -18,28 +18,32 @@ async fn works_when_disabled() {
#[tokio::test]
async fn doesnt_complain_about_unused_vars() {
let x = 123;
let result = parcheck::task!(format!("task:{x}"), async {
parcheck::operation!(
"op",
vec![ParcheckLock::AcquireExclusive {
scope: "scope".into()
}],
{ async { 123 } }
)
.await
let result = parcheck::task!(format!("task:{x}"), {
async {
parcheck::operation!(
"op",
vec![ParcheckLock::AcquireExclusive {
scope: "scope".into()
}],
{ async { 123 } }
)
.await
}
})
.await;
assert_eq!(result, 123);

let result = parcheck::task("task", async {
parcheck::operation!(
"op",
vec![ParcheckLock::AcquireExclusive {
scope: "scope".into()
}],
{ async { 123 } }
)
.await
let result = parcheck::task!("task", {
async {
parcheck::operation!(
"op",
vec![ParcheckLock::AcquireExclusive {
scope: "scope".into()
}],
{ async { 123 } }
)
.await
}
})
.await;
assert_eq!(result, 123);
Expand Down
Loading

0 comments on commit ac10083

Please sign in to comment.