Skip to content

Commit

Permalink
Handle file_lock
Browse files Browse the repository at this point in the history
Signed-off-by: Heinz N. Gies <heinz@licenser.net>
  • Loading branch information
Licenser committed Jul 13, 2022
1 parent 109b73d commit 3b97657
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
16 changes: 14 additions & 2 deletions serial_test/src/parallel_file_lock.rs
@@ -1,11 +1,16 @@
use std::panic;
use std::{panic, time::Duration};

use futures::FutureExt;

use crate::file_lock::make_lock_for_name_and_path;

#[doc(hidden)]
pub fn fs_parallel_core(name: &str, path: Option<&str>, function: fn()) {
pub fn fs_parallel_core(
name: &str,
_max_wait: Option<Duration>,
path: Option<&str>,
function: fn(),
) {
make_lock_for_name_and_path(name, path).start_parallel();
let res = panic::catch_unwind(|| {
function();
Expand All @@ -19,6 +24,7 @@ pub fn fs_parallel_core(name: &str, path: Option<&str>, function: fn()) {
#[doc(hidden)]
pub fn fs_parallel_core_with_return<E>(
name: &str,
_max_wait: Option<Duration>,
path: Option<&str>,
function: fn() -> Result<(), E>,
) -> Result<(), E> {
Expand All @@ -36,6 +42,7 @@ pub fn fs_parallel_core_with_return<E>(
#[doc(hidden)]
pub async fn fs_async_parallel_core_with_return<E>(
name: &str,
_max_wait: Option<Duration>,
path: Option<&str>,
fut: impl std::future::Future<Output = Result<(), E>> + panic::UnwindSafe,
) -> Result<(), E> {
Expand All @@ -53,6 +60,7 @@ pub async fn fs_async_parallel_core_with_return<E>(
#[doc(hidden)]
pub async fn fs_async_parallel_core(
name: &str,
_max_wait: Option<Duration>,
path: Option<&str>,
fut: impl std::future::Future<Output = ()> + panic::UnwindSafe,
) {
Expand Down Expand Up @@ -84,6 +92,7 @@ mod tests {
let _ = panic::catch_unwind(|| {
fs_parallel_core(
"unlock_on_assert_sync_without_return",
None,
Some(&lock_path),
|| {
assert!(false);
Expand All @@ -99,6 +108,7 @@ mod tests {
let _ = panic::catch_unwind(|| {
fs_parallel_core_with_return(
"unlock_on_assert_sync_with_return",
None,
Some(&lock_path),
|| -> Result<(), Error> {
assert!(false);
Expand All @@ -118,6 +128,7 @@ mod tests {
async fn call_serial_test_fn(lock_path: &str) {
fs_async_parallel_core(
"unlock_on_assert_async_without_return",
None,
Some(&lock_path),
demo_assert(),
)
Expand Down Expand Up @@ -146,6 +157,7 @@ mod tests {
async fn call_serial_test_fn(lock_path: &str) {
fs_async_parallel_core_with_return(
"unlock_on_assert_async_with_return",
None,
Some(&lock_path),
demo_assert(),
)
Expand Down
11 changes: 8 additions & 3 deletions serial_test/src/serial_file_lock.rs
@@ -1,7 +1,9 @@
use std::time::Duration;

use crate::file_lock::make_lock_for_name_and_path;

#[doc(hidden)]
pub fn fs_serial_core(name: &str, path: Option<&str>, function: fn()) {
pub fn fs_serial_core(name: &str, _max_wait: Option<Duration>, path: Option<&str>, function: fn()) {
let mut lock = make_lock_for_name_and_path(name, path);
lock.start_serial();
function();
Expand All @@ -11,6 +13,7 @@ pub fn fs_serial_core(name: &str, path: Option<&str>, function: fn()) {
#[doc(hidden)]
pub fn fs_serial_core_with_return<E>(
name: &str,
_max_wait: Option<Duration>,
path: Option<&str>,
function: fn() -> Result<(), E>,
) -> Result<(), E> {
Expand All @@ -24,6 +27,7 @@ pub fn fs_serial_core_with_return<E>(
#[doc(hidden)]
pub async fn fs_async_serial_core_with_return<E>(
name: &str,
_max_wait: Option<Duration>,
path: Option<&str>,
fut: impl std::future::Future<Output = Result<(), E>>,
) -> Result<(), E> {
Expand All @@ -37,6 +41,7 @@ pub async fn fs_async_serial_core_with_return<E>(
#[doc(hidden)]
pub async fn fs_async_serial_core(
name: &str,
_max_wait: Option<Duration>,
path: Option<&str>,
fut: impl std::future::Future<Output = ()>,
) {
Expand All @@ -57,14 +62,14 @@ mod tests {

#[test]
fn test_serial() {
fs_serial_core("test", None, || {});
fs_serial_core("test", None, None, || {});
}

#[test]
fn unlock_on_assert_sync_without_return() {
let lock_path = path_for_name("unlock_on_assert_sync_without_return");
let _ = panic::catch_unwind(|| {
fs_serial_core("foo", Some(&lock_path), || {
fs_serial_core("foo", None, Some(&lock_path), || {
assert!(false);
})
});
Expand Down
3 changes: 3 additions & 0 deletions serial_test_derive/src/lib.rs
Expand Up @@ -309,6 +309,9 @@ fn local_parallel_core(

fn fs_args(attr: proc_macro2::TokenStream) -> Vec<Box<dyn ToTokens>> {
let config = get_core_key(attr);
if config.timeout.is_some() {
panic!("Timeout is not supported for file_serial");
}
vec![
Box::new(config.name),
Box::new(QuoteOption(config.timeout)),
Expand Down

0 comments on commit 3b97657

Please sign in to comment.