From 4f8783c1fe878f50a9de114219b87cd281e5d792 Mon Sep 17 00:00:00 2001 From: Alex Kladov Date: Sun, 4 Jun 2023 12:45:35 +0100 Subject: [PATCH] switch to thread::scope in tests --- Cargo.toml | 1 - tests/it/race.rs | 20 ++++++++----------- tests/it/race_once_box.rs | 19 +++++++++---------- tests/it/sync_lazy.rs | 13 +++++-------- tests/it/sync_once_cell.rs | 39 +++++++++++++++++--------------------- 5 files changed, 39 insertions(+), 53 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index eb87276..6e326d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,6 @@ critical-section = { version = "1", optional = true } [dev-dependencies] lazy_static = "1.0.0" -crossbeam-utils = "0.8.7" regex = "1.2.0" critical-section = { version = "1.1.1", features = ["std"] } diff --git a/tests/it/race.rs b/tests/it/race.rs index 6c5e11d..24884dd 100644 --- a/tests/it/race.rs +++ b/tests/it/race.rs @@ -3,10 +3,9 @@ use std::sync::Barrier; use std::{ num::NonZeroUsize, sync::atomic::{AtomicUsize, Ordering::SeqCst}, + thread::scope, }; -use crossbeam_utils::thread::scope; - use once_cell::race::{OnceBool, OnceNonZeroUsize}; #[test] @@ -15,7 +14,7 @@ fn once_non_zero_usize_smoke_test() { let cell = OnceNonZeroUsize::new(); let val = NonZeroUsize::new(92).unwrap(); scope(|s| { - s.spawn(|_| { + s.spawn(|| { assert_eq!( cell.get_or_init(|| { cnt.fetch_add(1, SeqCst); @@ -34,8 +33,7 @@ fn once_non_zero_usize_smoke_test() { ); assert_eq!(cnt.load(SeqCst), 1); }); - }) - .unwrap(); + }); assert_eq!(cell.get(), Some(val)); assert_eq!(cnt.load(SeqCst), 1); } @@ -66,7 +64,7 @@ fn once_non_zero_usize_first_wins() { let b2 = Barrier::new(2); let b3 = Barrier::new(2); scope(|s| { - s.spawn(|_| { + s.spawn(|| { let r1 = cell.get_or_init(|| { b1.wait(); b2.wait(); @@ -76,7 +74,7 @@ fn once_non_zero_usize_first_wins() { b3.wait(); }); b1.wait(); - s.spawn(|_| { + s.spawn(|| { let r2 = cell.get_or_init(|| { b2.wait(); b3.wait(); @@ -84,8 +82,7 @@ fn once_non_zero_usize_first_wins() { }); assert_eq!(r2, val1); }); - }) - .unwrap(); + }); assert_eq!(cell.get(), Some(val1)); } @@ -95,7 +92,7 @@ fn once_bool_smoke_test() { let cnt = AtomicUsize::new(0); let cell = OnceBool::new(); scope(|s| { - s.spawn(|_| { + s.spawn(|| { assert_eq!( cell.get_or_init(|| { cnt.fetch_add(1, SeqCst); @@ -114,8 +111,7 @@ fn once_bool_smoke_test() { ); assert_eq!(cnt.load(SeqCst), 1); }); - }) - .unwrap(); + }); assert_eq!(cell.get(), Some(false)); assert_eq!(cnt.load(SeqCst), 1); } diff --git a/tests/it/race_once_box.rs b/tests/it/race_once_box.rs index 2c21b76..0bf3852 100644 --- a/tests/it/race_once_box.rs +++ b/tests/it/race_once_box.rs @@ -5,9 +5,6 @@ use std::sync::{ Arc, }; -#[cfg(feature = "std")] -use crossbeam_utils::thread::scope; - use once_cell::race::OnceBox; #[derive(Default)] @@ -40,13 +37,15 @@ impl Heap { #[cfg(feature = "std")] #[test] fn once_box_smoke_test() { + use std::thread::scope; + let heap = Heap::default(); let global_cnt = AtomicUsize::new(0); let cell = OnceBox::new(); let b = Barrier::new(128); scope(|s| { for _ in 0..128 { - s.spawn(|_| { + s.spawn(|| { let local_cnt = AtomicUsize::new(0); cell.get_or_init(|| { global_cnt.fetch_add(1, SeqCst); @@ -64,8 +63,7 @@ fn once_box_smoke_test() { assert_eq!(local_cnt.load(SeqCst), 1); }); } - }) - .unwrap(); + }); assert!(cell.get().is_some()); assert!(global_cnt.load(SeqCst) > 10); @@ -95,6 +93,8 @@ fn once_box_set() { #[cfg(feature = "std")] #[test] fn once_box_first_wins() { + use std::thread::scope; + let cell = OnceBox::new(); let val1 = 92; let val2 = 62; @@ -103,7 +103,7 @@ fn once_box_first_wins() { let b2 = Barrier::new(2); let b3 = Barrier::new(2); scope(|s| { - s.spawn(|_| { + s.spawn(|| { let r1 = cell.get_or_init(|| { b1.wait(); b2.wait(); @@ -113,7 +113,7 @@ fn once_box_first_wins() { b3.wait(); }); b1.wait(); - s.spawn(|_| { + s.spawn(|| { let r2 = cell.get_or_init(|| { b2.wait(); b3.wait(); @@ -121,8 +121,7 @@ fn once_box_first_wins() { }); assert_eq!(*r2, val1); }); - }) - .unwrap(); + }); assert_eq!(cell.get(), Some(&val1)); } diff --git a/tests/it/sync_lazy.rs b/tests/it/sync_lazy.rs index f0b5768..44d70fa 100644 --- a/tests/it/sync_lazy.rs +++ b/tests/it/sync_lazy.rs @@ -1,10 +1,9 @@ use std::{ cell::Cell, sync::atomic::{AtomicUsize, Ordering::SeqCst}, + thread::scope, }; -use crossbeam_utils::thread::scope; - use once_cell::sync::{Lazy, OnceCell}; #[test] @@ -18,13 +17,12 @@ fn lazy_new() { assert_eq!(called.load(SeqCst), 0); scope(|s| { - s.spawn(|_| { + s.spawn(|| { let y = *x - 30; assert_eq!(y, 62); assert_eq!(called.load(SeqCst), 1); }); - }) - .unwrap(); + }); let y = *x - 30; assert_eq!(y, 62); @@ -120,11 +118,10 @@ fn static_lazy() { xs }); scope(|s| { - s.spawn(|_| { + s.spawn(|| { assert_eq!(&*XS, &vec![1, 2, 3]); }); - }) - .unwrap(); + }); assert_eq!(&*XS, &vec![1, 2, 3]); } diff --git a/tests/it/sync_once_cell.rs b/tests/it/sync_once_cell.rs index 9205e6a..252adea 100644 --- a/tests/it/sync_once_cell.rs +++ b/tests/it/sync_once_cell.rs @@ -1,4 +1,7 @@ -use std::sync::atomic::{AtomicUsize, Ordering::SeqCst}; +use std::{ + sync::atomic::{AtomicUsize, Ordering::SeqCst}, + thread::scope, +}; #[cfg(feature = "std")] use std::sync::Barrier; @@ -6,8 +9,6 @@ use std::sync::Barrier; #[cfg(not(feature = "std"))] use core::cell::Cell; -use crossbeam_utils::thread::scope; - use once_cell::sync::{Lazy, OnceCell}; #[test] @@ -15,12 +16,11 @@ fn once_cell() { let c = OnceCell::new(); assert!(c.get().is_none()); scope(|s| { - s.spawn(|_| { + s.spawn(|| { c.get_or_init(|| 92); assert_eq!(c.get(), Some(&92)); }); - }) - .unwrap(); + }); c.get_or_init(|| panic!("Kabom!")); assert_eq!(c.get(), Some(&92)); } @@ -61,13 +61,12 @@ fn once_cell_drop() { let x = OnceCell::new(); scope(|s| { - s.spawn(|_| { + s.spawn(|| { x.get_or_init(|| Dropper); assert_eq!(DROP_CNT.load(SeqCst), 0); drop(x); }); - }) - .unwrap(); + }); assert_eq!(DROP_CNT.load(SeqCst), 1); } @@ -108,11 +107,10 @@ fn get_or_try_init() { fn wait() { let cell: OnceCell = OnceCell::new(); scope(|s| { - s.spawn(|_| cell.set("hello".to_string())); + s.spawn(|| cell.set("hello".to_string())); let greeting = cell.wait(); assert_eq!(greeting, "hello") - }) - .unwrap(); + }); } #[cfg(feature = "std")] @@ -126,7 +124,7 @@ fn get_or_init_stress() { scope(|s| { for t in 0..n_threads { let cells = &cells; - s.spawn(move |_| { + s.spawn(move || { for (i, (b, s)) in cells.iter().enumerate() { b.wait(); let j = if t % 2 == 0 { s.wait() } else { s.get_or_init(|| i) }; @@ -134,8 +132,7 @@ fn get_or_init_stress() { } }); } - }) - .unwrap(); + }); } #[test] @@ -260,7 +257,7 @@ fn once_cell_does_not_leak_partially_constructed_boxes() { let cell: OnceCell = OnceCell::new(); scope(|scope| { for _ in 0..n_readers { - scope.spawn(|_| loop { + scope.spawn(|| loop { if let Some(msg) = cell.get() { assert_eq!(msg, MSG); break; @@ -268,10 +265,9 @@ fn once_cell_does_not_leak_partially_constructed_boxes() { }); } for _ in 0..n_writers { - let _ = scope.spawn(|_| cell.set(MSG.to_owned())); + let _ = scope.spawn(|| cell.set(MSG.to_owned())); } - }) - .unwrap() + }); } } @@ -281,7 +277,7 @@ fn get_does_not_block() { let cell = OnceCell::new(); let barrier = Barrier::new(2); scope(|scope| { - scope.spawn(|_| { + scope.spawn(|| { cell.get_or_init(|| { barrier.wait(); barrier.wait(); @@ -291,8 +287,7 @@ fn get_does_not_block() { barrier.wait(); assert_eq!(cell.get(), None); barrier.wait(); - }) - .unwrap(); + }); assert_eq!(cell.get(), Some(&"hello".to_string())); }