-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from palfrey/file-locking
File locking
- Loading branch information
Showing
10 changed files
with
664 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use lazy_static::lazy_static; | ||
use parking_lot::ReentrantMutex; | ||
use std::collections::HashMap; | ||
use std::ops::{Deref, DerefMut}; | ||
use std::sync::{Arc, RwLock}; | ||
|
||
lazy_static! { | ||
static ref LOCKS: Arc<RwLock<HashMap<String, ReentrantMutex<()>>>> = | ||
Arc::new(RwLock::new(HashMap::new())); | ||
} | ||
|
||
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 | ||
let new_key = { | ||
let unlock = LOCKS.read().unwrap(); | ||
!unlock.deref().contains_key(name) | ||
}; | ||
if new_key { | ||
// This is the rare path, which avoids the multi-writer situation mostly | ||
LOCKS | ||
.write() | ||
.unwrap() | ||
.deref_mut() | ||
.insert(name.to_string(), ReentrantMutex::new(())); | ||
} | ||
} | ||
|
||
#[doc(hidden)] | ||
pub fn local_serial_core_with_return<E>( | ||
name: &str, | ||
function: fn() -> Result<(), E>, | ||
) -> Result<(), E> { | ||
check_new_key(name); | ||
|
||
let unlock = LOCKS.read().unwrap(); | ||
// _guard needs to be named to avoid being instant dropped | ||
let _guard = unlock.deref()[name].lock(); | ||
function() | ||
} | ||
|
||
#[doc(hidden)] | ||
pub fn local_serial_core(name: &str, function: fn()) { | ||
check_new_key(name); | ||
|
||
let unlock = LOCKS.read().unwrap(); | ||
// _guard needs to be named to avoid being instant dropped | ||
let _guard = unlock.deref()[name].lock(); | ||
function(); | ||
} | ||
|
||
#[doc(hidden)] | ||
pub async fn local_async_serial_core_with_return<E>( | ||
name: &str, | ||
fut: impl std::future::Future<Output = Result<(), E>>, | ||
) -> Result<(), E> { | ||
check_new_key(name); | ||
|
||
let unlock = LOCKS.read().unwrap(); | ||
// _guard needs to be named to avoid being instant dropped | ||
let _guard = unlock.deref()[name].lock(); | ||
fut.await | ||
} | ||
|
||
#[doc(hidden)] | ||
pub async fn local_async_serial_core(name: &str, fut: impl std::future::Future<Output = ()>) { | ||
check_new_key(name); | ||
|
||
let unlock = LOCKS.read().unwrap(); | ||
// _guard needs to be named to avoid being instant dropped | ||
let _guard = unlock.deref()[name].lock(); | ||
fut.await; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use fslock::LockFile; | ||
use std::{env, fs, path::Path}; | ||
|
||
struct Lock { | ||
lockfile: LockFile, | ||
} | ||
|
||
impl Lock { | ||
fn unlock(self: &mut Lock) { | ||
self.lockfile.unlock().unwrap(); | ||
println!("Unlock"); | ||
} | ||
} | ||
|
||
fn do_lock(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(); | ||
println!("Waiting on {:?}", path); | ||
lockfile.lock().unwrap(); | ||
println!("Locked for {:?}", path); | ||
Lock { lockfile } | ||
} | ||
|
||
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 { | ||
do_lock(opt_path) | ||
} else { | ||
let default_path = path_for_name(name); | ||
do_lock(&default_path) | ||
} | ||
} | ||
|
||
#[doc(hidden)] | ||
pub fn fs_serial_core(name: &str, path: Option<&str>, function: fn()) { | ||
let mut lock = make_lock_for_name_and_path(name, path); | ||
function(); | ||
lock.unlock(); | ||
} | ||
|
||
#[doc(hidden)] | ||
pub fn fs_serial_core_with_return<E>( | ||
name: &str, | ||
path: Option<&str>, | ||
function: fn() -> Result<(), E>, | ||
) -> Result<(), E> { | ||
let mut lock = make_lock_for_name_and_path(name, path); | ||
let ret = function(); | ||
lock.unlock(); | ||
ret | ||
} | ||
|
||
#[doc(hidden)] | ||
pub async fn fs_async_serial_core_with_return<E>( | ||
name: &str, | ||
path: Option<&str>, | ||
fut: impl std::future::Future<Output = Result<(), E>>, | ||
) -> Result<(), E> { | ||
let mut lock = make_lock_for_name_and_path(name, path); | ||
let ret = fut.await; | ||
lock.unlock(); | ||
ret | ||
} | ||
|
||
#[doc(hidden)] | ||
pub async fn fs_async_serial_core( | ||
name: &str, | ||
path: Option<&str>, | ||
fut: impl std::future::Future<Output = ()>, | ||
) { | ||
let mut lock = make_lock_for_name_and_path(name, path); | ||
fut.await; | ||
lock.unlock(); | ||
} | ||
|
||
#[test] | ||
fn test_serial() { | ||
fs_serial_core("test", None, || {}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
use serial_test::serial_core; | ||
use serial_test::local_serial_core; | ||
|
||
#[test] | ||
fn test_empty_serial_call() { | ||
serial_core("beta", || { | ||
local_serial_core("beta", || { | ||
println!("Bar"); | ||
}); | ||
} |
Oops, something went wrong.