about summary refs log tree commit diff
path: root/library/std
diff options
context:
space:
mode:
authorConnor Tsui <connor.tsui20@gmail.com>2025-07-29 10:44:36 +0200
committerConnor Tsui <connor.tsui20@gmail.com>2025-07-29 10:44:36 +0200
commitd073d297b65f8cc0c8b75249b74c7df5a3dc18bc (patch)
treecb57261ce3103e5d120b5568bd3e507f5dcbb570 /library/std
parent3eb722e655670cc204aee5e3b4f0a9b9f29a8cd0 (diff)
downloadrust-d073d297b65f8cc0c8b75249b74c7df5a3dc18bc.tar.gz
rust-d073d297b65f8cc0c8b75249b74c7df5a3dc18bc.zip
add extra drop, panic, and unwind tests
Diffstat (limited to 'library/std')
-rw-r--r--library/std/tests/sync/mutex.rs102
1 files changed, 80 insertions, 22 deletions
diff --git a/library/std/tests/sync/mutex.rs b/library/std/tests/sync/mutex.rs
index 60f5d4f45bf..90cefc0d594 100644
--- a/library/std/tests/sync/mutex.rs
+++ b/library/std/tests/sync/mutex.rs
@@ -111,6 +111,7 @@ nonpoison_and_poison_unwrap_test!(
                 self.0.fetch_add(1, Ordering::SeqCst);
             }
         }
+
         let num_drops = Arc::new(AtomicUsize::new(0));
         let m = Mutex::new(Foo(num_drops.clone()));
         assert_eq!(num_drops.load(Ordering::SeqCst), 0);
@@ -170,6 +171,28 @@ nonpoison_and_poison_unwrap_test!(
 
 // Ensure that old values that are replaced by `set` are correctly dropped.
 nonpoison_and_poison_unwrap_test!(
+    name: test_set_drop,
+    test_body: {
+        use locks::Mutex;
+
+        struct Foo(Arc<AtomicUsize>);
+        impl Drop for Foo {
+            fn drop(&mut self) {
+                self.0.fetch_add(1, Ordering::SeqCst);
+            }
+        }
+
+        let num_drops = Arc::new(AtomicUsize::new(0));
+        let m = Mutex::new(Foo(num_drops.clone()));
+        assert_eq!(num_drops.load(Ordering::SeqCst), 0);
+
+        let different = Foo(Arc::new(AtomicUsize::new(42)));
+        maybe_unwrap(m.set(different));
+        assert_eq!(num_drops.load(Ordering::SeqCst), 1);
+    }
+);
+
+nonpoison_and_poison_unwrap_test!(
     name: test_replace,
     test_body: {
         use locks::Mutex;
@@ -277,6 +300,63 @@ nonpoison_and_poison_unwrap_test!(
     }
 );
 
+#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
+nonpoison_and_poison_unwrap_test!(
+    name: test_panics,
+    test_body: {
+        use locks::Mutex;
+
+        let mutex = Mutex::new(42);
+
+        let catch_unwind_result1 = panic::catch_unwind(AssertUnwindSafe(|| {
+            let _guard1 = maybe_unwrap(mutex.lock());
+
+            panic!("test panic with mutex once");
+        }));
+        assert!(catch_unwind_result1.is_err());
+
+        let catch_unwind_result2 = panic::catch_unwind(AssertUnwindSafe(|| {
+            let _guard2 = maybe_unwrap(mutex.lock());
+
+            panic!("test panic with mutex twice");
+        }));
+        assert!(catch_unwind_result2.is_err());
+
+        let catch_unwind_result3 = panic::catch_unwind(AssertUnwindSafe(|| {
+            let _guard3 = maybe_unwrap(mutex.lock());
+
+            panic!("test panic with mutex thrice");
+        }));
+        assert!(catch_unwind_result3.is_err());
+    }
+);
+
+#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
+nonpoison_and_poison_unwrap_test!(
+    name: test_mutex_arc_access_in_unwind,
+    test_body: {
+        use locks::Mutex;
+
+        let arc = Arc::new(Mutex::new(1));
+        let arc2 = arc.clone();
+        let _ = thread::spawn(move || -> () {
+            struct Unwinder {
+                i: Arc<Mutex<i32>>,
+            }
+            impl Drop for Unwinder {
+                fn drop(&mut self) {
+                    *maybe_unwrap(self.i.lock()) += 1;
+                }
+            }
+            let _u = Unwinder { i: arc2 };
+            panic!();
+        })
+        .join();
+        let lock = maybe_unwrap(arc.lock());
+        assert_eq!(*lock, 2);
+    }
+);
+
 ////////////////////////////////////////////////////////////////////////////////////////////////////
 // Poison Tests
 ////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -442,28 +522,6 @@ fn test_mutex_arc_poison_mapped() {
 
 #[test]
 #[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
-fn test_mutex_arc_access_in_unwind() {
-    let arc = Arc::new(Mutex::new(1));
-    let arc2 = arc.clone();
-    let _ = thread::spawn(move || -> () {
-        struct Unwinder {
-            i: Arc<Mutex<i32>>,
-        }
-        impl Drop for Unwinder {
-            fn drop(&mut self) {
-                *self.i.lock().unwrap() += 1;
-            }
-        }
-        let _u = Unwinder { i: arc2 };
-        panic!();
-    })
-    .join();
-    let lock = arc.lock().unwrap();
-    assert_eq!(*lock, 2);
-}
-
-#[test]
-#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
 fn panic_while_mapping_unlocked_poison() {
     let lock = Mutex::new(());