Skip to content

Commit

Permalink
Merge #239
Browse files Browse the repository at this point in the history
239: switch to thread::scope in tests r=matklad a=matklad

bors r+

Co-authored-by: Alex Kladov <aleksey.kladov@gmail.com>
  • Loading branch information
bors[bot] and matklad committed Jun 4, 2023
2 parents 67f5856 + 8837f1f commit ab663de
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 100 deletions.
100 changes: 53 additions & 47 deletions Cargo.lock.msrv

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Expand Up @@ -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"] }

Expand Down
20 changes: 8 additions & 12 deletions tests/it/race.rs
Expand Up @@ -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]
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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();
Expand All @@ -76,16 +74,15 @@ 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();
val2
});
assert_eq!(r2, val1);
});
})
.unwrap();
});

assert_eq!(cell.get(), Some(val1));
}
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down
19 changes: 9 additions & 10 deletions tests/it/race_once_box.rs
Expand Up @@ -5,9 +5,6 @@ use std::sync::{
Arc,
};

#[cfg(feature = "std")]
use crossbeam_utils::thread::scope;

use once_cell::race::OnceBox;

#[derive(Default)]
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -113,16 +113,15 @@ fn once_box_first_wins() {
b3.wait();
});
b1.wait();
s.spawn(|_| {
s.spawn(|| {
let r2 = cell.get_or_init(|| {
b2.wait();
b3.wait();
Box::new(val2)
});
assert_eq!(*r2, val1);
});
})
.unwrap();
});

assert_eq!(cell.get(), Some(&val1));
}
Expand Down

0 comments on commit ab663de

Please sign in to comment.