Skip to main content

envtestkit/
lock.rs

1use parking_lot::lock_api::{RwLockReadGuard, RwLockWriteGuard};
2use parking_lot::RawRwLock;
3use parking_lot::RwLock;
4
5lazy_static! {
6    static ref LOCK: RwLock<()> = RwLock::new(());
7}
8
9/// Ensure no environment-modifying tests are running at the moment.
10///
11/// This method should be called before running environment-sensitive tests.
12///
13/// # Examples
14///
15/// ```
16/// use envtestkit::lock::lock_read;
17///
18/// let _lock = lock_read();
19/// ```
20pub fn lock_read<'a>() -> RwLockReadGuard<'a, RawRwLock, ()> {
21    LOCK.read()
22}
23
24/// Serialize environment-modifying tests.
25///
26/// This method should be called before running environment-modifying tests.
27///
28/// # Examples
29///
30/// ```
31/// use envtestkit::lock::lock_test;
32///
33/// let _lock = lock_test();
34/// ```
35pub fn lock_test<'a>() -> RwLockWriteGuard<'a, RawRwLock, ()> {
36    LOCK.write()
37}