rust/library/std/tests/thread.rs
Ralf Jung 1242093da2 also test parts of std
requires disabling some tests that do not work
2024-04-07 10:05:57 +02:00

40 lines
1.1 KiB
Rust

use std::cell::{Cell, RefCell};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
#[test]
#[cfg_attr(target_os = "emscripten", ignore)]
#[cfg_attr(miri, ignore)] // Miri does not like the thread leak
fn sleep_very_long() {
let finished = Arc::new(Mutex::new(false));
let t_finished = finished.clone();
thread::spawn(move || {
thread::sleep(Duration::new(u64::MAX, 0));
*t_finished.lock().unwrap() = true;
});
thread::sleep(Duration::from_millis(100));
assert_eq!(*finished.lock().unwrap(), false);
}
#[test]
fn thread_local_containing_const_statements() {
// This exercises the `const $init:block` cases of the thread_local macro.
// Despite overlapping with expression syntax, the `const { ... }` is not
// parsed as `$init:expr`.
thread_local! {
static CELL: Cell<u32> = const {
let value = 1;
Cell::new(value)
};
static REFCELL: RefCell<u32> = const {
let value = 1;
RefCell::new(value)
};
}
assert_eq!(CELL.get(), 1);
assert_eq!(REFCELL.take(), 1);
}