serial_test-3.2.0/.cargo_vcs_info.json0000644000000001510000000000100133510ustar { "git": { "sha1": "3ac9744b019bee996c4d44afc72a0b1af8be1809" }, "path_in_vcs": "serial_test" }serial_test-3.2.0/Cargo.toml0000644000000044350000000000100113600ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "serial_test" version = "3.2.0" authors = ["Tom Parker-Shemilt "] build = false autobins = false autoexamples = false autotests = false autobenches = false description = "Allows for the creation of serialised Rust tests" readme = "README.md" keywords = [ "sequential", "testing", "parallel", ] categories = ["development-tools::testing"] license = "MIT" repository = "https://github.com/palfrey/serial_test/" [package.metadata.cargo-all-features] denylist = [ "docsrs", "test_logging", ] skip_optional_dependencies = true [package.metadata.docs.rs] all-features = true rustdoc-args = [ "--cfg", "docsrs", ] [lib] name = "serial_test" path = "src/lib.rs" [[test]] name = "tests" path = "tests/tests.rs" [dependencies.document-features] version = "0.2" optional = true [dependencies.env_logger] version = ">=0.6.1" optional = true default-features = false [dependencies.fslock] version = "0.2" features = ["std"] optional = true default-features = false [dependencies.futures] version = "^0.3" features = ["executor"] optional = true default-features = false [dependencies.log] version = ">=0.4.4" optional = true [dependencies.once_cell] version = "^1.19" features = ["std"] default-features = false [dependencies.parking_lot] version = "^0.12" default-features = false [dependencies.scc] version = "2" default-features = false [dependencies.serial_test_derive] version = "~3.2.0" [dev-dependencies.itertools] version = ">=0.4" features = ["use_std"] default-features = false [features] async = [ "dep:futures", "serial_test_derive/async", ] default = [ "logging", "async", ] docsrs = ["dep:document-features"] file_locks = ["dep:fslock"] logging = ["dep:log"] test_logging = [ "logging", "dep:env_logger", "serial_test_derive/test_logging", ] serial_test-3.2.0/Cargo.toml.orig000064400000000000000000000035711046102023000150410ustar 00000000000000[package] name = "serial_test" description = "Allows for the creation of serialised Rust tests" license = "MIT" version = "3.2.0" authors = ["Tom Parker-Shemilt "] edition = "2018" repository = "https://github.com/palfrey/serial_test/" readme = "README.md" categories = ["development-tools::testing"] keywords = ["sequential", "testing", "parallel"] [dependencies] once_cell = {version="^1.19", features = ["std"], default-features = false} parking_lot = {version="^0.12", default-features = false} serial_test_derive = { version = "~3.2.0", path = "../serial_test_derive" } fslock = { version = "0.2", optional = true, default-features = false, features = ["std"]} document-features = { version = "0.2", optional = true } log = { version = ">=0.4.4", optional = true } futures = { version = "^0.3", default-features = false, features = [ "executor", ], optional = true} scc = { version = "2", default-features = false} env_logger = {version=">=0.6.1", optional=true, default-features = false} [dev-dependencies] itertools = {version=">=0.4", default-features = false, features = ["use_std"]} [features] default = ["logging", "async"] ## Switches on debug logging logging = ["dep:log"] ## Switches on debug with env_logger. Generally only needed by internal serial_test work. test_logging = ["logging", "dep:env_logger", "serial_test_derive/test_logging"] ## Enables async features (and requires the `futures` package) async = ["dep:futures", "serial_test_derive/async"] ## The file_locks feature unlocks the `file_serial`/`file_parallel` macros file_locks = ["dep:fslock"] docsrs = ["dep:document-features"] # docs.rs-specific configuration [package.metadata.docs.rs] all-features = true # defines the configuration attribute `docsrs` rustdoc-args = ["--cfg", "docsrs"] [package.metadata.cargo-all-features] skip_optional_dependencies = true denylist = ["docsrs", "test_logging"] serial_test-3.2.0/LICENSE000064400000000000000000000020451046102023000131520ustar 00000000000000Copyright (c) 2018 Tom Parker-Shemilt Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.serial_test-3.2.0/README.md000064400000000000000000000045261046102023000134320ustar 00000000000000# serial_test [![Version](https://img.shields.io/crates/v/serial_test.svg)](https://crates.io/crates/serial_test) [![Downloads](https://img.shields.io/crates/d/serial_test)](https://crates.io/crates/serial_test) [![Docs](https://docs.rs/serial_test/badge.svg)](https://docs.rs/serial_test/) [![MIT license](https://img.shields.io/crates/l/serial_test.svg)](./LICENSE) [![Build Status](https://github.com/palfrey/serial_test/workflows/Continuous%20integration/badge.svg?branch=main)](https://github.com/palfrey/serial_test/actions) [![MSRV: 1.68.2](https://flat.badgen.net/badge/MSRV/1.68.2/purple)](https://blog.rust-lang.org/2023/03/28/Rust-1.68.2.html) `serial_test` allows for the creation of serialised Rust tests using the `serial` attribute e.g. ```rust #[test] #[serial] fn test_serial_one() { // Do things } #[test] #[serial] fn test_serial_another() { // Do things } #[tokio::test] #[serial] async fn test_serial_another() { // Do things asynchronously } ``` Multiple tests with the `serial` attribute are guaranteed to be executed in serial. Ordering of the tests is not guaranteed however. Other tests with the `parallel` attribute may run at the same time as each other, but not at the same time as a test with `serial`. Tests with neither attribute may run at any time and no guarantees are made about their timing! Both support optional keys for defining subsets of tests to run in serial together, see docs for more details. For cases like doctests and integration tests where the tests are run as separate processes, we also support `file_serial`, with similar properties but based off file locking. Note that there are no guarantees about one test with `serial` and another with `file_serial` as they lock using different methods. All of the attributes can also be applied at a `mod` level and will be automagically applied to all test functions in that block. ## Usage The minimum supported Rust version here is 1.68.2. Note this is minimum _supported_, as it may well compile with lower versions, but they're not supported at all. Upgrades to this will require at a major version bump. 1.x supports 1.51 if you need a lower version than that. Add to your Cargo.toml ```toml [dev-dependencies] serial_test = "*" ``` plus `use serial_test::serial;` in your imports section. You can then either add `#[serial]` or `#[serial(some_key)]` to tests as required. serial_test-3.2.0/src/code_lock.rs000064400000000000000000000117621046102023000152320ustar 00000000000000use crate::rwlock::{Locks, MutexGuardWrapper}; use once_cell::sync::OnceCell; use scc::{hash_map::Entry, HashMap}; use std::sync::atomic::AtomicU32; #[derive(Clone)] pub(crate) struct UniqueReentrantMutex { locks: Locks, // Only actually used for tests #[allow(dead_code)] pub(crate) id: u32, } impl UniqueReentrantMutex { pub(crate) fn lock(&self) -> MutexGuardWrapper { self.locks.serial() } pub(crate) fn start_parallel(&self) { self.locks.start_parallel(); } pub(crate) fn end_parallel(&self) { self.locks.end_parallel(); } #[cfg(test)] pub fn parallel_count(&self) -> u32 { self.locks.parallel_count() } #[cfg(test)] pub fn is_locked(&self) -> bool { self.locks.is_locked() } pub fn is_locked_by_current_thread(&self) -> bool { self.locks.is_locked_by_current_thread() } } #[inline] pub(crate) fn global_locks() -> &'static HashMap { #[cfg(feature = "test_logging")] let _ = env_logger::builder().try_init(); static LOCKS: OnceCell> = OnceCell::new(); LOCKS.get_or_init(HashMap::new) } /// Check if the current thread is holding a serial lock /// /// Can be used to assert that a piece of code can only be called /// from a test marked `#[serial]`. /// /// Example, with `#[serial]`: /// /// ``` /// use serial_test::{is_locked_serially, serial}; /// /// fn do_something_in_need_of_serialization() { /// assert!(is_locked_serially(None)); /// /// // ... /// } /// /// #[test] /// # fn unused() {} /// #[serial] /// fn main() { /// do_something_in_need_of_serialization(); /// } /// ``` /// /// Example, missing `#[serial]`: /// /// ```should_panic /// use serial_test::{is_locked_serially, serial}; /// /// #[test] /// # fn unused() {} /// // #[serial] // <-- missing /// fn main() { /// assert!(is_locked_serially(None)); /// } /// ``` /// /// Example, `#[test(some_key)]`: /// /// ``` /// use serial_test::{is_locked_serially, serial}; /// /// #[test] /// # fn unused() {} /// #[serial(some_key)] /// fn main() { /// assert!(is_locked_serially(Some("some_key"))); /// assert!(!is_locked_serially(None)); /// } /// ``` pub fn is_locked_serially(name: Option<&str>) -> bool { global_locks() .get(name.unwrap_or_default()) .map(|lock| lock.get().is_locked_by_current_thread()) .unwrap_or_default() } static MUTEX_ID: AtomicU32 = AtomicU32::new(1); impl UniqueReentrantMutex { fn new_mutex(name: &str) -> Self { Self { locks: Locks::new(name), id: MUTEX_ID.fetch_add(1, std::sync::atomic::Ordering::SeqCst), } } } pub(crate) fn check_new_key(name: &str) { // Check if a new key is needed. Just need a read lock, which can be done in sync with everyone else if global_locks().contains(name) { return; }; // This is the rare path, which avoids the multi-writer situation mostly let entry = global_locks().entry(name.to_owned()); match entry { Entry::Occupied(o) => o, Entry::Vacant(v) => v.insert_entry(UniqueReentrantMutex::new_mutex(name)), }; } #[cfg(test)] mod tests { use super::*; use crate::{local_parallel_core, local_serial_core}; const NAME1: &str = "NAME1"; const NAME2: &str = "NAME2"; #[test] fn assert_serially_locked_without_name() { local_serial_core(vec![""], None, || { assert!(is_locked_serially(None)); assert!(!is_locked_serially(Some("no_such_name"))); }); } #[test] fn assert_serially_locked_with_multiple_names() { local_serial_core(vec![NAME1, NAME2], None, || { assert!(is_locked_serially(Some(NAME1))); assert!(is_locked_serially(Some(NAME2))); assert!(!is_locked_serially(Some("no_such_name"))); assert!(!is_locked_serially(None)); }); } #[test] fn assert_serially_locked_when_actually_locked_parallel() { local_parallel_core(vec![NAME1, NAME2], None, || { assert!(!is_locked_serially(Some(NAME1))); assert!(!is_locked_serially(Some(NAME2))); assert!(!is_locked_serially(Some("no_such_name"))); assert!(!is_locked_serially(None)); }); } #[test] fn assert_serially_locked_outside_serial_lock() { assert!(!is_locked_serially(Some(NAME1))); assert!(!is_locked_serially(Some(NAME2))); assert!(!is_locked_serially(None)); local_serial_core(vec![NAME1], None, || { // ... }); assert!(!is_locked_serially(Some(NAME1))); assert!(!is_locked_serially(Some(NAME2))); assert!(!is_locked_serially(None)); } #[test] fn assert_serially_locked_in_different_thread() { local_serial_core(vec![NAME1, NAME2], None, || { std::thread::spawn(|| { assert!(!is_locked_serially(Some(NAME2))); }) .join() .unwrap(); }); } } serial_test-3.2.0/src/file_lock.rs000064400000000000000000000075371046102023000152440ustar 00000000000000use fslock::LockFile; #[cfg(feature = "logging")] use log::debug; use std::{ env, fs::{self, File}, io::{Read, Write}, path::Path, thread, time::Duration, }; pub(crate) struct Lock { lockfile: LockFile, pub(crate) parallel_count: u32, path: String, } impl Lock { // Can't use the same file as fslock truncates it fn gen_count_file(path: &str) -> String { format!("{}-count", path) } fn read_parallel_count(path: &str) -> u32 { let parallel_count = match File::open(Lock::gen_count_file(path)) { Ok(mut file) => { let mut count_buf = [0; 4]; match file.read_exact(&mut count_buf) { Ok(_) => u32::from_ne_bytes(count_buf), Err(_err) => { #[cfg(feature = "logging")] debug!("Error loading count file: {}", _err); 0u32 } } } Err(_) => 0, }; #[cfg(feature = "logging")] debug!("Parallel count for {:?} is {}", path, parallel_count); parallel_count } pub(crate) fn new(path: &str) -> Lock { if !Path::new(path).exists() { fs::write(path, "").unwrap_or_else(|_| panic!("Lock file path was {:?}", path)) } let mut lockfile = LockFile::open(path).unwrap(); #[cfg(feature = "logging")] debug!("Waiting on {:?}", path); lockfile.lock().unwrap(); #[cfg(feature = "logging")] debug!("Locked for {:?}", path); Lock { lockfile, parallel_count: Lock::read_parallel_count(path), path: String::from(path), } } pub(crate) fn start_serial(self: &mut Lock) { loop { if self.parallel_count == 0 { return; } #[cfg(feature = "logging")] debug!("Waiting because parallel count is {}", self.parallel_count); // unlock here is safe because we re-lock before returning self.unlock(); thread::sleep(Duration::from_secs(1)); self.lockfile.lock().unwrap(); #[cfg(feature = "logging")] debug!("Locked for {:?}", self.path); self.parallel_count = Lock::read_parallel_count(&self.path) } } fn unlock(self: &mut Lock) { #[cfg(feature = "logging")] debug!("Unlocking {}", self.path); self.lockfile.unlock().unwrap(); } pub(crate) fn end_serial(mut self: Lock) { self.unlock(); } fn write_parallel(self: &Lock) { let mut file = File::create(&Lock::gen_count_file(&self.path)).unwrap(); file.write_all(&self.parallel_count.to_ne_bytes()).unwrap(); } pub(crate) fn start_parallel(self: &mut Lock) { self.parallel_count += 1; self.write_parallel(); self.unlock(); } pub(crate) fn end_parallel(mut self: Lock) { assert!(self.parallel_count > 0); self.parallel_count -= 1; self.write_parallel(); self.unlock(); } } pub(crate) fn path_for_name(name: &str) -> String { let mut pathbuf = env::temp_dir(); pathbuf.push(format!("serial-test-{}", name)); pathbuf.into_os_string().into_string().unwrap() } fn make_lock_for_name_and_path(name: &str, path: Option<&str>) -> Lock { if let Some(opt_path) = path { Lock::new(opt_path) } else { let default_path = path_for_name(name); Lock::new(&default_path) } } pub(crate) fn get_locks(names: &Vec<&str>, path: Option<&str>) -> Vec { if names.len() > 1 && path.is_some() { panic!("Can't do file_parallel with both more than one name _and_ a specific path"); } names .iter() .map(|name| make_lock_for_name_and_path(name, path)) .collect::>() } serial_test-3.2.0/src/lib.rs000064400000000000000000000070211046102023000140470ustar 00000000000000#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] #![deny(unused_variables)] #![deny(missing_docs)] #![deny(unused_imports)] //! # serial_test //! `serial_test` allows for the creation of serialised Rust tests using the [serial](macro@serial) attribute //! e.g. //! ```` //! #[test] //! #[serial] //! fn test_serial_one() { //! // Do things //! } //! //! #[test] //! #[serial(some_key)] //! fn test_serial_another() { //! // Do things //! } //! //! #[test] //! #[parallel] //! fn test_parallel_another() { //! // Do parallel things //! } //! ```` //! Multiple tests with the [serial](macro@serial) attribute are guaranteed to be executed in serial. Ordering //! of the tests is not guaranteed however. Other tests with the [parallel](macro@parallel) attribute may run //! at the same time as each other, but not at the same time as a test with [serial](macro@serial). Tests with //! neither attribute may run at any time and no guarantees are made about their timing! //! //! For cases like doctests and integration tests where the tests are run as separate processes, we also support //! [file_serial](macro@file_serial)/[file_parallel](macro@file_parallel), with similar properties but based off file locking. Note that there are no //! guarantees about one test with [serial](macro@serial)/[parallel](macro@parallel) and another with [file_serial](macro@file_serial)/[file_parallel](macro@file_parallel) //! as they lock using different methods. //! ```` //! #[test] //! #[file_serial] //! fn test_serial_three() { //! // Do things //! } //! ```` //! //! All of the attributes can also be applied at a `mod` level and will be automagically applied to all test functions in that block //! ```` //! #[cfg(test)] //! #[serial] //! mod serial_attr_tests { //! fn foo() { //! // Won't have `serial` applied, because not a test function //! println!("Nothing"); //! } //! //! #[test] //! fn test_bar() { //! // Will be run serially //! } //!} //! ```` //! //! ## Feature flags #![cfg_attr( feature = "docsrs", cfg_attr(doc, doc = ::document_features::document_features!()) )] mod code_lock; mod parallel_code_lock; mod rwlock; mod serial_code_lock; #[cfg(feature = "file_locks")] mod file_lock; #[cfg(feature = "file_locks")] mod parallel_file_lock; #[cfg(feature = "file_locks")] mod serial_file_lock; #[cfg(feature = "async")] #[doc(hidden)] pub use parallel_code_lock::{local_async_parallel_core, local_async_parallel_core_with_return}; #[doc(hidden)] pub use parallel_code_lock::{local_parallel_core, local_parallel_core_with_return}; #[cfg(feature = "async")] #[doc(hidden)] pub use serial_code_lock::{local_async_serial_core, local_async_serial_core_with_return}; #[doc(hidden)] pub use serial_code_lock::{local_serial_core, local_serial_core_with_return}; #[cfg(all(feature = "file_locks", feature = "async"))] #[doc(hidden)] pub use serial_file_lock::{fs_async_serial_core, fs_async_serial_core_with_return}; #[cfg(feature = "file_locks")] #[doc(hidden)] pub use serial_file_lock::{fs_serial_core, fs_serial_core_with_return}; #[cfg(all(feature = "file_locks", feature = "async"))] #[doc(hidden)] pub use parallel_file_lock::{fs_async_parallel_core, fs_async_parallel_core_with_return}; #[cfg(feature = "file_locks")] #[doc(hidden)] pub use parallel_file_lock::{fs_parallel_core, fs_parallel_core_with_return}; // Re-export #[serial/parallel]. pub use serial_test_derive::{parallel, serial}; #[cfg(feature = "file_locks")] pub use serial_test_derive::{file_parallel, file_serial}; pub use code_lock::is_locked_serially; serial_test-3.2.0/src/parallel_code_lock.rs000064400000000000000000000124351046102023000171040ustar 00000000000000#![allow(clippy::await_holding_lock)] use crate::code_lock::{check_new_key, global_locks}; #[cfg(feature = "async")] use futures::FutureExt; use std::panic; fn get_locks(names: Vec<&str>) -> Vec { names .into_iter() .map(|name| { check_new_key(name); global_locks() .get(name) .expect("key to be set") .get() .clone() }) .collect::>() } #[doc(hidden)] pub fn local_parallel_core_with_return( names: Vec<&str>, _path: Option<&str>, function: fn() -> Result<(), E>, ) -> Result<(), E> { let locks = get_locks(names); locks.iter().for_each(|lock| lock.start_parallel()); let res = panic::catch_unwind(function); locks.iter().for_each(|lock| lock.end_parallel()); match res { Ok(ret) => ret, Err(err) => { panic::resume_unwind(err); } } } #[doc(hidden)] pub fn local_parallel_core(names: Vec<&str>, _path: Option<&str>, function: fn()) { let locks = get_locks(names); locks.iter().for_each(|lock| lock.start_parallel()); let res = panic::catch_unwind(|| { function(); }); locks.iter().for_each(|lock| lock.end_parallel()); if let Err(err) = res { panic::resume_unwind(err); } } #[doc(hidden)] #[cfg(feature = "async")] pub async fn local_async_parallel_core_with_return( names: Vec<&str>, _path: Option<&str>, fut: impl std::future::Future> + panic::UnwindSafe, ) -> Result<(), E> { let locks = get_locks(names); locks.iter().for_each(|lock| lock.start_parallel()); let res = fut.catch_unwind().await; locks.iter().for_each(|lock| lock.end_parallel()); match res { Ok(ret) => ret, Err(err) => { panic::resume_unwind(err); } } } #[doc(hidden)] #[cfg(feature = "async")] pub async fn local_async_parallel_core( names: Vec<&str>, _path: Option<&str>, fut: impl std::future::Future + panic::UnwindSafe, ) { let locks = get_locks(names); locks.iter().for_each(|lock| lock.start_parallel()); let res = fut.catch_unwind().await; locks.iter().for_each(|lock| lock.end_parallel()); if let Err(err) = res { panic::resume_unwind(err); } } #[cfg(test)] mod tests { #[cfg(feature = "async")] use crate::{local_async_parallel_core, local_async_parallel_core_with_return}; use crate::{code_lock::global_locks, local_parallel_core, local_parallel_core_with_return}; use std::{io::Error, panic}; #[test] fn unlock_on_assert_sync_without_return() { let _ = panic::catch_unwind(|| { local_parallel_core(vec!["unlock_on_assert_sync_without_return"], None, || { assert!(false); }) }); assert_eq!( global_locks() .get("unlock_on_assert_sync_without_return") .unwrap() .get() .parallel_count(), 0 ); } #[test] fn unlock_on_assert_sync_with_return() { let _ = panic::catch_unwind(|| { local_parallel_core_with_return( vec!["unlock_on_assert_sync_with_return"], None, || -> Result<(), Error> { assert!(false); Ok(()) }, ) }); assert_eq!( global_locks() .get("unlock_on_assert_sync_with_return") .unwrap() .get() .parallel_count(), 0 ); } #[test] #[cfg(feature = "async")] fn unlock_on_assert_async_without_return() { async fn demo_assert() { assert!(false); } async fn call_serial_test_fn() { local_async_parallel_core( vec!["unlock_on_assert_async_without_return"], None, demo_assert(), ) .await } // as per https://stackoverflow.com/a/66529014/320546 let _ = panic::catch_unwind(|| { futures::executor::block_on(call_serial_test_fn()); }); assert_eq!( global_locks() .get("unlock_on_assert_async_without_return") .unwrap() .get() .parallel_count(), 0 ); } #[test] #[cfg(feature = "async")] fn unlock_on_assert_async_with_return() { async fn demo_assert() -> Result<(), Error> { assert!(false); Ok(()) } #[allow(unused_must_use)] async fn call_serial_test_fn() { local_async_parallel_core_with_return( vec!["unlock_on_assert_async_with_return"], None, demo_assert(), ) .await; } // as per https://stackoverflow.com/a/66529014/320546 let _ = panic::catch_unwind(|| { futures::executor::block_on(call_serial_test_fn()); }); assert_eq!( global_locks() .get("unlock_on_assert_async_with_return") .unwrap() .get() .parallel_count(), 0 ); } } serial_test-3.2.0/src/parallel_file_lock.rs000064400000000000000000000115151046102023000171070ustar 00000000000000use std::panic; #[cfg(feature = "async")] use futures::FutureExt; use crate::file_lock::get_locks; #[doc(hidden)] pub fn fs_parallel_core(names: Vec<&str>, path: Option<&str>, function: fn()) { get_locks(&names, path) .iter_mut() .for_each(|lock| lock.start_parallel()); let res = panic::catch_unwind(|| { function(); }); get_locks(&names, path) .into_iter() .for_each(|lock| lock.end_parallel()); if let Err(err) = res { panic::resume_unwind(err); } } #[doc(hidden)] pub fn fs_parallel_core_with_return( names: Vec<&str>, path: Option<&str>, function: fn() -> Result<(), E>, ) -> Result<(), E> { get_locks(&names, path) .iter_mut() .for_each(|lock| lock.start_parallel()); let res = panic::catch_unwind(function); get_locks(&names, path) .into_iter() .for_each(|lock| lock.end_parallel()); match res { Ok(ret) => ret, Err(err) => { panic::resume_unwind(err); } } } #[doc(hidden)] #[cfg(feature = "async")] pub async fn fs_async_parallel_core_with_return( names: Vec<&str>, path: Option<&str>, fut: impl std::future::Future> + panic::UnwindSafe, ) -> Result<(), E> { get_locks(&names, path) .iter_mut() .for_each(|lock| lock.start_parallel()); let res = fut.catch_unwind().await; get_locks(&names, path) .into_iter() .for_each(|lock| lock.end_parallel()); match res { Ok(ret) => ret, Err(err) => { panic::resume_unwind(err); } } } #[doc(hidden)] #[cfg(feature = "async")] pub async fn fs_async_parallel_core( names: Vec<&str>, path: Option<&str>, fut: impl std::future::Future + panic::UnwindSafe, ) { get_locks(&names, path) .iter_mut() .for_each(|lock| lock.start_parallel()); let res = fut.catch_unwind().await; get_locks(&names, path) .into_iter() .for_each(|lock| lock.end_parallel()); if let Err(err) = res { panic::resume_unwind(err); } } #[cfg(test)] mod tests { #[cfg(feature = "async")] use crate::{fs_async_parallel_core, fs_async_parallel_core_with_return}; use crate::{ file_lock::{path_for_name, Lock}, fs_parallel_core, fs_parallel_core_with_return, }; use std::{io::Error, panic}; fn unlock_ok(lock_path: &str) { let lock = Lock::new(lock_path); assert_eq!(lock.parallel_count, 0); } #[test] fn unlock_on_assert_sync_without_return() { let lock_path = path_for_name("parallel_unlock_on_assert_sync_without_return"); let _ = panic::catch_unwind(|| { fs_parallel_core( vec!["parallel_unlock_on_assert_sync_without_return"], Some(&lock_path), || { assert!(false); }, ) }); unlock_ok(&lock_path); } #[test] fn unlock_on_assert_sync_with_return() { let lock_path = path_for_name("unlock_on_assert_sync_with_return"); let _ = panic::catch_unwind(|| { fs_parallel_core_with_return( vec!["unlock_on_assert_sync_with_return"], Some(&lock_path), || -> Result<(), Error> { assert!(false); Ok(()) }, ) }); unlock_ok(&lock_path); } #[test] #[cfg(feature = "async")] fn unlock_on_assert_async_without_return() { let lock_path = path_for_name("unlock_on_assert_async_without_return"); async fn demo_assert() { assert!(false); } async fn call_serial_test_fn(lock_path: &str) { fs_async_parallel_core( vec!["unlock_on_assert_async_without_return"], Some(&lock_path), demo_assert(), ) .await } let _ = panic::catch_unwind(|| { futures::executor::block_on(call_serial_test_fn(&lock_path)); }); unlock_ok(&lock_path); } #[test] #[cfg(feature = "async")] fn unlock_on_assert_async_with_return() { let lock_path = path_for_name("unlock_on_assert_async_with_return"); async fn demo_assert() -> Result<(), Error> { assert!(false); Ok(()) } #[allow(unused_must_use)] async fn call_serial_test_fn(lock_path: &str) { fs_async_parallel_core_with_return( vec!["unlock_on_assert_async_with_return"], Some(&lock_path), demo_assert(), ) .await; } let _ = panic::catch_unwind(|| { futures::executor::block_on(call_serial_test_fn(&lock_path)); }); unlock_ok(&lock_path); } } serial_test-3.2.0/src/rwlock.rs000064400000000000000000000103041046102023000146000ustar 00000000000000#[cfg(feature = "logging")] use log::debug; use parking_lot::{Condvar, Mutex, ReentrantMutex, ReentrantMutexGuard}; use std::{sync::Arc, time::Duration}; struct LockState { parallels: u32, } struct LockData { mutex: Mutex, serial: ReentrantMutex<()>, condvar: Condvar, } #[derive(Clone)] pub(crate) struct Locks { arc: Arc, // Name we're locking for (mostly test usage) #[cfg(feature = "logging")] pub(crate) name: String, } pub(crate) struct MutexGuardWrapper<'a> { #[allow(dead_code)] // need it around to get dropped mutex_guard: ReentrantMutexGuard<'a, ()>, locks: Locks, } impl<'a> Drop for MutexGuardWrapper<'a> { fn drop(&mut self) { #[cfg(feature = "logging")] debug!("End serial"); self.locks.arc.condvar.notify_one(); } } impl Locks { #[allow(unused_variables)] pub fn new(name: &str) -> Locks { Locks { arc: Arc::new(LockData { mutex: Mutex::new(LockState { parallels: 0 }), condvar: Condvar::new(), serial: Default::default(), }), #[cfg(feature = "logging")] name: name.to_owned(), } } #[cfg(test)] pub fn is_locked(&self) -> bool { self.arc.serial.is_locked() } pub fn is_locked_by_current_thread(&self) -> bool { self.arc.serial.is_owned_by_current_thread() } pub fn serial(&self) -> MutexGuardWrapper { #[cfg(feature = "logging")] debug!("Get serial lock '{}'", self.name); let mut lock_state = self.arc.mutex.lock(); loop { #[cfg(feature = "logging")] debug!("Serial acquire {} {}", lock_state.parallels, self.name); // If all the things we want are true, try to lock out serial if lock_state.parallels == 0 { let possible_serial_lock = self.arc.serial.try_lock(); if let Some(serial_lock) = possible_serial_lock { #[cfg(feature = "logging")] debug!("Got serial '{}'", self.name); return MutexGuardWrapper { mutex_guard: serial_lock, locks: self.clone(), }; } else { #[cfg(feature = "logging")] debug!("Someone else has serial '{}'", self.name); } } self.arc .condvar .wait_for(&mut lock_state, Duration::from_secs(1)); } } pub fn start_parallel(&self) { #[cfg(feature = "logging")] debug!("Get parallel lock '{}'", self.name); let mut lock_state = self.arc.mutex.lock(); loop { #[cfg(feature = "logging")] debug!( "Parallel, existing {} '{}'", lock_state.parallels, self.name ); if lock_state.parallels > 0 { // fast path, as someone else already has it locked lock_state.parallels += 1; return; } let possible_serial_lock = self.arc.serial.try_lock(); if possible_serial_lock.is_some() { #[cfg(feature = "logging")] debug!("Parallel first '{}'", self.name); // We now know no-one else has the serial lock, so we can add to parallel lock_state.parallels = 1; // Had to have been 0 before, as otherwise we'd have hit the fast path return; } #[cfg(feature = "logging")] debug!("Parallel waiting '{}'", self.name); self.arc .condvar .wait_for(&mut lock_state, Duration::from_secs(1)); } } pub fn end_parallel(&self) { #[cfg(feature = "logging")] debug!("End parallel '{}", self.name); let mut lock_state = self.arc.mutex.lock(); assert!(lock_state.parallels > 0); lock_state.parallels -= 1; drop(lock_state); self.arc.condvar.notify_one(); } #[cfg(test)] pub fn parallel_count(&self) -> u32 { let lock_state = self.arc.mutex.lock(); lock_state.parallels } } serial_test-3.2.0/src/serial_code_lock.rs000064400000000000000000000064221046102023000165660ustar 00000000000000#![allow(clippy::await_holding_lock)] use crate::code_lock::{check_new_key, global_locks}; #[doc(hidden)] macro_rules! core_internal { ($names: ident) => { let unlocks: Vec<_> = $names .into_iter() .map(|name| { check_new_key(name); global_locks() .get(name) .expect("key to be set") .get() .clone() }) .collect(); let _guards: Vec<_> = unlocks.iter().map(|unlock| unlock.lock()).collect(); }; } #[doc(hidden)] pub fn local_serial_core_with_return( names: Vec<&str>, _path: Option, function: fn() -> Result<(), E>, ) -> Result<(), E> { core_internal!(names); function() } #[doc(hidden)] pub fn local_serial_core(names: Vec<&str>, _path: Option<&str>, function: fn()) { core_internal!(names); function(); } #[doc(hidden)] #[cfg(feature = "async")] pub async fn local_async_serial_core_with_return( names: Vec<&str>, _path: Option<&str>, fut: impl std::future::Future> + std::marker::Send, ) -> Result<(), E> { core_internal!(names); fut.await } #[doc(hidden)] #[cfg(feature = "async")] pub async fn local_async_serial_core( names: Vec<&str>, _path: Option<&str>, fut: impl std::future::Future, ) { core_internal!(names); fut.await; } #[cfg(test)] #[allow(clippy::print_stdout)] mod tests { use super::local_serial_core; use crate::code_lock::{check_new_key, global_locks}; use itertools::Itertools; use parking_lot::RwLock; use std::{ sync::{Arc, Barrier}, thread, time::Duration, }; #[test] fn test_hammer_check_new_key() { let ptrs = Arc::new(RwLock::new(Vec::new())); let mut threads = Vec::new(); let count = 100; let barrier = Arc::new(Barrier::new(count)); for _ in 0..count { let local_locks = global_locks(); let local_ptrs = ptrs.clone(); let c = barrier.clone(); threads.push(thread::spawn(move || { c.wait(); check_new_key("foo"); { let unlock = local_locks.get("foo").expect("read didn't work"); let mutex = unlock.get(); let mut ptr_guard = local_ptrs .try_write_for(Duration::from_secs(1)) .expect("write lock didn't work"); ptr_guard.push(mutex.id); } c.wait(); })); } for thread in threads { thread.join().expect("thread join worked"); } let ptrs_read_lock = ptrs .try_read_recursive_for(Duration::from_secs(1)) .expect("ptrs read work"); assert_eq!(ptrs_read_lock.len(), count); println!("{:?}", ptrs_read_lock); assert_eq!(ptrs_read_lock.iter().unique().count(), 1); } #[test] fn unlock_on_assert() { let _ = std::panic::catch_unwind(|| { local_serial_core(vec!["assert"], None, || { assert!(false); }) }); assert!(!global_locks().get("assert").unwrap().get().is_locked()); } } serial_test-3.2.0/src/serial_file_lock.rs000064400000000000000000000046451046102023000166000ustar 00000000000000use std::panic; use crate::file_lock::get_locks; #[doc(hidden)] pub fn fs_serial_core(names: Vec<&str>, path: Option<&str>, function: fn()) { let mut locks = get_locks(&names, path); locks.iter_mut().for_each(|lock| lock.start_serial()); let res = panic::catch_unwind(function); locks.into_iter().for_each(|lock| lock.end_serial()); if let Err(err) = res { panic::resume_unwind(err); } } #[doc(hidden)] pub fn fs_serial_core_with_return( names: Vec<&str>, path: Option<&str>, function: fn() -> Result<(), E>, ) -> Result<(), E> { let mut locks = get_locks(&names, path); locks.iter_mut().for_each(|lock| lock.start_serial()); let res = panic::catch_unwind(function); locks.into_iter().for_each(|lock| lock.end_serial()); match res { Ok(ret) => ret, Err(err) => { panic::resume_unwind(err); } } } #[doc(hidden)] #[cfg(feature = "async")] pub async fn fs_async_serial_core_with_return( names: Vec<&str>, path: Option<&str>, fut: impl std::future::Future>, ) -> Result<(), E> { let mut locks = get_locks(&names, path); locks.iter_mut().for_each(|lock| lock.start_serial()); let ret: Result<(), E> = fut.await; locks.into_iter().for_each(|lock| lock.end_serial()); ret } #[doc(hidden)] #[cfg(feature = "async")] pub async fn fs_async_serial_core( names: Vec<&str>, path: Option<&str>, fut: impl std::future::Future, ) { let mut locks = get_locks(&names, path); locks.iter_mut().for_each(|lock| lock.start_serial()); fut.await; locks.into_iter().for_each(|lock| lock.end_serial()); } #[cfg(test)] mod tests { use std::panic; use fslock::LockFile; use super::fs_serial_core; use crate::file_lock::path_for_name; #[test] fn test_serial() { fs_serial_core(vec!["test"], None, || {}); } #[test] fn unlock_on_assert_sync_without_return() { let lock_path = path_for_name("serial_unlock_on_assert_sync_without_return"); let _ = panic::catch_unwind(|| { fs_serial_core( vec!["serial_unlock_on_assert_sync_without_return"], Some(&lock_path), || { assert!(false); }, ) }); let mut lockfile = LockFile::open(&lock_path).unwrap(); assert!(lockfile.try_lock().unwrap()); } } serial_test-3.2.0/tests/tests.rs000064400000000000000000000002351046102023000150160ustar 00000000000000use serial_test::local_serial_core; #[test] fn test_empty_serial_call() { local_serial_core(vec!["beta"], None, || { println!("Bar"); }); }