diff --git a/maitake-sync/src/mutex/tests.rs b/maitake-sync/src/mutex/tests.rs index 43b7b147..a3d4cc2c 100644 --- a/maitake-sync/src/mutex/tests.rs +++ b/maitake-sync/src/mutex/tests.rs @@ -1,6 +1,6 @@ -use mutex_traits::ScopedRawMutex; - +use super::*; use crate::loom::{self, future}; +use crate::util::test::{assert_future, NopRawMutex}; use crate::Mutex; #[test] @@ -57,49 +57,11 @@ fn basic_multi_threaded() { }); } -struct NopRawMutex; - -unsafe impl ScopedRawMutex for NopRawMutex { - fn try_with_lock(&self, _: impl FnOnce() -> R) -> Option { - None - } - - fn with_lock(&self, _: impl FnOnce() -> R) -> R { - unimplemented!("this doesn't actually do anything") - } - - fn is_locked(&self) -> bool { - true - } -} - -fn assert_future(_: F) {} - #[test] fn lock_future_impls_future() { - loom::model(|| { - // Mutex with `DefaultMutex` as the `ScopedRawMutex` implementation - let mutex = Mutex::new(()); - assert_future(mutex.lock()); - - // Mutex with a custom `ScopedRawMutex` implementation - let mutex = Mutex::new_with_raw_mutex((), NopRawMutex); - assert_future(mutex.lock()); - }) -} - -#[test] -#[cfg(feature = "alloc")] -fn lock_owned_future_impls_future() { - loom::model(|| { - use alloc::sync::Arc; - - // Mutex with `DefaultMutex` as the `ScopedRawMutex` implementation - let mutex = Arc::new(Mutex::new(())); - assert_future(mutex.lock_owned()); + // Mutex with `DefaultMutex` as the `ScopedRawMutex` implementation + assert_future::>(); - // Mutex with a custom `ScopedRawMutex` implementation - let mutex = Arc::new(Mutex::new_with_raw_mutex((), NopRawMutex)); - assert_future(mutex.lock_owned()); - }) + // Mutex with a custom `ScopedRawMutex` implementation + assert_future::>(); } diff --git a/maitake-sync/src/rwlock/tests.rs b/maitake-sync/src/rwlock/tests.rs index 86edfa09..67167c3d 100644 --- a/maitake-sync/src/rwlock/tests.rs +++ b/maitake-sync/src/rwlock/tests.rs @@ -1,5 +1,5 @@ use super::*; -use crate::util; +use crate::util::test::assert_send_sync; #[cfg(any(loom, feature = "alloc"))] mod loom; @@ -9,15 +9,15 @@ mod sequential; #[test] fn lock_is_send_sync() { - util::test::assert_send_sync::>(); + assert_send_sync::>(); } #[test] fn read_guard_is_send_sync() { - util::test::assert_send_sync::>(); + assert_send_sync::>(); } #[test] fn write_guard_is_send_sync() { - util::test::assert_send_sync::>(); + assert_send_sync::>(); } diff --git a/maitake-sync/src/semaphore/tests.rs b/maitake-sync/src/semaphore/tests.rs index 0cc68dc7..4381dbed 100644 --- a/maitake-sync/src/semaphore/tests.rs +++ b/maitake-sync/src/semaphore/tests.rs @@ -1,19 +1,28 @@ use super::*; -use crate::util; +use crate::util::test::{assert_future, assert_send_sync, NopRawMutex}; #[test] fn semaphore_is_send_and_sync() { - util::test::assert_send_sync::(); + assert_send_sync::(); } #[test] fn permit_is_send_and_sync() { - util::test::assert_send_sync::>(); + assert_send_sync::>(); } #[test] fn acquire_is_send_and_sync() { - util::test::assert_send_sync::>(); + assert_send_sync::>(); +} + +#[test] +fn acquire_is_future() { + // Semaphore with `DefaultRawMutex` + assert_future::>(); + + // Semaphore with overridden `ScopedRawMutex` + assert_future::>(); } #[cfg(feature = "alloc")] @@ -22,12 +31,18 @@ mod owned { #[test] fn owned_permit_is_send_and_sync() { - util::test::assert_send_sync::(); + assert_send_sync::(); } #[test] fn acquire_owned_is_send_and_sync() { - util::test::assert_send_sync::(); + assert_send_sync::(); + } + + #[test] + fn acquire_owned_is_future() { + assert_future::(); + assert_future::>(); } } diff --git a/maitake-sync/src/util.rs b/maitake-sync/src/util.rs index 0e4b5a08..6fb5dba6 100644 --- a/maitake-sync/src/util.rs +++ b/maitake-sync/src/util.rs @@ -263,4 +263,27 @@ pub(crate) mod test { #[allow(dead_code)] pub(crate) fn assert_sync() {} pub(crate) fn assert_send_sync() {} + + pub(crate) fn assert_future() {} + + pub(crate) struct NopRawMutex; + + unsafe impl mutex_traits::RawMutex for NopRawMutex { + type GuardMarker = (); + fn lock(&self) { + unimplemented!("don't actually try to lock this thing") + } + + fn is_locked(&self) -> bool { + unimplemented!("don't actually try to lock this thing") + } + + fn try_lock(&self) -> bool { + unimplemented!("don't actually try to lock this thing") + } + + unsafe fn unlock(&self) { + unimplemented!("don't actually try to lock this thing") + } + } } diff --git a/maitake-sync/src/wait_map/tests.rs b/maitake-sync/src/wait_map/tests.rs index f4165fad..73f0957f 100644 --- a/maitake-sync/src/wait_map/tests.rs +++ b/maitake-sync/src/wait_map/tests.rs @@ -1,5 +1,24 @@ use super::*; +use crate::util::test::{assert_future, NopRawMutex}; #[cfg(all(not(loom), feature = "alloc"))] mod alloc_tests; #[cfg(any(loom, feature = "alloc"))] mod loom; + +#[test] +fn wait_future_is_future() { + // WaitMap with default mutex. + assert_future::>(); + + // WaitMap with overridden `ScopedRawMutex`. + assert_future::>(); +} + +#[test] +fn subscribe_future_is_future() { + // WaitMap with default mutex. + assert_future::>(); + + // WaitMap with overridden `ScopedRawMutex`. + assert_future::>(); +} diff --git a/maitake-sync/src/wait_map/tests/alloc_tests.rs b/maitake-sync/src/wait_map/tests/alloc_tests.rs index 9d6b6988..d225cc0b 100644 --- a/maitake-sync/src/wait_map/tests/alloc_tests.rs +++ b/maitake-sync/src/wait_map/tests/alloc_tests.rs @@ -1,5 +1,6 @@ use super::super::*; use crate::loom::sync::Arc; +use crate::util::test::{assert_future, NopRawMutex}; use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use futures::{future::poll_fn, pin_mut, select_biased, FutureExt}; use tokio_test::{assert_pending, assert_ready, assert_ready_err, task}; @@ -402,3 +403,12 @@ fn drop_wake_bailed() { assert_eq!(KEY_DROPS.load(Ordering::Relaxed), TASKS); assert_eq!(VAL_DROPS.load(Ordering::Relaxed), TASKS); } + +#[test] +fn wait_owned_future_is_future() { + // WaitMap with default mutex. + assert_future::>(); + + // WaitMap with overridden `ScopedRawMutex`. + assert_future::>(); +} diff --git a/maitake-sync/src/wait_queue/tests.rs b/maitake-sync/src/wait_queue/tests.rs index 52843ef5..cfed9e2d 100644 --- a/maitake-sync/src/wait_queue/tests.rs +++ b/maitake-sync/src/wait_queue/tests.rs @@ -1,8 +1,16 @@ -#[cfg(any(loom, feature = "alloc"))] use super::*; +use crate::util::test::{assert_future, NopRawMutex}; #[cfg(all(not(loom), feature = "alloc"))] mod alloc_tests; #[cfg(any(loom, feature = "alloc"))] mod loom; + +#[test] +fn wait_future_is_future() { + // WaitQueue with default raw mutex + assert_future::>(); + // WaitQueue with overridden raw mutex + assert_future::>(); +} diff --git a/maitake-sync/src/wait_queue/tests/alloc_tests.rs b/maitake-sync/src/wait_queue/tests/alloc_tests.rs index 802f575c..80326851 100644 --- a/maitake-sync/src/wait_queue/tests/alloc_tests.rs +++ b/maitake-sync/src/wait_queue/tests/alloc_tests.rs @@ -1,5 +1,6 @@ use super::*; use crate::loom::sync::Arc; +use crate::util::test::{assert_future, NopRawMutex}; use core::sync::atomic::{AtomicUsize, Ordering}; use tokio_test::{assert_pending, assert_ready, assert_ready_ok, task}; @@ -169,3 +170,12 @@ fn subscribe_consumes_wakeup() { let mut future2 = task::spawn(q.wait()); future2.enter(|_, f| assert_pending!(f.subscribe())); } + +#[test] +fn wait_owned_future_is_future() { + // WaitQueue with default raw mutex + assert_future::(); + + // WaitQueue with overridden raw mutex + assert_future::>(); +}