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 + 4f8783c commit d6359fe
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 53 deletions.
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
13 changes: 5 additions & 8 deletions 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]
Expand All @@ -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);
Expand Down Expand Up @@ -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]);
}

Expand Down
39 changes: 17 additions & 22 deletions tests/it/sync_once_cell.rs
@@ -1,26 +1,26 @@
use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
use std::{
sync::atomic::{AtomicUsize, Ordering::SeqCst},
thread::scope,
};

#[cfg(feature = "std")]
use std::sync::Barrier;

#[cfg(not(feature = "std"))]
use core::cell::Cell;

use crossbeam_utils::thread::scope;

use once_cell::sync::{Lazy, OnceCell};

#[test]
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));
}
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -108,11 +107,10 @@ fn get_or_try_init() {
fn wait() {
let cell: OnceCell<String> = 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")]
Expand All @@ -126,16 +124,15 @@ 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) };
assert_eq!(*j, i);
}
});
}
})
.unwrap();
});
}

#[test]
Expand Down Expand Up @@ -260,18 +257,17 @@ fn once_cell_does_not_leak_partially_constructed_boxes() {
let cell: OnceCell<String> = 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;
}
});
}
for _ in 0..n_writers {
let _ = scope.spawn(|_| cell.set(MSG.to_owned()));
let _ = scope.spawn(|| cell.set(MSG.to_owned()));
}
})
.unwrap()
});
}
}

Expand All @@ -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();
Expand All @@ -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()));
}

Expand Down

0 comments on commit d6359fe

Please sign in to comment.