5 use crate::sync::mpsc::{channel, Sender};
6 use crate::thread::{self, ThreadId};
7 use crate::time::Duration;
9 // !!! These tests are dangerous. If something is buggy, they will hang, !!!
10 // !!! instead of exiting cleanly. This might wedge the buildbots. !!!
13 fn test_unnamed_thread() {
14 thread::spawn(move || {
15 assert!(thread::current().name().is_none());
19 .expect("thread panicked");
23 fn test_named_thread() {
25 .name("ada lovelace".to_string())
27 assert!(thread::current().name().unwrap() == "ada lovelace".to_string());
36 fn test_invalid_named_thread() {
37 let _ = Builder::new().name("ada l\0velace".to_string()).spawn(|| {});
42 let (tx, rx) = channel();
43 thread::spawn(move || {
50 fn test_join_panic() {
51 match thread::spawn(move || panic!()).join() {
52 result::Result::Err(_) => (),
53 result::Result::Ok(()) => panic!(),
58 fn test_spawn_sched() {
59 let (tx, rx) = channel();
61 fn f(i: i32, tx: Sender<()>) {
63 thread::spawn(move || {
76 fn test_spawn_sched_childs_on_default_sched() {
77 let (tx, rx) = channel();
79 thread::spawn(move || {
80 thread::spawn(move || {
88 fn avoid_copying_the_body<F>(spawnfn: F)
90 F: FnOnce(Box<dyn Fn() + Send>),
92 let (tx, rx) = channel();
94 let x: Box<_> = box 1;
95 let x_in_parent = (&*x) as *const i32 as usize;
97 spawnfn(Box::new(move || {
98 let x_in_child = (&*x) as *const i32 as usize;
99 tx.send(x_in_child).unwrap();
102 let x_in_child = rx.recv().unwrap();
103 assert_eq!(x_in_parent, x_in_child);
107 fn test_avoid_copying_the_body_spawn() {
108 avoid_copying_the_body(|v| {
109 thread::spawn(move || v());
114 fn test_avoid_copying_the_body_thread_spawn() {
115 avoid_copying_the_body(|f| {
116 thread::spawn(move || {
123 fn test_avoid_copying_the_body_join() {
124 avoid_copying_the_body(|f| {
125 let _ = thread::spawn(move || f()).join();
130 fn test_child_doesnt_ref_parent() {
131 // If the child refcounts the parent thread, this will stack overflow when
132 // climbing the thread tree to dereference each ancestor. (See #1789)
133 // (well, it would if the constant were 8000+ - I lowered it to be more
134 // valgrind-friendly. try this at home, instead..!)
135 const GENERATIONS: u32 = 16;
136 fn child_no(x: u32) -> Box<dyn Fn() + Send> {
137 return Box::new(move || {
139 thread::spawn(move || child_no(x + 1)());
143 thread::spawn(|| child_no(0)());
147 fn test_simple_newsched_spawn() {
148 thread::spawn(move || {});
152 fn test_try_panic_message_static_str() {
153 match thread::spawn(move || {
154 panic!("static string");
159 type T = &'static str;
160 assert!(e.is::<T>());
161 assert_eq!(*e.downcast::<T>().unwrap(), "static string");
168 fn test_try_panic_message_owned_str() {
169 match thread::spawn(move || {
170 panic!("owned string".to_string());
176 assert!(e.is::<T>());
177 assert_eq!(*e.downcast::<T>().unwrap(), "owned string".to_string());
184 fn test_try_panic_message_any() {
185 match thread::spawn(move || {
186 panic!(box 413u16 as Box<dyn Any + Send>);
191 type T = Box<dyn Any + Send>;
192 assert!(e.is::<T>());
193 let any = e.downcast::<T>().unwrap();
194 assert!(any.is::<u16>());
195 assert_eq!(*any.downcast::<u16>().unwrap(), 413);
202 fn test_try_panic_message_unit_struct() {
205 match thread::spawn(move || panic!(Juju)).join() {
206 Err(ref e) if e.is::<Juju>() => {}
207 Err(_) | Ok(()) => panic!(),
212 fn test_park_timeout_unpark_before() {
214 thread::current().unpark();
215 thread::park_timeout(Duration::from_millis(u32::MAX as u64));
220 fn test_park_timeout_unpark_not_called() {
222 thread::park_timeout(Duration::from_millis(10));
227 fn test_park_timeout_unpark_called_other_thread() {
229 let th = thread::current();
231 let _guard = thread::spawn(move || {
232 super::sleep(Duration::from_millis(50));
236 thread::park_timeout(Duration::from_millis(u32::MAX as u64));
241 fn sleep_ms_smoke() {
242 thread::sleep(Duration::from_millis(2));
246 fn test_size_of_option_thread_id() {
247 assert_eq!(mem::size_of::<Option<ThreadId>>(), mem::size_of::<ThreadId>());
251 fn test_thread_id_equal() {
252 assert!(thread::current().id() == thread::current().id());
256 fn test_thread_id_not_equal() {
257 let spawned_id = thread::spawn(|| thread::current().id()).join().unwrap();
258 assert!(thread::current().id() != spawned_id);
261 // NOTE: the corresponding test for stderr is in ui/thread-stderr, due
262 // to the test harness apparently interfering with stderr configuration.