[svsm-devel] [PR] locking: Add unit tests

Carlos Bilbao carlos.bilbao at amd.com
Thu Sep 28 16:51:44 CEST 2023


Hello,

Just opened pull request to add unit tests for our custom spinlock and
rwlock [1]. Sharing patch below too in case anyone wants to review. Please
remember we also have an open PR for the unit tests for ELF handling [2].

[1] https://github.com/coconut-svsm/svsm/pull/105
[2] https://github.com/coconut-svsm/svsm/pull/99

Thanks,
Carlos

---

Subject: [PATCH] locking: Add unit tests

Add unit tests to test correct behavior of both spinlock and
rwlock.

Signed-off-by: Carlos Bilbao <carlos.bilbao at amd.com>
---
  src/locking/rwlock.rs   | 43 +++++++++++++++++++++++++++++++++++++++++
  src/locking/spinlock.rs | 25 ++++++++++++++++++++++++
  2 files changed, 68 insertions(+)

diff --git a/src/locking/rwlock.rs b/src/locking/rwlock.rs
index 517862e..c219a52 100644
--- a/src/locking/rwlock.rs
+++ b/src/locking/rwlock.rs
@@ -155,3 +155,46 @@ impl<T: Debug> RWLock<T> {
          }
      }
  }
+
+mod tests {
+    use crate::locking::*;
+
+    #[test]
+    fn test_lock_rw() {
+        let rwlock = RWLock::new(42);
+
+        // Acquire a read lock and check the initial value
+        let read_guard = rwlock.lock_read();
+        assert_eq!(*read_guard, 42);
+
+        drop(read_guard);
+
+        let read_guard2 = rwlock.lock_read();
+        assert_eq!(*read_guard2, 42);
+
+        // Create another RWLock instance for modification
+        let rwlock_modify = RWLock::new(0);
+
+        let mut write_guard = rwlock_modify.lock_write();
+        *write_guard = 99;
+        assert_eq!(*write_guard, 99);
+
+        drop(write_guard);
+
+        let read_guard = rwlock.lock_read();
+        assert_eq!(*read_guard, 42);
+
+        // Let's test two concurrent readers on a new RWLock instance
+        let rwlock_concurrent = RWLock::new(123);
+
+        let read_guard1 = rwlock_concurrent.lock_read();
+        let read_guard2 = rwlock_concurrent.lock_read();
+
+        // Assert that both readers can access the same value (123)
+        assert_eq!(*read_guard1, 123);
+        assert_eq!(*read_guard2, 123);
+
+        drop(read_guard1);
+        drop(read_guard2);
+    }
+}
diff --git a/src/locking/spinlock.rs b/src/locking/spinlock.rs
index ef0e0c5..a4b7b34 100644
--- a/src/locking/spinlock.rs
+++ b/src/locking/spinlock.rs
@@ -89,3 +89,28 @@ impl<T: Debug> SpinLock<T> {
          None
      }
  }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_spin_lock() {
+        let spin_lock = SpinLock::new(0);
+
+        let mut guard = spin_lock.lock();
+        *guard += 1;
+
+        // Ensure the locked data is updated.
+        assert_eq!(*guard, 1);
+
+        let spin_lock = SpinLock::new(0);
+
+        let mut guard_failed = spin_lock.lock();
+        *guard_failed += 1;
+
+        // Try to lock again; it should fail and return None.
+        let try_lock_result = spin_lock.try_lock();
+        assert!(try_lock_result.is_none());
+    }
+}
-- 
2.41.0





More information about the Svsm-devel mailing list