Skip to content

Commit b8ae372

Browse files
committed
Move std::sync unit tests to integration tests
This removes two minor OnceLock tests which test private methods. The rest of the tests should be more than enough to catch mistakes in those private methods. Also makes ReentrantLock::try_lock public. And finally it makes the mpmc tests actually run.
1 parent 332fb7e commit b8ae372

22 files changed

+101
-99
lines changed

Diff for: library/std/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ name = "pipe-subprocess"
130130
path = "tests/pipe_subprocess.rs"
131131
harness = false
132132

133+
[[test]]
134+
name = "sync"
135+
path = "tests/sync/lib.rs"
136+
133137
[[test]]
134138
name = "floats"
135139
path = "tests/floats/lib.rs"

Diff for: library/std/src/sync/barrier.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#[cfg(test)]
2-
mod tests;
3-
41
use crate::fmt;
52
// FIXME(nonpoison_mutex,nonpoison_condvar): switch to nonpoison versions once they are available
63
use crate::sync::{Condvar, Mutex};

Diff for: library/std/src/sync/lazy_lock.rs

-3
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,3 @@ unsafe impl<T: Sync + Send, F: Send> Sync for LazyLock<T, F> {}
350350
impl<T: RefUnwindSafe + UnwindSafe, F: UnwindSafe> RefUnwindSafe for LazyLock<T, F> {}
351351
#[stable(feature = "lazy_cell", since = "1.80.0")]
352352
impl<T: UnwindSafe, F: UnwindSafe> UnwindSafe for LazyLock<T, F> {}
353-
354-
#[cfg(test)]
355-
mod tests;

Diff for: library/std/src/sync/mpsc/mod.rs renamed to library/std/src/sync/mpsc.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,6 @@
137137
138138
#![stable(feature = "rust1", since = "1.0.0")]
139139

140-
#[cfg(all(test, not(any(target_os = "emscripten", target_os = "wasi"))))]
141-
mod tests;
142-
143-
#[cfg(all(test, not(any(target_os = "emscripten", target_os = "wasi"))))]
144-
mod sync_tests;
145-
146140
// MPSC channels are built as a wrapper around MPMC channels, which
147141
// were ported from the `crossbeam-channel` crate. MPMC channels are
148142
// not exposed publicly, but if you are curious about the implementation,
@@ -737,9 +731,10 @@ impl<T> SyncSender<T> {
737731
// Attempts to send for a value on this receiver, returning an error if the
738732
// corresponding channel has hung up, or if it waits more than `timeout`.
739733
//
740-
// This method is currently private and only used for tests.
741-
#[allow(unused)]
742-
fn send_timeout(&self, t: T, timeout: Duration) -> Result<(), mpmc::SendTimeoutError<T>> {
734+
// This method is currently only used for tests.
735+
#[unstable(issue = "none", feature = "std_internals")]
736+
#[doc(hidden)]
737+
pub fn send_timeout(&self, t: T, timeout: Duration) -> Result<(), mpmc::SendTimeoutError<T>> {
743738
self.inner.send_timeout(t, timeout)
744739
}
745740
}

Diff for: library/std/src/sync/once_lock.rs

-3
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,3 @@ unsafe impl<#[may_dangle] T> Drop for OnceLock<T> {
676676
}
677677
}
678678
}
679-
680-
#[cfg(test)]
681-
mod tests;

Diff for: library/std/src/sync/poison/condvar.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#[cfg(test)]
2-
mod tests;
3-
41
use crate::fmt;
52
use crate::sync::poison::{self, LockResult, MutexGuard, PoisonError, mutex};
63
use crate::sys::sync as sys;

Diff for: library/std/src/sync/poison/mutex.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#[cfg(all(test, not(any(target_os = "emscripten", target_os = "wasi"))))]
2-
mod tests;
3-
41
use crate::cell::UnsafeCell;
52
use crate::fmt;
63
use crate::marker::PhantomData;

Diff for: library/std/src/sync/poison/once.rs

-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
//! This primitive is meant to be used to run one-time initialization. An
44
//! example use case would be for initializing an FFI library.
55
6-
#[cfg(all(test, not(any(target_os = "emscripten", target_os = "wasi"))))]
7-
mod tests;
8-
96
use crate::fmt;
107
use crate::panic::{RefUnwindSafe, UnwindSafe};
118
use crate::sys::sync as sys;

