Skip to content

Commit 2fc79fd

Browse files
committed
add nonpoison and poison mutex tests
Adds tests for the `nonpoison::Mutex` variant by using a macro to duplicate the existing `poison` tests. Note that all of the tests here are adapted from the existing `poison` tests.
1 parent ff7835f commit 2fc79fd

File tree

2 files changed

+260
-146
lines changed

2 files changed

+260
-146
lines changed

library/std/tests/sync/lib.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
#![feature(reentrant_lock)]
77
#![feature(rwlock_downgrade)]
88
#![feature(std_internals)]
9+
#![feature(sync_nonpoison)]
10+
#![feature(nonpoison_mutex)]
911
#![allow(internal_features)]
12+
#![feature(macro_metavar_expr_concat)] // For concatenating identifiers in macros.
1013

1114
mod barrier;
1215
mod condvar;
@@ -29,3 +32,55 @@ mod rwlock;
2932

3033
#[path = "../common/mod.rs"]
3134
mod common;
35+
36+
// A macro that generates two test cases for both the poison and nonpoison locks.
37+
//
38+
// To write a test that tests both `poison` and `nonpoison` locks, import any of the types
39+
// under both `poison` and `nonpoison` using the module name `locks` instead. For example, write
40+
// `use locks::Mutex;` instead of `use std::sync::poiosn::Mutex`. This will import the correct type
41+
// for each test variant.
42+
//
43+
// Write a test as normal in the `test_body`, but instead of calling `unwrap` on `poison` methods
44+
// that return a `LockResult` or similar, call the macro `maybe_unwrap!(...)` on the result.
45+
//
46+
// For example, call `maybe_unwrap!(mutex.lock())` instead of `mutex.lock().unwrap()` or
47+
// `maybe_unwrap!(rwlock.read())` instead of `rwlock.read().unwrap()`.
48+
//
49+
// For the `poison` types, `maybe_unwrap!` will simply unwrap the `Result` (usually this is a form
50+
// of `LockResult`, but it could also be other kinds of results). For the `nonpoison` types, it is a
51+
// no-op.
52+
//
53+
// The `poison` test will have the same `name`, but with a suffix of `_unwrap_poisoned`.
54+
#[macro_export]
55+
macro_rules! nonpoison_and_poison_unwrap_test {
56+
(
57+
name: $name:ident,
58+
test_body: {$($test_body:tt)*}
59+
) => {
60+
// Creates the nonpoison test.
61+
#[test]
62+
fn $name() {
63+
use ::std::sync::nonpoison as locks;
64+
65+
#[allow(unused_macros)]
66+
macro_rules! maybe_unwrap {
67+
($e:expr) => { $e };
68+
}
69+
70+
$($test_body)*
71+
}
72+
73+
// Creates the poison test with the suffix `_unwrap_poisoned`.
74+
#[test]
75+
fn ${concat($name, _unwrap_poisoned)}() {
76+
use ::std::sync::poison as locks;
77+
78+
#[allow(unused_macros)]
79+
macro_rules! maybe_unwrap {
80+
($e:expr) => { ::std::result::Result::unwrap($e) };
81+
}
82+
83+
$($test_body)*
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)