use crate::rayon::{ThreadPoolBuilder, join, unwind, yield_now};
use std::{
sync::{
Arc,
atomic::{AtomicUsize, Ordering},
mpsc::channel,
},
thread, time,
};
#[test]
#[should_panic(expected = "Hello, world!")]
fn panic_propagate() {
let thread_pool = ThreadPoolBuilder::new().build().unwrap();
thread_pool.install(|| {
panic!("Hello, world!");
});
}
#[test]
fn workers_stop() {
let registry;
{
let thread_pool = ThreadPoolBuilder::new().num_threads(22).build().unwrap();
registry = thread_pool.install(|| {
join_a_lot(22);
Arc::clone(&thread_pool.registry)
});
assert_eq!(registry.num_threads(), 22);
}
registry.wait_until_stopped();
}
fn join_a_lot(n: usize) {
if n > 0 {
join(|| join_a_lot(n - 1), || join_a_lot(n - 1));
}
}
#[test]
fn sleeper_stop() {
let registry;
{
let thread_pool = ThreadPoolBuilder::new().num_threads(22).build().unwrap();
registry = Arc::clone(&thread_pool.registry);
thread::sleep(time::Duration::from_secs(1));
}
registry.wait_until_stopped();
}
fn count_handler() -> (Arc<AtomicUsize>, impl Fn(usize)) {
let count = Arc::new(AtomicUsize::new(0));
(Arc::clone(&count), move |_| {
count.fetch_add(1, Ordering::SeqCst);
})
}
fn wait_for_counter(mut counter: Arc<AtomicUsize>) -> usize {
for _ in 0..60 {
counter = match Arc::try_unwrap(counter) {
Ok(counter) => return counter.into_inner(),
Err(counter) => {
thread::sleep(time::Duration::from_secs(1));
counter
}
};
}
panic!("Counter is still shared!");
}
#[test]
fn failed_thread_stack() {
let stack_size = isize::MAX as usize;
let (start_count, start_handler) = count_handler();
let builder = ThreadPoolBuilder::new()
.num_threads(10)
.stack_size(stack_size)
.start_handler(start_handler);
let pool = builder.build();
assert!(pool.is_err(), "thread stack should have failed!");
let start_count = wait_for_counter(start_count);
assert!(start_count <= 1);
}
#[test]
fn panic_thread_name() {
let (start_count, start_handler) = count_handler();
let builder = ThreadPoolBuilder::new()
.num_threads(10)
.start_handler(start_handler)
.thread_name(|i| {
if i >= 5 {
panic!();
}
format!("panic_thread_name#{i}")
});
let pool = unwind::halt_unwinding(|| builder.build());
assert!(pool.is_err(), "thread-name panic should propagate!");
assert_eq!(5, wait_for_counter(start_count));
}
#[test]
fn self_install() {
let pool = ThreadPoolBuilder::new().num_threads(1).build().unwrap();
assert!(pool.install(|| pool.install(|| true)));
}
#[test]
fn mutual_install() {
let pool1 = ThreadPoolBuilder::new().num_threads(1).build().unwrap();
let pool2 = ThreadPoolBuilder::new().num_threads(1).build().unwrap();
let ok = pool1.install(|| {
pool2.install(|| {
pool1.install(|| {
true
})
})
});
assert!(ok);
}
#[test]
fn mutual_install_sleepy() {
let pool1 = ThreadPoolBuilder::new().num_threads(1).build().unwrap();
let pool2 = ThreadPoolBuilder::new().num_threads(1).build().unwrap();
let ok = pool1.install(|| {
pool2.install(|| {
thread::sleep(time::Duration::from_secs(1));
pool1.install(|| {
thread::sleep(time::Duration::from_secs(1));
true
})
})
});
assert!(ok);
}
#[test]
fn spawn_lifo_order() {
let builder = ThreadPoolBuilder::new().num_threads(1);
let pool = &builder.build().unwrap();
let (tx, rx) = channel();
pool.install(move || {
for i in 0..10 {
let tx = tx.clone();
pool.spawn(move || {
tx.send(i).unwrap();
});
}
});
let vec = rx.iter().collect::<Vec<i32>>();
let expected: Vec<i32> = (0..10).rev().collect(); assert_eq!(vec, expected);
}
#[test]
fn yield_now_to_spawn() {
let (tx, rx) = channel();
let pool = ThreadPoolBuilder::new().num_threads(4).build().unwrap();
pool.spawn(move || tx.send(22).unwrap());
pool.install(move || {
yield_now();
});
assert_eq!(22, rx.recv().unwrap());
}