diff --git a/tests/iter_panic.rs b/tests/iter_panic.rs index 0497dc870..43f53cd42 100644 --- a/tests/iter_panic.rs +++ b/tests/iter_panic.rs @@ -1,4 +1,6 @@ extern crate rayon; +#[macro_use] +extern crate more_asserts; use rayon::prelude::*; use std::ops::Range; @@ -22,26 +24,30 @@ fn iter_panic() { #[test] fn iter_panic_fuse() { - fn count(iter: impl ParallelIterator + UnwindSafe) -> usize { - let count = AtomicUsize::new(0); - let result = panic::catch_unwind(|| { - iter.for_each(|_| { - count.fetch_add(1, Ordering::Relaxed); + // We only use a single thread in order to make the behavior + // of 'panic_fuse' deterministic + rayon::ThreadPoolBuilder::new().num_threads(1).build().unwrap().install(|| { + fn count(iter: impl ParallelIterator + UnwindSafe) -> usize { + let count = AtomicUsize::new(0); + let result = panic::catch_unwind(|| { + iter.for_each(|_| { + count.fetch_add(1, Ordering::Relaxed); + }); }); - }); - assert!(result.is_err()); - count.into_inner() - } - - // Without `panic_fuse()`, we'll reach every item except the panicking one. - let expected = ITER.len() - 1; - let iter = ITER.into_par_iter().with_max_len(1); - assert_eq!(count(iter.clone().inspect(check)), expected); - - // With `panic_fuse()` anywhere in the chain, we'll reach fewer items. - assert!(count(iter.clone().inspect(check).panic_fuse()) < expected); - assert!(count(iter.clone().panic_fuse().inspect(check)) < expected); - - // Try in reverse to be sure we hit the producer case. - assert!(count(iter.clone().panic_fuse().inspect(check).rev()) < expected); + assert!(result.is_err()); + count.into_inner() + } + + // Without `panic_fuse()`, we'll reach every item except the panicking one. + let expected = ITER.len() - 1; + let iter = ITER.into_par_iter().with_max_len(1); + assert_eq!(count(iter.clone().inspect(check)), expected); + + // With `panic_fuse()` anywhere in the chain, we'll reach fewer items. + assert_lt!(count(iter.clone().inspect(check).panic_fuse()), expected); + assert_lt!(count(iter.clone().panic_fuse().inspect(check)), expected); + + // Try in reverse to be sure we hit the producer case. + assert_lt!(count(iter.clone().panic_fuse().inspect(check).rev()), expected); + }); }