Skip to content

Commit 7b0a316

Browse files
committed
add extra drop and unwind tests
1 parent 2fc79fd commit 7b0a316

File tree

1 file changed

+80
-23
lines changed

1 file changed

+80
-23
lines changed

library/std/tests/sync/mutex.rs

Lines changed: 80 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use std::{hint, mem, thread};
99
////////////////////////////////////////////////////////////////////////////////////////////////////
1010
// Nonpoison & Poison Tests
1111
////////////////////////////////////////////////////////////////////////////////////////////////////
12-
1312
use super::nonpoison_and_poison_unwrap_test;
1413

1514
nonpoison_and_poison_unwrap_test!(
@@ -112,6 +111,7 @@ nonpoison_and_poison_unwrap_test!(
112111
self.0.fetch_add(1, Ordering::SeqCst);
113112
}
114113
}
114+
115115
let num_drops = Arc::new(AtomicUsize::new(0));
116116
let m = Mutex::new(Foo(num_drops.clone()));
117117
assert_eq!(num_drops.load(Ordering::SeqCst), 0);
@@ -169,6 +169,28 @@ nonpoison_and_poison_unwrap_test!(
169169
}
170170
);
171171

172+
nonpoison_and_poison_unwrap_test!(
173+
name: test_set_drop,
174+
test_body: {
175+
use locks::Mutex;
176+
177+
struct Foo(Arc<AtomicUsize>);
178+
impl Drop for Foo {
179+
fn drop(&mut self) {
180+
self.0.fetch_add(1, Ordering::SeqCst);
181+
}
182+
}
183+
184+
let num_drops = Arc::new(AtomicUsize::new(0));
185+
let m = Mutex::new(Foo(num_drops.clone()));
186+
assert_eq!(num_drops.load(Ordering::SeqCst), 0);
187+
188+
let different = Foo(Arc::new(AtomicUsize::new(42)));
189+
maybe_unwrap!(m.set(different));
190+
assert_eq!(num_drops.load(Ordering::SeqCst), 1);
191+
}
192+
);
193+
172194
nonpoison_and_poison_unwrap_test!(
173195
name: test_replace,
174196
test_body: {
@@ -277,6 +299,63 @@ nonpoison_and_poison_unwrap_test!(
277299
}
278300
);
279301

302+
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
303+
nonpoison_and_poison_unwrap_test!(
304+
name: test_panics,
305+
test_body: {
306+
use locks::Mutex;
307+
308+
let mutex = Mutex::new(42);
309+
310+
let catch_unwind_result1 = panic::catch_unwind(AssertUnwindSafe(|| {
311+
let _guard1 = maybe_unwrap!(mutex.lock());
312+
313+
panic!("test panic with mutex once");
314+
}));
315+
assert!(catch_unwind_result1.is_err());
316+
317+
let catch_unwind_result2 = panic::catch_unwind(AssertUnwindSafe(|| {
318+
let _guard2 = maybe_unwrap!(mutex.lock());
319+
320+
panic!("test panic with mutex twice");
321+
}));
322+
assert!(catch_unwind_result2.is_err());
323+
324+
let catch_unwind_result3 = panic::catch_unwind(AssertUnwindSafe(|| {
325+
let _guard3 = maybe_unwrap!(mutex.lock());
326+
327+
panic!("test panic with mutex thrice");
328+
}));
329+
assert!(catch_unwind_result3.is_err());
330+
}
331+
);
332+
333+
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
334+
nonpoison_and_poison_unwrap_test!(
335+
name: test_mutex_arc_access_in_unwind,
336+
test_body: {
337+
use locks::Mutex;
338+
339+
let arc = Arc::new(Mutex::new(1));
340+
let arc2 = arc.clone();
341+
let _ = thread::spawn(move || -> () {
342+
struct Unwinder {
343+
i: Arc<Mutex<i32>>,
344+
}
345+
impl Drop for Unwinder {
346+
fn drop(&mut self) {
347+
*maybe_unwrap!(self.i.lock()) += 1;
348+
}
349+
}
350+
let _u = Unwinder { i: arc2 };
351+
panic!();
352+
})
353+
.join();
354+
let lock = maybe_unwrap!(arc.lock());
355+
assert_eq!(*lock, 2);
356+
}
357+
);
358+
280359
////////////////////////////////////////////////////////////////////////////////////////////////////
281360
// Poison Tests
282361
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -440,28 +519,6 @@ fn test_mutex_arc_poison_mapped() {
440519
assert!(arc.is_poisoned());
441520
}
442521

443-
#[test]
444-
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
445-
fn test_mutex_arc_access_in_unwind() {
446-
let arc = Arc::new(Mutex::new(1));
447-
let arc2 = arc.clone();
448-
let _ = thread::spawn(move || -> () {
449-
struct Unwinder {
450-
i: Arc<Mutex<i32>>,
451-
}
452-
impl Drop for Unwinder {
453-
fn drop(&mut self) {
454-
*self.i.lock().unwrap() += 1;
455-
}
456-
}
457-
let _u = Unwinder { i: arc2 };
458-
panic!();
459-
})
460-
.join();
461-
let lock = arc.lock().unwrap();
462-
assert_eq!(*lock, 2);
463-
}
464-
465522
#[test]
466523
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
467524
fn panic_while_mapping_unlocked_poison() {

0 commit comments

Comments
 (0)