Skip to content

Commit

Permalink
Move std::sync unit tests to integration tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bjorn3 committed Jan 17, 2025
1 parent c4289e4 commit f301e4d
Show file tree
Hide file tree
Showing 22 changed files with 101 additions and 99 deletions.
4 changes: 4 additions & 0 deletions library/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ name = "pipe-subprocess"
path = "tests/pipe_subprocess.rs"
harness = false

[[test]]
name = "sync"
path = "tests/sync/lib.rs"

[[test]]
name = "floats"
path = "tests/floats/lib.rs"
Expand Down
3 changes: 0 additions & 3 deletions library/std/src/sync/barrier.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#[cfg(test)]
mod tests;

use crate::fmt;
// FIXME(nonpoison_mutex,nonpoison_condvar): switch to nonpoison versions once they are available
use crate::sync::{Condvar, Mutex};
Expand Down
3 changes: 0 additions & 3 deletions library/std/src/sync/lazy_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,3 @@ unsafe impl<T: Sync + Send, F: Send> Sync for LazyLock<T, F> {}
impl<T: RefUnwindSafe + UnwindSafe, F: UnwindSafe> RefUnwindSafe for LazyLock<T, F> {}
#[stable(feature = "lazy_cell", since = "1.80.0")]
impl<T: UnwindSafe, F: UnwindSafe> UnwindSafe for LazyLock<T, F> {}

#[cfg(test)]
mod tests;
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,6 @@
#![stable(feature = "rust1", since = "1.0.0")]

#[cfg(all(test, not(any(target_os = "emscripten", target_os = "wasi"))))]
mod tests;

#[cfg(all(test, not(any(target_os = "emscripten", target_os = "wasi"))))]
mod sync_tests;

// MPSC channels are built as a wrapper around MPMC channels, which
// were ported from the `crossbeam-channel` crate. MPMC channels are
// not exposed publicly, but if you are curious about the implementation,
Expand Down Expand Up @@ -737,9 +731,10 @@ impl<T> SyncSender<T> {
// Attempts to send for a value on this receiver, returning an error if the
// corresponding channel has hung up, or if it waits more than `timeout`.
//
// This method is currently private and only used for tests.
#[allow(unused)]
fn send_timeout(&self, t: T, timeout: Duration) -> Result<(), mpmc::SendTimeoutError<T>> {
// This method is currently only used for tests.
#[unstable(issue = "none", feature = "std_internals")]
#[doc(hidden)]
pub fn send_timeout(&self, t: T, timeout: Duration) -> Result<(), mpmc::SendTimeoutError<T>> {
self.inner.send_timeout(t, timeout)
}
}
Expand Down
3 changes: 0 additions & 3 deletions library/std/src/sync/once_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,3 @@ unsafe impl<#[may_dangle] T> Drop for OnceLock<T> {
}
}
}

#[cfg(test)]
mod tests;
3 changes: 0 additions & 3 deletions library/std/src/sync/poison/condvar.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#[cfg(test)]
mod tests;

use crate::fmt;
use crate::sync::poison::{self, LockResult, MutexGuard, PoisonError, mutex};
use crate::sys::sync as sys;
Expand Down
3 changes: 0 additions & 3 deletions library/std/src/sync/poison/mutex.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#[cfg(all(test, not(any(target_os = "emscripten", target_os = "wasi"))))]
mod tests;

use crate::cell::UnsafeCell;
use crate::fmt;
use crate::marker::PhantomData;
Expand Down
3 changes: 0 additions & 3 deletions library/std/src/sync/poison/once.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
//! This primitive is meant to be used to run one-time initialization. An
//! example use case would be for initializing an FFI library.
#[cfg(all(test, not(any(target_os = "emscripten", target_os = "wasi"))))]
mod tests;

use crate::fmt;
use crate::panic::{RefUnwindSafe, UnwindSafe};
use crate::sys::sync as sys;
Expand Down
3 changes: 0 additions & 3 deletions library/std/src/sync/poison/rwlock.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#[cfg(all(test, not(any(target_os = "emscripten", target_os = "wasi"))))]
mod tests;

