diff options
| author | Mica White <botahamec@outlook.com> | 2025-03-09 20:49:56 -0400 |
|---|---|---|
| committer | Mica White <botahamec@outlook.com> | 2025-03-09 20:49:56 -0400 |
| commit | 58abf5872023aca7ee6459fa3b2e067d57923ba5 (patch) | |
| tree | 196cadda0dd4386668477ef286f9c9b09480e713 /tests | |
| parent | 4ba03be97e6cc7e790bbc9bfc18caaa228c8a262 (diff) | |
Finish testing and fixing
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/evil_rwlock.rs | 18 | ||||
| -rw-r--r-- | tests/retry_rw.rs | 10 |
2 files changed, 28 insertions, 0 deletions
diff --git a/tests/evil_rwlock.rs b/tests/evil_rwlock.rs index 9eed8a8..4be86a1 100644 --- a/tests/evil_rwlock.rs +++ b/tests/evil_rwlock.rs @@ -1,3 +1,4 @@ +use std::panic::AssertUnwindSafe; use std::sync::Arc; use happylock::collection::{BoxedLockCollection, RetryingLockCollection}; @@ -63,6 +64,23 @@ fn boxed_rwlocks() { assert!(good_mutex.scoped_try_write(&mut key, |_| {}).is_ok()); assert!(evil_mutex.scoped_try_write(&mut key, |_| {}).is_err()); assert!(useless_mutex.scoped_try_write(&mut key, |_| {}).is_ok()); + + std::thread::scope(|s| { + s.spawn(|| { + let evil_mutex = AssertUnwindSafe(evil_mutex); + let r = std::panic::catch_unwind(|| { + let key = ThreadKey::get().unwrap(); + evil_mutex.write(key); + }); + + assert!(r.is_err()); + }); + + s.spawn(|| { + let key = ThreadKey::get().unwrap(); + good_mutex.write(key); + }); + }); } #[test] diff --git a/tests/retry_rw.rs b/tests/retry_rw.rs index 976ab14..0a946b0 100644 --- a/tests/retry_rw.rs +++ b/tests/retry_rw.rs @@ -24,11 +24,21 @@ fn thread_2() { assert_eq!(*guard[2], 3); } +fn thread_3() { + let key = ThreadKey::get().unwrap(); + std::thread::sleep(Duration::from_millis(50)); + let guard = RWLOCK_1.write(key); + std::thread::sleep(Duration::from_millis(50)); + assert_eq!(*guard, 1); +} + #[test] fn retries() { let t1 = std::thread::spawn(thread_1); let t2 = std::thread::spawn(thread_2); + let t3 = std::thread::spawn(thread_3); t1.join().unwrap(); t2.join().unwrap(); + t3.join().unwrap(); } |
