usecrate::unwind; usecrate::ThreadPoolBuilder; usecrate::{scope, scope_fifo, Scope, ScopeFifo}; use rand::{Rng, SeedableRng}; use rand_xorshift::XorShiftRng; use std::cmp; use std::iter::once; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Barrier, Mutex}; use std::vec;
#[test] fn scope_empty() {
scope(|_| {});
}
#[test] fn scope_result() { let x = scope(|_| 22);
assert_eq!(x, 22);
}
fn random_tree1(depth: usize, rng: &mut XorShiftRng) -> Tree<u32> { let children = if depth == 0 {
vec![]
} else {
(0..rng.gen_range(0..4)) // somewhere between 0 and 3 children at each level
.map(|_| random_tree1(depth - 1, rng))
.collect()
};
Tree {
value: rng.gen_range(0..1_000_000),
children,
}
}
#[test] fn update_tree() { letmut tree: Tree<u32> = random_tree(10); let values: Vec<u32> = tree.iter().cloned().collect();
tree.update(|v| *v += 1); let new_values: Vec<u32> = tree.iter().cloned().collect();
assert_eq!(values.len(), new_values.len()); for (&i, &j) in values.iter().zip(&new_values) {
assert_eq!(i + 1, j);
}
}
/// Check that if you have a chain of scoped tasks where T0 spawns T1 /// spawns T2 and so forth down to Tn, the stack space should not grow /// linearly with N. We test this by some unsafe hackery and /// permitting an approx 10% change with a 10x input change. #[test] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] fn linear_stack_growth() { let builder = ThreadPoolBuilder::new().num_threads(1); let pool = builder.build().unwrap();
pool.install(|| { letmut max_diff = Mutex::new(0); let bottom_of_stack = 0;
scope(|s| the_final_countdown(s, &bottom_of_stack, &max_diff, 5)); let diff_when_5 = *max_diff.get_mut().unwrap() as f64;
scope(|s| the_final_countdown(s, &bottom_of_stack, &max_diff, 500)); let diff_when_500 = *max_diff.get_mut().unwrap() as f64;
let ratio = diff_when_5 / diff_when_500;
assert!(
ratio > 0.9 && ratio < 1.1, "stack usage ratio out of bounds: {}",
ratio
);
});
}
fn the_final_countdown<'scope>(
s: &Scope<'scope>,
bottom_of_stack: &'scope i32,
max: &'scope Mutex<usize>,
n: usize,
) { let top_of_stack = 0; let p = bottom_of_stack as *const i32 as usize; let q = &top_of_stack as *const i32 as usize; let diff = if p > q { p - q } else { q - p };
letmut data = max.lock().unwrap();
*data = cmp::max(diff, *data);
if n > 0 {
s.spawn(move |s| the_final_countdown(s, bottom_of_stack, max, n - 1));
}
}
#[test] #[cfg_attr(not(panic = "unwind"), ignore)] fn panic_propagate_still_execute_1() { letmut x = false; match unwind::halt_unwinding(|| {
scope(|s| {
s.spawn(|_| panic!("Hello, world!")); // job A
s.spawn(|_| x = true); // job B, should still execute even though A panics
});
}) {
Ok(_) => panic!("failed to propagate panic"),
Err(_) => assert!(x, "job b failed to execute"),
}
}
#[test] #[cfg_attr(not(panic = "unwind"), ignore)] fn panic_propagate_still_execute_2() { letmut x = false; match unwind::halt_unwinding(|| {
scope(|s| {
s.spawn(|_| x = true); // job B, should still execute even though A panics
s.spawn(|_| panic!("Hello, world!")); // job A
});
}) {
Ok(_) => panic!("failed to propagate panic"),
Err(_) => assert!(x, "job b failed to execute"),
}
}
#[test] #[cfg_attr(not(panic = "unwind"), ignore)] fn panic_propagate_still_execute_3() { letmut x = false; match unwind::halt_unwinding(|| {
scope(|s| {
s.spawn(|_| x = true); // spawned job should still execute despite later panic
panic!("Hello, world!");
});
}) {
Ok(_) => panic!("failed to propagate panic"),
Err(_) => assert!(x, "panic after spawn, spawn failed to execute"),
}
}
#[test] #[cfg_attr(not(panic = "unwind"), ignore)] fn panic_propagate_still_execute_4() { letmut x = false; match unwind::halt_unwinding(|| {
scope(|s| {
s.spawn(|_| panic!("Hello, world!"));
x = true;
});
}) {
Ok(_) => panic!("failed to propagate panic"),
Err(_) => assert!(x, "panic in spawn tainted scope"),
}
}
macro_rules! test_order {
($scope:ident => $spawn:ident) => {{ let builder = ThreadPoolBuilder::new().num_threads(1); let pool = builder.build().unwrap();
pool.install(|| { let vec = Mutex::new(vec![]);
$scope(|scope| { let vec = &vec; for i in0..10 {
scope.$spawn(move |scope| { for j in0..10 {
scope.$spawn(move |_| {
vec.lock().unwrap().push(i * 10 + j);
});
}
});
}
});
vec.into_inner().unwrap()
})
}};
}
#[test] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] fn lifo_order() { // In the absence of stealing, `scope()` runs its `spawn()` jobs in LIFO order. let vec = test_order!(scope => spawn); let expected: Vec<i32> = (0..100).rev().collect(); // LIFO -> reversed
assert_eq!(vec, expected);
}
#[test] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] fn fifo_order() { // In the absence of stealing, `scope_fifo()` runs its `spawn_fifo()` jobs in FIFO order. let vec = test_order!(scope_fifo => spawn_fifo); let expected: Vec<i32> = (0..100).collect(); // FIFO -> natural order
assert_eq!(vec, expected);
}
macro_rules! test_nested_order {
($outer_scope:ident => $outer_spawn:ident,
$inner_scope:ident => $inner_spawn:ident) => {{ let builder = ThreadPoolBuilder::new().num_threads(1); let pool = builder.build().unwrap();
pool.install(|| { let vec = Mutex::new(vec![]);
$outer_scope(|scope| { let vec = &vec; for i in0..10 {
scope.$outer_spawn(move |_| {
$inner_scope(|scope| { for j in0..10 {
scope.$inner_spawn(move |_| {
vec.lock().unwrap().push(i * 10 + j);
});
}
});
});
}
});
vec.into_inner().unwrap()
})
}};
}
#[test] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] fn nested_lifo_order() { // In the absence of stealing, `scope()` runs its `spawn()` jobs in LIFO order. let vec = test_nested_order!(scope => spawn, scope => spawn); let expected: Vec<i32> = (0..100).rev().collect(); // LIFO -> reversed
assert_eq!(vec, expected);
}
#[test] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] fn nested_fifo_order() { // In the absence of stealing, `scope_fifo()` runs its `spawn_fifo()` jobs in FIFO order. let vec = test_nested_order!(scope_fifo => spawn_fifo, scope_fifo => spawn_fifo); let expected: Vec<i32> = (0..100).collect(); // FIFO -> natural order
assert_eq!(vec, expected);
}
#[test] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] fn nested_lifo_fifo_order() { // LIFO on the outside, FIFO on the inside let vec = test_nested_order!(scope => spawn, scope_fifo => spawn_fifo); let expected: Vec<i32> = (0..10)
.rev()
.flat_map(|i| (0..10).map(move |j| i * 10 + j))
.collect();
assert_eq!(vec, expected);
}
#[test] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] fn nested_fifo_lifo_order() { // FIFO on the outside, LIFO on the inside let vec = test_nested_order!(scope_fifo => spawn_fifo, scope => spawn); let expected: Vec<i32> = (0..10)
.flat_map(|i| (0..10).rev().map(move |j| i * 10 + j))
.collect();
assert_eq!(vec, expected);
}
/// Test spawns pushing a series of numbers, interleaved /// such that negative values are using an inner scope.
macro_rules! test_mixed_order {
($outer_scope:ident => $outer_spawn:ident,
$inner_scope:ident => $inner_spawn:ident) => {{ let builder = ThreadPoolBuilder::new().num_threads(1); let pool = builder.build().unwrap();
pool.install(|| { let vec = Mutex::new(vec![]);
$outer_scope(|outer_scope| { let vec = &vec;
spawn_push!(outer_scope.$outer_spawn, vec, 0);
$inner_scope(|inner_scope| {
spawn_push!(inner_scope.$inner_spawn, vec, -1);
spawn_push!(outer_scope.$outer_spawn, vec, 1);
spawn_push!(inner_scope.$inner_spawn, vec, -2);
spawn_push!(outer_scope.$outer_spawn, vec, 2);
spawn_push!(inner_scope.$inner_spawn, vec, -3);
});
spawn_push!(outer_scope.$outer_spawn, vec, 3);
});
vec.into_inner().unwrap()
})
}};
}
#[test] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] fn mixed_lifo_order() { // NB: the end of the inner scope makes us execute some of the outer scope // before they've all been spawned, so they're not perfectly LIFO. let vec = test_mixed_order!(scope => spawn, scope => spawn); let expected = vec![-3, 2, -2, 1, -1, 3, 0];
assert_eq!(vec, expected);
}
#[test] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] fn mixed_lifo_fifo_order() { // NB: the end of the inner scope makes us execute some of the outer scope // before they've all been spawned, so they're not perfectly LIFO. let vec = test_mixed_order!(scope => spawn, scope_fifo => spawn_fifo); let expected = vec![-1, 2, -2, 1, -3, 3, 0];
assert_eq!(vec, expected);
}
letmut range = 0..100; let sum = range.clone().sum(); let iter = &mut range;
COUNTER.store(0, Ordering::Relaxed);
scope(|s: &Scope<'static>| { // While we're allowed the locally borrowed iterator, // the spawns must be static. for i in iter {
s.spawn(move |_| {
COUNTER.fetch_add(i, Ordering::Relaxed);
});
}
});
letmut range = 0..100; let sum = range.clone().sum(); let iter = &mut range;
COUNTER.store(0, Ordering::Relaxed);
scope_fifo(|s: &ScopeFifo<'static>| { // While we're allowed the locally borrowed iterator, // the spawns must be static. for i in iter {
s.spawn_fifo(move |_| {
COUNTER.fetch_add(i, Ordering::Relaxed);
});
}
});
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.