summaryrefslogtreecommitdiff
path: root/tests/retry_rw.rs
diff options
context:
space:
mode:
authorBotahamec <botahamec@outlook.com>2025-02-28 16:09:11 -0500
committerBotahamec <botahamec@outlook.com>2025-02-28 16:09:11 -0500
commit4ba03be97e6cc7e790bbc9bfc18caaa228c8a262 (patch)
treea257184577a93ddf240aba698755c2886188788b /tests/retry_rw.rs
parent4a5ec04a29cba07c5960792528bd66b0f99ee3ee (diff)
Scoped lock API
Diffstat (limited to 'tests/retry_rw.rs')
-rw-r--r--tests/retry_rw.rs34
1 files changed, 34 insertions, 0 deletions
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<i32> = RwLock::new(1);
+static RWLOCK_2: RwLock<i32> = RwLock::new(2);
+static RWLOCK_3: RwLock<i32> = 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();
+}