#![doc = include_str!("../README.md")]
#![warn(missing_docs)]
use std::{future::Future, time::Duration};
#[cfg(any(test, feature = "test"))]
mod real;
#[cfg(all(test, not(feature = "test")))]
const _: () = panic!("Trying to test `reord` without its `test` feature");
#[derive(Debug)]
#[non_exhaustive]
pub struct Config {
pub seed: u64,
pub maybe_lock_timeout: Duration,
pub check_named_locks_work_for: Option<Duration>,
pub check_addressed_locks_work_for: Option<Duration>,
}
impl Config {
pub fn with_random_seed() -> Config {
use rand::Rng;
let seed = rand::thread_rng().gen();
eprintln!("Running `reord` test with random seed {:?}", seed);
Config {
seed,
maybe_lock_timeout: Duration::from_millis(100),
check_addressed_locks_work_for: None,
check_named_locks_work_for: None,
}
}
pub fn from_seed(seed: u64) -> Config {
Config {
seed,
maybe_lock_timeout: Duration::from_millis(100),
check_addressed_locks_work_for: None,
check_named_locks_work_for: None,
}
}
}
#[inline]
#[allow(unused_variables)]
pub async fn init_test(config: Config) {
#[cfg(feature = "test")]
real::init_test(config).await
}
#[inline]
pub async fn new_task<T>(f: impl Future<Output = T>) -> T {
#[cfg(feature = "test")]
let res = real::new_task(f).await;
#[cfg(not(feature = "test"))]
let res = f.await;
res
}
#[inline]
#[allow(unused_variables)]
pub async fn start(tasks: usize) -> tokio::task::JoinHandle<()> {
#[cfg(not(feature = "test"))]
panic!("Trying to start a `reord` test, but the `test` feature is not set");
#[cfg(feature = "test")]
real::start(tasks).await
}
#[inline]
pub async fn point() {
#[cfg(feature = "test")]
real::point().await
}
#[inline]
pub async fn maybe_lock() {
#[cfg(feature = "test")]
real::maybe_lock().await
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum LockInfo {
Named(String),
Addressed(usize),
}
#[derive(Debug)]
pub struct Lock {
#[cfg(feature = "test")]
_data: real::Lock,
#[cfg(not(feature = "test"))]
_unused: (),
}
impl Lock {
#[inline]
#[allow(unused_variables)]
pub async fn take_named(name: String) -> Lock {
#[cfg(feature = "test")]
let res = Lock {
_data: real::Lock::take_named(name).await,
};
#[cfg(not(feature = "test"))]
let res = Lock { _unused: () };
res
}
#[inline]
#[allow(unused_variables)]
pub async fn take_addressed(address: usize) -> Lock {
#[cfg(feature = "test")]
let res = Lock {
_data: real::Lock::take_addressed(address).await,
};
#[cfg(not(feature = "test"))]
let res = Lock { _unused: () };
res
}
#[inline]
#[allow(unused_variables)]
pub async fn take_atomic(l: Vec<LockInfo>) -> Lock {
#[cfg(feature = "test")]
let res = Lock {
_data: real::Lock::take_atomic(l).await,
};
#[cfg(not(feature = "test"))]
let res = Lock { _unused: () };
res
}
}
impl Drop for Lock {
#[inline]
fn drop(&mut self) {}
}
#[cfg(test)]
mod tests;