From 4ba03be97e6cc7e790bbc9bfc18caaa228c8a262 Mon Sep 17 00:00:00 2001 From: Botahamec Date: Fri, 28 Feb 2025 16:09:11 -0500 Subject: Scoped lock API --- tests/retry_rw.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 tests/retry_rw.rs (limited to 'tests/retry_rw.rs') diff --git a/tests/retry_rw.rs b/tests/retry_rw.rs new file mode 100644 index 0000000..976ab14 --- /dev/null +++ b/tests/retry_rw.rs @@ -0,0 +1,34 @@ +use std::time::Duration; + +use happylock::{collection::RetryingLockCollection, RwLock, ThreadKey}; + +static RWLOCK_1: RwLock = RwLock::new(1); +static RWLOCK_2: RwLock = RwLock::new(2); +static RWLOCK_3: RwLock = RwLock::new(3); + +fn thread_1() { + let key = ThreadKey::get().unwrap(); + let mut guard = RWLOCK_2.write(key); + std::thread::sleep(Duration::from_millis(75)); + assert_eq!(*guard, 2); + *guard = 5; +} + +fn thread_2() { + let key = ThreadKey::get().unwrap(); + let collection = RetryingLockCollection::try_new([&RWLOCK_1, &RWLOCK_2, &RWLOCK_3]).unwrap(); + std::thread::sleep(Duration::from_millis(25)); + let guard = collection.read(key); + assert_eq!(*guard[0], 1); + assert_eq!(*guard[1], 5); + assert_eq!(*guard[2], 3); +} + +#[test] +fn retries() { + let t1 = std::thread::spawn(thread_1); + let t2 = std::thread::spawn(thread_2); + + t1.join().unwrap(); + t2.join().unwrap(); +} -- cgit v1.2.3