]>
git.proxmox.com Git - rustc.git/blob - library/std/src/thread/tests.rs
4 use crate::panic
::panic_any
;
7 mpsc
::{channel, Sender}
,
10 use crate::thread
::{self, ThreadId}
;
11 use crate::time
::Duration
;
12 use crate::time
::Instant
;
14 // !!! These tests are dangerous. If something is buggy, they will hang, !!!
15 // !!! instead of exiting cleanly. This might wedge the buildbots. !!!
18 fn test_unnamed_thread() {
19 thread
::spawn(move || {
20 assert
!(thread
::current().name().is_none());
24 .expect("thread panicked");
28 fn test_named_thread() {
30 .name("ada lovelace".to_string())
32 assert
!(thread
::current().name().unwrap() == "ada lovelace".to_string());
41 fn test_invalid_named_thread() {
42 let _
= Builder
::new().name("ada l\0velace".to_string()).spawn(|| {}
);
47 let (tx
, rx
) = channel();
48 thread
::spawn(move || {
55 fn test_is_running() {
56 let b
= Arc
::new(Barrier
::new(2));
57 let t
= thread
::spawn({
65 // Thread is definitely running here, since it's still waiting for the barrier.
66 assert_eq
!(t
.is_running(), true);
68 // Unblock the barrier.
71 // Now check that t.is_running() becomes false within a reasonable time.
72 let start
= Instant
::now();
73 while t
.is_running() {
74 assert
!(start
.elapsed() < Duration
::from_secs(2));
75 thread
::sleep(Duration
::from_millis(15));
78 // Joining the thread should not block for a significant time now.
79 let join_time
= Instant
::now();
80 assert_eq
!(t
.join().unwrap(), 1234);
81 assert
!(join_time
.elapsed() < Duration
::from_secs(2));
85 fn test_join_panic() {
86 match thread
::spawn(move || panic
!()).join() {
87 result
::Result
::Err(_
) => (),
88 result
::Result
::Ok(()) => panic
!(),
93 fn test_spawn_sched() {
94 let (tx
, rx
) = channel();
96 fn f(i
: i32, tx
: Sender
<()>) {
98 thread
::spawn(move || {
100 tx
.send(()).unwrap();
111 fn test_spawn_sched_childs_on_default_sched() {
112 let (tx
, rx
) = channel();
114 thread
::spawn(move || {
115 thread
::spawn(move || {
116 tx
.send(()).unwrap();
123 fn avoid_copying_the_body
<F
>(spawnfn
: F
)
125 F
: FnOnce(Box
<dyn Fn() + Send
>),
127 let (tx
, rx
) = channel();
129 let x
: Box
<_
> = box 1;
130 let x_in_parent
= (&*x
) as *const i32 as usize;
132 spawnfn(Box
::new(move || {
133 let x_in_child
= (&*x
) as *const i32 as usize;
134 tx
.send(x_in_child
).unwrap();
137 let x_in_child
= rx
.recv().unwrap();
138 assert_eq
!(x_in_parent
, x_in_child
);
142 fn test_avoid_copying_the_body_spawn() {
143 avoid_copying_the_body(|v
| {
144 thread
::spawn(move || v());
149 fn test_avoid_copying_the_body_thread_spawn() {
150 avoid_copying_the_body(|f
| {
151 thread
::spawn(move || {
158 fn test_avoid_copying_the_body_join() {
159 avoid_copying_the_body(|f
| {
160 let _
= thread
::spawn(move || f()).join();
165 fn test_child_doesnt_ref_parent() {
166 // If the child refcounts the parent thread, this will stack overflow when
167 // climbing the thread tree to dereference each ancestor. (See #1789)
168 // (well, it would if the constant were 8000+ - I lowered it to be more
169 // valgrind-friendly. try this at home, instead..!)
170 const GENERATIONS
: u32 = 16;
171 fn child_no(x
: u32) -> Box
<dyn Fn() + Send
> {
172 return Box
::new(move || {
174 thread
::spawn(move || child_no(x
+ 1)());
178 thread
::spawn(|| child_no(0)());
182 fn test_simple_newsched_spawn() {
183 thread
::spawn(move || {}
);
187 fn test_try_panic_message_string_literal() {
188 match thread
::spawn(move || {
189 panic
!("static string");
194 type T
= &'
static str;
195 assert
!(e
.is
::<T
>());
196 assert_eq
!(*e
.downcast
::<T
>().unwrap(), "static string");
203 fn test_try_panic_any_message_owned_str() {
204 match thread
::spawn(move || {
205 panic_any("owned string".to_string());
211 assert
!(e
.is
::<T
>());
212 assert_eq
!(*e
.downcast
::<T
>().unwrap(), "owned string".to_string());
219 fn test_try_panic_any_message_any() {
220 match thread
::spawn(move || {
221 panic_any(box 413u16 as Box
<dyn Any
+ Send
>);
226 type T
= Box
<dyn Any
+ Send
>;
227 assert
!(e
.is
::<T
>());
228 let any
= e
.downcast
::<T
>().unwrap();
229 assert
!(any
.is
::<u16>());
230 assert_eq
!(*any
.downcast
::<u16>().unwrap(), 413);
237 fn test_try_panic_any_message_unit_struct() {
240 match thread
::spawn(move || panic_any(Juju
)).join() {
241 Err(ref e
) if e
.is
::<Juju
>() => {}
242 Err(_
) | Ok(()) => panic
!(),
247 fn test_park_timeout_unpark_before() {
249 thread
::current().unpark();
250 thread
::park_timeout(Duration
::from_millis(u32::MAX
as u64));
255 fn test_park_timeout_unpark_not_called() {
257 thread
::park_timeout(Duration
::from_millis(10));
262 fn test_park_timeout_unpark_called_other_thread() {
264 let th
= thread
::current();
266 let _guard
= thread
::spawn(move || {
267 super::sleep(Duration
::from_millis(50));
271 thread
::park_timeout(Duration
::from_millis(u32::MAX
as u64));
276 fn sleep_ms_smoke() {
277 thread
::sleep(Duration
::from_millis(2));
281 fn test_size_of_option_thread_id() {
282 assert_eq
!(mem
::size_of
::<Option
<ThreadId
>>(), mem
::size_of
::<ThreadId
>());
286 fn test_thread_id_equal() {
287 assert
!(thread
::current().id() == thread
::current().id());
291 fn test_thread_id_not_equal() {
292 let spawned_id
= thread
::spawn(|| thread
::current().id()).join().unwrap();
293 assert
!(thread
::current().id() != spawned_id
);
296 // NOTE: the corresponding test for stderr is in ui/thread-stderr, due
297 // to the test harness apparently interfering with stderr configuration.