Skip to content

Commit 1b00867

Browse files
authored
Merge pull request #74 from palfrey/optional-async
Feature to disable the async work
2 parents d108a62 + 0fb75d6 commit 1b00867

File tree

7 files changed

+50
-24
lines changed

7 files changed

+50
-24
lines changed

serial_test/Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,22 @@ document-features = { version = "0.2", optional = true }
1919
log = { version = "0.4", optional = true }
2020
futures = { version = "^0.3", default_features = false, features = [
2121
"executor",
22-
] }
22+
], optional = true}
2323
dashmap = { version = "5"}
2424

2525
[dev-dependencies]
2626
itertools = "0.10"
2727
tokio = { version = "^1.17", features = ["macros", "rt"] }
2828

2929
[features]
30-
default = ["logging"]
30+
default = ["logging", "async"]
3131

3232
## Switches on debug logging (and requires the `log` package)
3333
logging = ["log"]
3434

35+
## Enables async features
36+
async = ["futures"]
37+
3538
## The file_locks feature unlocks the `file_serial`/`file_parallel` macros
3639
file_locks = ["fslock"]
3740

serial_test/src/lib.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,26 +64,27 @@ mod parallel_file_lock;
6464
#[cfg(feature = "file_locks")]
6565
mod serial_file_lock;
6666

67-
pub use parallel_code_lock::{
68-
local_async_parallel_core, local_async_parallel_core_with_return, local_parallel_core,
69-
local_parallel_core_with_return,
70-
};
71-
pub use serial_code_lock::{
72-
local_async_serial_core, local_async_serial_core_with_return, local_serial_core,
73-
local_serial_core_with_return,
74-
};
67+
#[cfg(feature = "async")]
68+
pub use parallel_code_lock::{local_async_parallel_core, local_async_parallel_core_with_return};
69+
70+
pub use parallel_code_lock::{local_parallel_core, local_parallel_core_with_return};
71+
72+
#[cfg(feature = "async")]
73+
pub use serial_code_lock::{local_async_serial_core, local_async_serial_core_with_return};
74+
75+
pub use serial_code_lock::{local_serial_core, local_serial_core_with_return};
76+
77+
#[cfg(all(feature = "file_locks", feature = "async"))]
78+
pub use serial_file_lock::{fs_async_serial_core, fs_async_serial_core_with_return};
7579

7680
#[cfg(feature = "file_locks")]
77-
pub use serial_file_lock::{
78-
fs_async_serial_core, fs_async_serial_core_with_return, fs_serial_core,
79-
fs_serial_core_with_return,
80-
};
81+
pub use serial_file_lock::{fs_serial_core, fs_serial_core_with_return};
82+
83+
#[cfg(all(feature = "file_locks", feature = "async"))]
84+
pub use parallel_file_lock::{fs_async_parallel_core, fs_async_parallel_core_with_return};
8185

8286
#[cfg(feature = "file_locks")]
83-
pub use parallel_file_lock::{
84-
fs_async_parallel_core, fs_async_parallel_core_with_return, fs_parallel_core,
85-
fs_parallel_core_with_return,
86-
};
87+
pub use parallel_file_lock::{fs_parallel_core, fs_parallel_core_with_return};
8788

8889
// Re-export #[serial/parallel].
8990
pub use serial_test_derive::{parallel, serial};

serial_test/src/parallel_code_lock.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![allow(clippy::await_holding_lock)]
22

33
use crate::code_lock::{check_new_key, LOCKS};
4+
#[cfg(feature = "async")]
45
use futures::FutureExt;
56
use std::{panic, time::Duration};
67

