Skip to content

Commit

Permalink
test: Multithreaded tests
Browse files Browse the repository at this point in the history
  • Loading branch information
WieslerAA committed Feb 12, 2025
1 parent ac2436c commit 18f8c30
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ impl<T> DerefMut for MutexGuard<'_, T> {

#[cfg(test)]
mod tests {
use std::{hint::black_box, sync::Arc, thread};

use super::*;

#[test]
Expand All @@ -130,6 +132,19 @@ mod tests {
assert_eq!(43, *guard)
}

#[test]
fn multithreaded() {
let mutex = Arc::new(Mutex::new(()));
let thread = thread::spawn({
let mutex = mutex.clone();
move || {
black_box(mutex.lock().unwrap());
}
});
black_box(mutex.lock().unwrap());
thread.join().unwrap();
}

#[test]
#[should_panic(
expected = "Tried to acquire lock with level 0 while a lock with level 0 is acquired. This is a violation of lock hierarchies which could lead to deadlocks."
Expand Down
17 changes: 17 additions & 0 deletions src/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ impl<T> DerefMut for RwLockWriteGuard<'_, T> {

#[cfg(test)]
mod tests {
use std::{hint::black_box, sync::Arc, thread};

use super::*;

#[test]
Expand All @@ -168,6 +170,21 @@ mod tests {
assert_eq!(43, *guard)
}

#[test]
fn multithreaded() {
let mutex = Arc::new(RwLock::new(()));
let thread = thread::spawn({
let mutex = mutex.clone();
move || {
black_box(mutex.read().unwrap());
black_box(mutex.write().unwrap());
}
});
black_box(mutex.read().unwrap());
black_box(mutex.write().unwrap());
thread.join().unwrap();
}

#[cfg(debug_assertions)]
fn poisoned_lock() -> RwLock<()> {
let mutex = RwLock::new(());
Expand Down

0 comments on commit 18f8c30

Please sign in to comment.