Diff for: library/std/src/sync/poison/rwlock.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#[cfg(all(test, not(any(target_os = "emscripten", target_os = "wasi"))))]
2-
mod tests;
3-
41
use crate::cell::UnsafeCell;
52
use crate::fmt;
63
use crate::marker::PhantomData;

Diff for: library/std/src/sync/reentrant_lock.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#[cfg(all(test, not(any(target_os = "emscripten", target_os = "wasi"))))]
2-
mod tests;
3-
41
use cfg_if::cfg_if;
52

63
use crate::cell::UnsafeCell;
@@ -324,7 +321,10 @@ impl<T: ?Sized> ReentrantLock<T> {
324321
/// Otherwise, an RAII guard is returned.
325322
///
326323
/// This function does not block.
327-
pub(crate) fn try_lock(&self) -> Option<ReentrantLockGuard<'_, T>> {
324+
// FIXME maybe make it a public part of the API?
325+
#[unstable(issue = "none", feature = "std_internals")]
326+
#[doc(hidden)]
327+
pub fn try_lock(&self) -> Option<ReentrantLockGuard<'_, T>> {
328328
let this_thread = current_id();
329329
// Safety: We only touch lock_count when we own the inner mutex.
330330
// Additionally, we only call `self.owner.set()` while holding

Diff for: library/std/src/sync/barrier/tests.rs renamed to library/std/tests/sync/barrier.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use crate::sync::mpsc::{TryRecvError, channel};
2-
use crate::sync::{Arc, Barrier};
3-
use crate::thread;
1+
use std::sync::mpsc::{TryRecvError, channel};
2+
use std::sync::{Arc, Barrier};
3+
use std::thread;
44

55
#[test]
66
#[cfg_attr(any(target_os = "emscripten", target_os = "wasi"), ignore)] // no threads

Diff for: library/std/src/sync/poison/condvar/tests.rs renamed to library/std/tests/sync/condvar.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use crate::sync::atomic::{AtomicBool, Ordering};
2-
use crate::sync::mpsc::channel;
3-
use crate::sync::{Arc, Condvar, Mutex};
4-
use crate::thread;
5-
use crate::time::Duration;
1+
use std::sync::atomic::{AtomicBool, Ordering};
2+
use std::sync::mpsc::channel;
3+
use std::sync::{Arc, Condvar, Mutex};
4+
use std::thread;
5+
use std::time::Duration;
66

77
#[test]
88
fn smoke() {

Diff for: library/std/src/sync/lazy_lock/tests.rs renamed to library/std/tests/sync/lazy_lock.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use crate::cell::LazyCell;
2-
use crate::sync::atomic::AtomicUsize;
3-
use crate::sync::atomic::Ordering::SeqCst;
4-
use crate::sync::{LazyLock, Mutex, OnceLock};
5-
use crate::{panic, thread};
1+
use std::cell::LazyCell;
2+
use std::sync::atomic::AtomicUsize;
3+
use std::sync::atomic::Ordering::SeqCst;
4+
use std::sync::{LazyLock, Mutex, OnceLock};
5+
use std::{panic, thread};
66

77
fn spawn_and_wait<R: Send + 'static>(f: impl FnOnce() -> R + Send + 'static) -> R {
88
thread::spawn(f).join().unwrap()
@@ -149,7 +149,7 @@ fn is_sync_send() {
149149
#[should_panic = "has previously been poisoned"]
150150
fn lazy_force_mut_panic() {
151151
let mut lazy = LazyLock::<String>::new(|| panic!());
152-
crate::panic::catch_unwind(crate::panic::AssertUnwindSafe(|| {
152+
panic::catch_unwind(panic::AssertUnwindSafe(|| {
153153
let _ = LazyLock::force_mut(&mut lazy);
154154
}))
155155
.unwrap_err();

Diff for: library/std/tests/sync/lib.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#![feature(lazy_get)]
2+
#![feature(mapped_lock_guards)]
3+
#![feature(mpmc_channel)]
4+
#![feature(once_cell_try)]
5+
#![feature(once_wait)]
6+
#![feature(lock_value_accessors)]
7+
#![feature(reentrant_lock)]
8+
#![feature(rwlock_downgrade)]
9+
#![feature(std_internals)]
10+
#![allow(internal_features)]
11+
12+
mod barrier;
13+
mod condvar;
14+
mod lazy_lock;
15+
#[cfg(not(any(target_os = "emscripten", target_os = "wasi")))]
16+
mod mpmc;
17+
#[cfg(not(any(target_os = "emscripten", target_os = "wasi")))]
18+
mod mpsc;
19+
#[cfg(not(any(target_os = "emscripten", target_os = "wasi")))]
20+
mod mpsc_sync;
21+
#[cfg(not(any(target_os = "emscripten", target_os = "wasi")))]
22+
mod mutex;
23+
#[cfg(not(any(target_os = "emscripten", target_os = "wasi")))]
24+
mod once;
25+
mod once_lock;
26+
#[cfg(not(any(target_os = "emscripten", target_os = "wasi")))]
27+
mod reentrant_lock;
28+
#[cfg(not(any(target_os = "emscripten", target_os = "wasi")))]
29+
mod rwlock;
30+
31+
#[path = "../common/mod.rs"]
32+
mod common;

Diff for: library/std/src/sync/mpmc/tests.rs renamed to library/std/tests/sync/mpmc.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use super::*;
2-
use crate::{env, thread};
1+
use std::sync::mpmc::*;
2+
use std::time::{Duration, Instant};
3+
use std::{env, thread};
34

45
pub fn stress_factor() -> usize {
56
match env::var("RUST_TEST_STRESS") {

Diff for: library/std/src/sync/mpsc/tests.rs renamed to library/std/tests/sync/mpsc.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use super::*;
2-
use crate::{env, thread};
1+
use std::sync::mpsc::*;
2+
use std::time::{Duration, Instant};
3+
use std::{env, thread};
34

45
pub fn stress_factor() -> usize {
56
match env::var("RUST_TEST_STRESS") {

Diff for: library/std/src/sync/mpsc/sync_tests.rs renamed to library/std/tests/sync/mpsc_sync.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use super::*;
2-
use crate::rc::Rc;
3-
use crate::sync::mpmc::SendTimeoutError;
4-
use crate::{env, thread};
1+
use std::rc::Rc;
2+
use std::sync::mpmc::SendTimeoutError;
3+
use std::sync::mpsc::*;
4+
use std::time::Duration;
5+
use std::{env, thread};
56

67
pub fn stress_factor() -> usize {
78
match env::var("RUST_TEST_STRESS") {

Diff for: library/std/src/sync/poison/mutex/tests.rs renamed to library/std/tests/sync/mutex.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use crate::fmt::Debug;
2-
use crate::ops::FnMut;
3-
use crate::panic::{self, AssertUnwindSafe};
4-
use crate::sync::atomic::{AtomicUsize, Ordering};
5-
use crate::sync::mpsc::channel;
6-
use crate::sync::{Arc, Condvar, MappedMutexGuard, Mutex, MutexGuard, TryLockError};
7-
use crate::{hint, mem, thread};
1+
use std::fmt::Debug;
2+
use std::ops::FnMut;
3+
use std::panic::{self, AssertUnwindSafe};
4+
use std::sync::atomic::{AtomicUsize, Ordering};
5+
use std::sync::mpsc::channel;
6+
use std::sync::{Arc, Condvar, MappedMutexGuard, Mutex, MutexGuard, TryLockError};
7+
use std::{hint, mem, thread};
88

99
struct Packet<T>(Arc<(Mutex<T>, Condvar)>);
1010

Diff for: library/std/src/sync/poison/once/tests.rs renamed to library/std/tests/sync/once.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use super::Once;
2-
use crate::sync::atomic::AtomicBool;
3-
use crate::sync::atomic::Ordering::Relaxed;
4-
use crate::sync::mpsc::channel;
5-
use crate::time::Duration;
6-
use crate::{panic, thread};
1+
use std::sync::Once;
2+
use std::sync::atomic::AtomicBool;
3+
use std::sync::atomic::Ordering::Relaxed;
4+
use std::sync::mpsc::channel;
5+
use std::time::Duration;
6+
use std::{panic, thread};
77

88
#[test]
99
fn smoke_once() {

Diff for: library/std/src/sync/once_lock/tests.rs renamed to library/std/tests/sync/once_lock.rs

+5-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use crate::sync::OnceLock;
2-
use crate::sync::atomic::AtomicUsize;
3-
use crate::sync::atomic::Ordering::SeqCst;
4-
use crate::sync::mpsc::channel;
5-
use crate::{panic, thread};
1+
use std::sync::OnceLock;
2+
use std::sync::atomic::AtomicUsize;
3+
use std::sync::atomic::Ordering::SeqCst;
4+
use std::sync::mpsc::channel;
5+
use std::{panic, thread};
66

77
fn spawn_and_wait<R: Send + 'static>(f: impl FnOnce() -> R + Send + 'static) -> R {
88
thread::spawn(f).join().unwrap()
@@ -33,15 +33,6 @@ fn sync_once_cell_get_mut() {
3333
assert_eq!(c.get_mut(), Some(&mut 92));
3434
}
3535

36-
#[test]
37-
fn sync_once_cell_get_unchecked() {
38-
let c = OnceLock::new();
39-
c.set(92).unwrap();
40-
unsafe {
41-
assert_eq!(c.get_unchecked(), &92);
42-
}
43-
}
44-
4536
#[test]
4637
#[cfg_attr(any(target_os = "emscripten", target_os = "wasi"), ignore)] // no threads
4738
fn sync_once_cell_drop() {
@@ -88,7 +79,6 @@ fn get_or_try_init() {
8879

8980
let res = panic::catch_unwind(|| cell.get_or_try_init(|| -> Result<_, ()> { panic!() }));
9081
assert!(res.is_err());
91-
assert!(!cell.is_initialized());
9282
assert!(cell.get().is_none());
9383

9484
assert_eq!(cell.get_or_try_init(|| Err(())), Err(()));

Diff for: library/std/src/sync/reentrant_lock/tests.rs renamed to library/std/tests/sync/reentrant_lock.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use super::ReentrantLock;
2-
use crate::cell::RefCell;
3-
use crate::sync::Arc;
4-
use crate::thread;
1+
use std::cell::RefCell;
2+
use std::sync::{Arc, ReentrantLock};
3+
use std::thread;
54

65
#[test]
76
fn smoke() {

Diff for: library/std/src/sync/poison/rwlock/tests.rs renamed to library/std/tests/sync/rwlock.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
use rand::Rng;
2-
3-
use crate::fmt::Debug;
4-
use crate::ops::FnMut;
5-
use crate::panic::{self, AssertUnwindSafe};
6-
use crate::sync::atomic::{AtomicUsize, Ordering};
7-
use crate::sync::mpsc::channel;
8-
use crate::sync::{
1+
use std::fmt::Debug;
2+
use std::ops::FnMut;
3+
use std::panic::{self, AssertUnwindSafe};
4+
use std::sync::atomic::{AtomicUsize, Ordering};
5+
use std::sync::mpsc::channel;
6+
use std::sync::{
97
Arc, MappedRwLockReadGuard, MappedRwLockWriteGuard, RwLock, RwLockReadGuard, RwLockWriteGuard,
108
TryLockError,
119
};
12-
use crate::{hint, mem, thread};
10+
use std::{hint, mem, thread};
11+
12+
use rand::Rng;
1313

1414
#[derive(Eq, PartialEq, Debug)]
1515
struct NonCopy(i32);
@@ -57,7 +57,7 @@ fn frob() {
5757
let tx = tx.clone();
5858
let r = r.clone();
5959
thread::spawn(move || {
60-
let mut rng = crate::test_helpers::test_rng();
60+
let mut rng = crate::common::test_rng();
6161
for _ in 0..M {
6262
if rng.gen_bool(1.0 / (N as f64)) {
6363
drop(r.write().unwrap());
@@ -704,7 +704,7 @@ fn test_downgrade_atomic() {
704704

705705
// Wait for a good amount of time so that evil threads go to sleep.
706706
// Note: this is not strictly necessary...
707-
let eternity = crate::time::Duration::from_millis(42);
707+
let eternity = std::time::Duration::from_millis(42);
708708
thread::sleep(eternity);
709709

710710
// Once everyone is asleep, set the value to `NEW_VALUE`.

0 commit comments

Comments
 (0)