Skip to content

Commit 94588d9

Browse files
committed
add nonpoison and poison once tests
1 parent a13a2d0 commit 94588d9

File tree

2 files changed

+76
-61
lines changed

2 files changed

+76
-61
lines changed

library/std/tests/sync/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#![feature(std_internals)]
99
#![feature(sync_nonpoison)]
1010
#![feature(nonpoison_mutex)]
11+
#![feature(nonpoison_once)]
1112
#![allow(internal_features)]
1213
#![feature(macro_metavar_expr_concat)] // For concatenating identifiers in macros.
1314

library/std/tests/sync/once.rs

Lines changed: 75 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,84 @@ use std::sync::mpsc::channel;
55
use std::time::Duration;
66
use std::{panic, thread};
77

8-
#[test]
9-
fn smoke_once() {
10-
static O: Once = Once::new();
11-
let mut a = 0;
12-
O.call_once(|| a += 1);
13-
assert_eq!(a, 1);
14-
O.call_once(|| a += 1);
15-
assert_eq!(a, 1);
16-
}
17-
18-
#[test]
19-
fn stampede_once() {
20-
static O: Once = Once::new();
21-
static mut RUN: bool = false;
22-
23-
let (tx, rx) = channel();
24-
for _ in 0..10 {
25-
let tx = tx.clone();
26-
thread::spawn(move || {
27-
for _ in 0..4 {
28-
thread::yield_now()
29-
}
30-
unsafe {
31-
O.call_once(|| {
32-
assert!(!RUN);
33-
RUN = true;
34-
});
35-
assert!(RUN);
36-
}
37-
tx.send(()).unwrap();
38-
});
8+
use super::nonpoison_and_poison_unwrap_test;
9+
10+
nonpoison_and_poison_unwrap_test!(
11+
name: smoke_once,
12+
test_body: {
13+
use locks::Once;
14+
15+
static O: Once = Once::new();
16+
let mut a = 0;
17+
O.call_once(|| a += 1);
18+
assert_eq!(a, 1);
19+
O.call_once(|| a += 1);
20+
assert_eq!(a, 1);
3921
}
40-
41-
unsafe {
42-
O.call_once(|| {
43-
assert!(!RUN);
44-
RUN = true;
45-
});
46-
assert!(RUN);
22+
);
23+
24+
nonpoison_and_poison_unwrap_test!(
25+
name: stampede_once,
26+
test_body: {
27+
use locks::Once;
28+
29+
static O: Once = Once::new();
30+
static mut RUN: bool = false;
31+
32+
let (tx, rx) = channel();
33+
for _ in 0..10 {
34+
let tx = tx.clone();
35+
thread::spawn(move || {
36+
for _ in 0..4 {
37+
thread::yield_now()
38+
}
39+
unsafe {
40+
O.call_once(|| {
41+
assert!(!RUN);
42+
RUN = true;
43+
});
44+
assert!(RUN);
45+
}
46+
tx.send(()).unwrap();
47+
});
48+
}
49+
50+
unsafe {
51+
O.call_once(|| {
52+
assert!(!RUN);
53+
RUN = true;
54+
});
55+
assert!(RUN);
56+
}
57+
58+
for _ in 0..10 {
59+
rx.recv().unwrap();
60+
}
4761
}
48-
49-
for _ in 0..10 {
50-
rx.recv().unwrap();
62+
);
63+
64+
nonpoison_and_poison_unwrap_test!(
65+
name: wait,
66+
test_body: {
67+
use locks::Once;
68+
69+
for _ in 0..50 {
70+
let val = AtomicBool::new(false);
71+
let once = Once::new();
72+
73+
thread::scope(|s| {
74+
for _ in 0..4 {
75+
s.spawn(|| {
76+
once.wait();
77+
assert!(val.load(Relaxed));
78+
});
79+
}
80+
81+
once.call_once(|| val.store(true, Relaxed));
82+
});
83+
}
5184
}
52-
}
85+
);
5386

5487
#[test]
5588
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
@@ -119,25 +152,6 @@ fn wait_for_force_to_finish() {
119152
assert!(t2.join().is_ok());
120153
}
121154

122-
#[test]
123-
fn wait() {
124-
for _ in 0..50 {
125-
let val = AtomicBool::new(false);
126-
let once = Once::new();
127-
128-
thread::scope(|s| {
129-
for _ in 0..4 {
130-
s.spawn(|| {
131-
once.wait();
132-
assert!(val.load(Relaxed));
133-
});
134-
}
135-
136-
once.call_once(|| val.store(true, Relaxed));
137-
});
138-
}
139-
}
140-
141155
#[test]
142156
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
143157
fn wait_on_poisoned() {

0 commit comments

Comments
 (0)