use crate::cell::UnsafeCell;
use crate::fmt;
use crate::marker::PhantomData;
Expand Down
8 changes: 4 additions & 4 deletions library/std/src/sync/reentrant_lock.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#[cfg(all(test, not(any(target_os = "emscripten", target_os = "wasi"))))]
mod tests;

use cfg_if::cfg_if;

use crate::cell::UnsafeCell;
Expand Down Expand Up @@ -324,7 +321,10 @@ impl<T: ?Sized> ReentrantLock<T> {
/// Otherwise, an RAII guard is returned.
///
/// This function does not block.
pub(crate) fn try_lock(&self) -> Option<ReentrantLockGuard<'_, T>> {
// FIXME maybe make it a public part of the API?
#[unstable(issue = "none", feature = "std_internals")]
#[doc(hidden)]
pub fn try_lock(&self) -> Option<ReentrantLockGuard<'_, T>> {
let this_thread = current_id();
// Safety: We only touch lock_count when we own the inner mutex.
// Additionally, we only call `self.owner.set()` while holding
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::sync::mpsc::{TryRecvError, channel};
use crate::sync::{Arc, Barrier};
use crate::thread;
use std::sync::mpsc::{TryRecvError, channel};
use std::sync::{Arc, Barrier};
use std::thread;

#[test]
#[cfg_attr(any(target_os = "emscripten", target_os = "wasi"), ignore)] // no threads
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::sync::atomic::{AtomicBool, Ordering};
use crate::sync::mpsc::channel;
use crate::sync::{Arc, Condvar, Mutex};
use crate::thread;
use crate::time::Duration;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::channel;
use std::sync::{Arc, Condvar, Mutex};
use std::thread;
use std::time::Duration;

#[test]
fn smoke() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::cell::LazyCell;
use crate::sync::atomic::AtomicUsize;
use crate::sync::atomic::Ordering::SeqCst;
use crate::sync::{LazyLock, Mutex, OnceLock};
use crate::{panic, thread};
use std::cell::LazyCell;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering::SeqCst;
use std::sync::{LazyLock, Mutex, OnceLock};
use std::{panic, thread};

fn spawn_and_wait<R: Send + 'static>(f: impl FnOnce() -> R + Send + 'static) -> R {
thread::spawn(f).join().unwrap()
Expand Down Expand Up @@ -149,7 +149,7 @@ fn is_sync_send() {
#[should_panic = "has previously been poisoned"]
fn lazy_force_mut_panic() {
let mut lazy = LazyLock::<String>::new(|| panic!());
crate::panic::catch_unwind(crate::panic::AssertUnwindSafe(|| {
panic::catch_unwind(panic::AssertUnwindSafe(|| {
let _ = LazyLock::force_mut(&mut lazy);
}))
.unwrap_err();
Expand Down
32 changes: 32 additions & 0 deletions library/std/tests/sync/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#![feature(lazy_get)]
#![feature(mapped_lock_guards)]
#![feature(mpmc_channel)]
#![feature(once_cell_try)]
#![feature(once_wait)]
#![feature(lock_value_accessors)]
#![feature(reentrant_lock)]
#![feature(rwlock_downgrade)]
#![feature(std_internals)]
#![allow(internal_features)]

mod barrier;
mod condvar;
mod lazy_lock;
#[cfg(not(any(target_os = "emscripten", target_os = "wasi")))]
mod mpmc;
#[cfg(not(any(target_os = "emscripten", target_os = "wasi")))]
mod mpsc;
#[cfg(not(any(target_os = "emscripten", target_os = "wasi")))]
mod mpsc_sync;
#[cfg(not(any(target_os = "emscripten", target_os = "wasi")))]
mod mutex;
#[cfg(not(any(target_os = "emscripten", target_os = "wasi")))]
mod once;
mod once_lock;
#[cfg(not(any(target_os = "emscripten", target_os = "wasi")))]
mod reentrant_lock;
#[cfg(not(any(target_os = "emscripten", target_os = "wasi")))]
mod rwlock;

#[path = "../common/mod.rs"]
mod common;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::*;
use crate::{env, thread};
use std::sync::mpmc::*;
use std::time::{Duration, Instant};
use std::{env, thread};

pub fn stress_factor() -> usize {
match env::var("RUST_TEST_STRESS") {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::*;
use crate::{env, thread};
use std::sync::mpsc::*;
use std::time::{Duration, Instant};
use std::{env, thread};

pub fn stress_factor() -> usize {
match env::var("RUST_TEST_STRESS") {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::*;
use crate::rc::Rc;
use crate::sync::mpmc::SendTimeoutError;
use crate::{env, thread};
use std::rc::Rc;
use std::sync::mpmc::SendTimeoutError;
use std::sync::mpsc::*;
use std::time::Duration;
use std::{env, thread};

pub fn stress_factor() -> usize {
match env::var("RUST_TEST_STRESS") {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::fmt::Debug;
use crate::ops::FnMut;
use crate::panic::{self, AssertUnwindSafe};
use crate::sync::atomic::{AtomicUsize, Ordering};
use crate::sync::mpsc::channel;
use crate::sync::{Arc, Condvar, MappedMutexGuard, Mutex, MutexGuard, TryLockError};
use crate::{hint, mem, thread};
use std::fmt::Debug;
use std::ops::FnMut;
use std::panic::{self, AssertUnwindSafe};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::mpsc::channel;
use std::sync::{Arc, Condvar, MappedMutexGuard, Mutex, MutexGuard, TryLockError};
use std::{hint, mem, thread};

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::Once;
use crate::sync::atomic::AtomicBool;
use crate::sync::atomic::Ordering::Relaxed;
use crate::sync::mpsc::channel;
use crate::time::Duration;
use crate::{panic, thread};
use std::sync::Once;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::Relaxed;
use std::sync::mpsc::channel;
use std::time::Duration;
use std::{panic, thread};

#[test]
fn smoke_once() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::sync::OnceLock;
use crate::sync::atomic::AtomicUsize;
use crate::sync::atomic::Ordering::SeqCst;
use crate::sync::mpsc::channel;
use crate::{panic, thread};
use std::sync::OnceLock;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering::SeqCst;
use std::sync::mpsc::channel;
use std::{panic, thread};

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

#[test]
fn sync_once_cell_get_unchecked() {
let c = OnceLock::new();
c.set(92).unwrap();
unsafe {
assert_eq!(c.get_unchecked(), &92);
}
}

#[test]
#[cfg_attr(any(target_os = "emscripten", target_os = "wasi"), ignore)] // no threads
fn sync_once_cell_drop() {
Expand Down Expand Up @@ -88,7 +79,6 @@ fn get_or_try_init() {

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

assert_eq!(cell.get_or_try_init(|| Err(())), Err(()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::ReentrantLock;
use crate::cell::RefCell;
use crate::sync::Arc;
use crate::thread;
use std::cell::RefCell;
use std::sync::{Arc, ReentrantLock};
use std::thread;

#[test]
fn smoke() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use rand::Rng;

use crate::fmt::Debug;
use crate::ops::FnMut;
use crate::panic::{self, AssertUnwindSafe};
use crate::sync::atomic::{AtomicUsize, Ordering};
use crate::sync::mpsc::channel;
use crate::sync::{
use std::fmt::Debug;
use std::ops::FnMut;
use std::panic::{self, AssertUnwindSafe};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::mpsc::channel;
use std::sync::{
Arc, MappedRwLockReadGuard, MappedRwLockWriteGuard, RwLock, RwLockReadGuard, RwLockWriteGuard,
TryLockError,
};
use crate::{hint, mem, thread};
use std::{hint, mem, thread};

use rand::Rng;

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

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

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

0 comments on commit f301e4d

Please sign in to comment.