Skip to content

Commit

Permalink
Add a few more tests.
Browse files Browse the repository at this point in the history
This adds tests for trying to acquire a write lock when a read lock is
held, and trying to acuire a read lock when a write lock is held.
  • Loading branch information
sunfishcode committed Nov 23, 2022
1 parent 50aa097 commit 8ad7940
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,35 @@ fn double_write_lock() {

drop(g0);
}

#[test]
fn read_and_write_lock() {
let dir = tempdir().unwrap();
let path = dir.path().join("lockfile");

let l0 = RwLock::new(File::create(&path).unwrap());
let mut l1 = RwLock::new(File::open(path).unwrap());

let g0 = l0.try_read().unwrap();

let err = l1.try_write().unwrap_err();
assert!(matches!(err.kind(), ErrorKind::WouldBlock));

drop(g0);
}

#[test]
fn write_and_read_lock() {
let dir = tempdir().unwrap();
let path = dir.path().join("lockfile");

let mut l0 = RwLock::new(File::create(&path).unwrap());
let l1 = RwLock::new(File::open(path).unwrap());

let g0 = l0.try_write().unwrap();

let err = l1.try_read().unwrap_err();
assert!(matches!(err.kind(), ErrorKind::WouldBlock));

drop(g0);
}

0 comments on commit 8ad7940

Please sign in to comment.