Skip to content

Commit 71395b1

Browse files
committed
move nonpoison_and_poison_unwrap_test macro to lib.rs
This commit moves the duplicating test macro for poison and nonpoison locks into the parent module. Since it is generic for all locks (either it calls `unwrap` or it doesn't), we can use it for all poison/nonpoison lock tests.
1 parent 12db91e commit 71395b1

File tree

2 files changed

+52
-45
lines changed

2 files changed

+52
-45
lines changed

library/std/tests/sync/lib.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#![feature(sync_nonpoison)]
1010
#![feature(nonpoison_mutex)]
1111
#![allow(internal_features)]
12-
#![feature(macro_metavar_expr_concat)] // For the mutex test macros.
12+
#![feature(macro_metavar_expr_concat)] // For concatenating identifiers in macros.
1313

1414
mod barrier;
1515
mod condvar;
@@ -32,3 +32,50 @@ mod rwlock;
3232

3333
#[path = "../common/mod.rs"]
3434
mod common;
35+
36+
// Generates two test cases for both the poison and nonpoison locks.
37+
//
38+
// To write a test that uses both `poison` and `nonpoison` locks, simply call the macro
39+
// `maybe_unwrap!(...)` on the result of `mutex.lock()`, `rwlock.read()`, etc. For example, you
40+
// would call `maybe_unwrap!(mutex.lock())` instead of `mutex.lock().unwrap()`.
41+
//
42+
// For the `poison` types, it will simply unwrap the `Result` (usually this is a form of
43+
// `LockResult`, but it could also be other kinds of results). For the `nonpoison` types, it is a
44+
// no-op.
45+
//
46+
// The `poison` test will have the same name, but with a suffix of `_unwrap_poisoned`.
47+
#[macro_export]
48+
macro_rules! nonpoison_and_poison_unwrap_test {
49+
(
50+
name: $name:ident,
51+
test_body: {$($test_body:tt)*}
52+
) => {
53+
// Creates the nonpoison test.
54+
#[test]
55+
fn $name() {
56+
#[allow(unused_imports)]
57+
use ::std::sync::nonpoison::*;
58+
59+
#[allow(unused_macros)]
60+
macro_rules! maybe_unwrap {
61+
($e:expr) => { $e };
62+
}
63+
64+
$($test_body)*
65+
}
66+
67+
// Creates the poison test with the suffix `_unwrap_poisoned`.
68+
#[test]
69+
fn ${concat($name, _unwrap_poisoned)}() {
70+
#[allow(unused_imports)]
71+
use ::std::sync::poison::*;
72+
73+
#[allow(unused_macros)]
74+
macro_rules! maybe_unwrap {
75+
($e:expr) => { ::std::result::Result::unwrap($e) };
76+
}
77+
78+
$($test_body)*
79+
}
80+
}
81+
}

library/std/tests/sync/mutex.rs

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,7 @@ use std::sync::mpsc::channel;
66
use std::sync::{Arc, Condvar, MappedMutexGuard, Mutex, MutexGuard, TryLockError};
77
use std::{hint, mem, thread};
88

9-
// Generates two test cases for both the poison and nonpoison locks.
10-
//
11-
// To write a test that uses both `poison::Mutex` and `nonpoison::Mutex`, simply call the macro
12-
// `maybe_unwrap!(...)` on the result of `mutex.lock()`. For the `poison::Mutex`, it will unwrap the
13-
// `Result` (usually `LockResult`, but it could be other kinds of results), but for the
14-
// `nonpoison::Mutex` it will do nothing.
15-
//
16-
// The `poison` test will have the same name, but with a suffix of `_unwrap_poisoned`.
17-
//
18-
// See some of the tests below for examples.
19-
macro_rules! nonpoison_and_poison_unwrap_test {
20-
(
21-
name: $name:ident,
22-
test_body: {$($test_body:tt)*}
23-
) => {
24-
// Creates the nonpoison test.
25-
#[test]
26-
fn $name() {
27-
#[allow(unused_imports)]
28-
use ::std::sync::nonpoison::{Mutex, MappedMutexGuard, MutexGuard};
29-
30-
#[allow(unused_macros)]
31-
macro_rules! maybe_unwrap {
32-
($e:expr) => { $e };
33-
}
34-
35-
$($test_body)*
36-
}
37-
38-
// Creates the poison test with the suffix `_unwrap_poisoned`.
39-
#[test]
40-
fn ${concat($name, _unwrap_poisoned)}() {
41-
#[allow(unused_imports)]
42-
use ::std::sync::{Mutex, MappedMutexGuard, MutexGuard};
43-
44-
#[allow(unused_macros)]
45-
macro_rules! maybe_unwrap {
46-
($e:expr) => { Result::unwrap($e) };
47-
}
48-
49-
$($test_body)*
50-
}
51-
}
52-
}
9+
use super::nonpoison_and_poison_unwrap_test;
5310

5411
nonpoison_and_poison_unwrap_test!(
5512
name: smoke,
@@ -211,6 +168,8 @@ nonpoison_and_poison_unwrap_test!(
211168
}
212169
);
213170

171+
// FIXME(nonpoison_condvar): Move this to the `condvar.rs` test file once `nonpoison::condvar` gets
172+
// implemented.
214173
#[test]
215174
fn test_mutex_arc_condvar() {
216175
struct Packet<T>(Arc<(Mutex<T>, Condvar)>);
@@ -290,6 +249,7 @@ nonpoison_and_poison_unwrap_test!(
290249
}
291250
);
292251

252+
/// Creates a mutex that is immediately poisoned.
293253
fn new_poisoned_mutex<T>(value: T) -> Mutex<T> {
294254
let mutex = Mutex::new(value);
295255

0 commit comments

Comments
 (0)