@@ -40,6 +41,7 @@ pub fn local_parallel_core(name: &str, max_wait: Option<Duration>, function: fn(
4041
}
4142

4243
#[doc(hidden)]
44+
#[cfg(feature = "async")]
4345
pub async fn local_async_parallel_core_with_return<E>(
4446
name: &str,
4547
max_wait: Option<Duration>,
@@ -60,6 +62,7 @@ pub async fn local_async_parallel_core_with_return<E>(
6062
}
6163

6264
#[doc(hidden)]
65+
#[cfg(feature = "async")]
6366
pub async fn local_async_parallel_core(
6467
name: &str,
6568
max_wait: Option<Duration>,
@@ -78,10 +81,10 @@ pub async fn local_async_parallel_core(
7881

7982
#[cfg(test)]
8083
mod tests {
81-
use crate::{
82-
code_lock::LOCKS, local_async_parallel_core, local_async_parallel_core_with_return,
83-
local_parallel_core, local_parallel_core_with_return,
84-
};
84+
#[cfg(feature = "async")]
85+
use crate::{local_async_parallel_core, local_async_parallel_core_with_return};
86+
87+
use crate::{code_lock::LOCKS, local_parallel_core, local_parallel_core_with_return};
8588
use std::{io::Error, panic};
8689

8790
#[test]
@@ -122,6 +125,7 @@ mod tests {
122125
}
123126

124127
#[tokio::test]
128+
#[cfg(feature = "async")]
125129
async fn unlock_on_assert_async_without_return() {
126130
async fn demo_assert() {
127131
assert!(false);
@@ -146,6 +150,7 @@ mod tests {
146150
}
147151

148152
#[tokio::test]
153+
#[cfg(feature = "async")]
149154
async fn unlock_on_assert_async_with_return() {
150155
async fn demo_assert() -> Result<(), Error> {
151156
assert!(false);

serial_test/src/parallel_file_lock.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::{panic, time::Duration};
22

3+
#[cfg(feature = "async")]
34
use futures::FutureExt;
45

56
use crate::file_lock::make_lock_for_name_and_path;
@@ -40,6 +41,7 @@ pub fn fs_parallel_core_with_return<E>(
4041
}
4142

4243
#[doc(hidden)]
44+
#[cfg(feature = "async")]
4345
pub async fn fs_async_parallel_core_with_return<E>(
4446
name: &str,
4547
_max_wait: Option<Duration>,
@@ -58,6 +60,7 @@ pub async fn fs_async_parallel_core_with_return<E>(
5860
}
5961

6062
#[doc(hidden)]
63+
#[cfg(feature = "async")]
6164
pub async fn fs_async_parallel_core(
6265
name: &str,
6366
_max_wait: Option<Duration>,
@@ -74,10 +77,12 @@ pub async fn fs_async_parallel_core(
7477

7578
#[cfg(test)]
7679
mod tests {
80+
#[cfg(feature = "async")]
81+
use crate::{fs_async_parallel_core, fs_async_parallel_core_with_return};
82+
7783
use crate::{
7884
file_lock::{path_for_name, Lock},
79-
fs_async_parallel_core, fs_async_parallel_core_with_return, fs_parallel_core,
80-
fs_parallel_core_with_return,
85+
fs_parallel_core, fs_parallel_core_with_return,
8186
};
8287
use std::{io::Error, panic};
8388

@@ -120,6 +125,7 @@ mod tests {
120125
}
121126

122127
#[tokio::test]
128+
#[cfg(feature = "async")]
123129
async fn unlock_on_assert_async_without_return() {
124130
let lock_path = path_for_name("unlock_on_assert_async_without_return");
125131
async fn demo_assert() {
@@ -145,6 +151,7 @@ mod tests {
145151
}
146152

147153
#[tokio::test]
154+
#[cfg(feature = "async")]
148155
async fn unlock_on_assert_async_with_return() {
149156
let lock_path = path_for_name("unlock_on_assert_async_with_return");
150157

serial_test/src/serial_code_lock.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub fn local_serial_core(name: &str, max_wait: Option<Duration>, function: fn())
2828
}
2929

3030
#[doc(hidden)]
31+
#[cfg(feature = "async")]
3132
pub async fn local_async_serial_core_with_return<E>(
3233
name: &str,
3334
max_wait: Option<Duration>,
@@ -42,6 +43,7 @@ pub async fn local_async_serial_core_with_return<E>(
4243
}
4344

4445
#[doc(hidden)]
46+
#[cfg(feature = "async")]
4547
pub async fn local_async_serial_core(
4648
name: &str,
4749
max_wait: Option<Duration>,

serial_test/src/serial_file_lock.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub fn fs_serial_core_with_return<E>(
2525
}
2626

2727
#[doc(hidden)]
28+
#[cfg(feature = "async")]
2829
pub async fn fs_async_serial_core_with_return<E>(
2930
name: &str,
3031
_max_wait: Option<Duration>,
@@ -39,6 +40,7 @@ pub async fn fs_async_serial_core_with_return<E>(
3940
}
4041

4142
#[doc(hidden)]
43+
#[cfg(feature = "async")]
4244
pub async fn fs_async_serial_core(
4345
name: &str,
4446
_max_wait: Option<Duration>,

serial_test_derive/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,9 @@ where
380380
{
381381
let ast: syn::ItemFn = syn::parse2(input).unwrap();
382382
let asyncness = ast.sig.asyncness;
383+
if asyncness.is_some() && cfg!(not(feature = "async")) {
384+
panic!("async testing attempted with async feature disabled in serial_test!");
385+
}
383386
let name = ast.sig.ident;
384387
let return_type = match ast.sig.output {
385388
syn::ReturnType::Default => None,
@@ -577,6 +580,7 @@ mod tests {
577580
}
578581

579582
#[test]
583+
#[cfg(feature = "async")]
580584
fn test_serial_async() {
581585
let attrs = proc_macro2::TokenStream::new();
582586
let input = quote! {
@@ -592,6 +596,7 @@ mod tests {
592596
}
593597

594598
#[test]
599+
#[cfg(feature = "async")]
595600
fn test_serial_async_return() {
596601
let attrs = proc_macro2::TokenStream::new();
597602
let input = quote! {
@@ -609,6 +614,7 @@ mod tests {
609614
// 1.54 needed for https://github.com/rust-lang/rust/commit/9daf546b77dbeab7754a80d7336cd8d00c6746e4 change in note message
610615
#[rustversion::since(1.54)]
611616
#[test]
617+
#[cfg(feature = "async")]
612618
fn test_serial_async_before_wrapper() {
613619
let t = trybuild::TestCases::new();
614620
t.compile_fail("tests/broken/test_serial_async_before_wrapper.rs");

0 commit comments

Comments
 (0)