Skip to main content

MockMonitor

Struct MockMonitor 

Source
pub struct MockMonitor<T> { /* private fields */ }
Expand description

Monitor implementation for deterministic tests.

MockMonitor protects a state value like the real monitor implementations, but timeout methods use manually controlled mock elapsed time. Advancing the mock time wakes waiters so they can recheck predicates and timeout budgets.

Implementations§

Source§

impl<T> MockMonitor<T>

Source

pub fn new(state: T) -> Self

Creates a mock monitor protecting the supplied state value.

§Arguments
  • state - Initial protected state.
§Returns

A mock monitor whose elapsed time starts at zero.

Source

pub fn elapsed(&self) -> Duration

Returns the current mock elapsed time.

§Returns

The elapsed time used by timeout waits.

Source

pub fn set_elapsed(&self, elapsed: Duration)

Sets the current mock elapsed time.

This wakes timeout waiters so they can recheck their budgets.

§Arguments
  • elapsed - New mock elapsed time.
Source

pub fn advance(&self, duration: Duration)

Advances mock elapsed time by a relative duration.

§Arguments
  • duration - Duration added to the current mock elapsed time.
Source

pub fn reset_elapsed(&self)

Resets mock elapsed time to zero.

Source

pub fn read<R, F>(&self, f: F) -> R
where F: FnOnce(&T) -> R,

Acquires the monitor and reads the protected state.

§Arguments
  • f - Closure that receives an immutable reference to the state.
§Returns

The value returned by the closure.

Source

pub fn write<R, F>(&self, f: F) -> R
where F: FnOnce(&mut T) -> R,

Acquires the monitor and mutates the protected state.

This does not notify waiters automatically.

§Arguments
  • f - Closure that receives a mutable reference to the state.
§Returns

The value returned by the closure.

Source

pub fn write_notify_one<R, F>(&self, f: F) -> R
where F: FnOnce(&mut T) -> R,

Mutates the protected state and wakes one waiter.

§Arguments
  • f - Closure that receives a mutable reference to the state.
§Returns

The value returned by the closure.

Source

pub fn write_notify_all<R, F>(&self, f: F) -> R
where F: FnOnce(&mut T) -> R,

Mutates the protected state and wakes all waiters.

§Arguments
  • f - Closure that receives a mutable reference to the state.
§Returns

The value returned by the closure.

Source

pub fn notify_one(&self)

Wakes one waiter if one is blocked.

Source

pub fn notify_all(&self)

Wakes all waiters.

Trait Implementations§

Source§

impl<T> AsRef<MockMonitor<T>> for ArcMockMonitor<T>

Source§

fn as_ref(&self) -> &MockMonitor<T>

Returns a reference to the wrapped mock monitor.

Source§

impl<T: Send> AsyncConditionWaiter for MockMonitor<T>

Available on crate feature async only.
Source§

fn async_wait_until<'a, R, P, F>( &'a self, predicate: P, action: F, ) -> AsyncMonitorFuture<'a, R>
where R: Send + 'a, P: FnMut(&Self::State) -> bool + Send + 'a, F: FnOnce(&mut Self::State) -> R + Send + 'a,

Returns a future that waits until the predicate becomes true.

Source§

fn async_wait_while<'a, R, P, F>( &'a self, predicate: P, action: F, ) -> AsyncMonitorFuture<'a, R>
where R: Send + 'a, P: FnMut(&Self::State) -> bool + Send + 'a, F: FnOnce(&mut Self::State) -> R + Send + 'a,

Returns a future that waits while the predicate remains true.

Source§

type State = T

State protected by the monitor.
Source§

impl<T: Send> AsyncNotificationWaiter for MockMonitor<T>

Available on crate feature async only.
Source§

fn async_wait<'a>(&'a self) -> AsyncMonitorFuture<'a, ()>

Returns a future that resolves after an async notification.

Source§

impl<T: Send> AsyncTimeoutConditionWaiter for MockMonitor<T>

Available on crate feature async only.
Source§

fn async_wait_until_for<'a, R, P, F>( &'a self, timeout: Duration, predicate: P, action: F, ) -> AsyncMonitorFuture<'a, WaitTimeoutResult<R>>
where R: Send + 'a, P: FnMut(&Self::State) -> bool + Send + 'a, F: FnOnce(&mut Self::State) -> R + Send + 'a,

Returns a future that waits until the predicate becomes true or times out.

Source§

fn async_wait_while_for<'a, R, P, F>( &'a self, timeout: Duration, predicate: P, action: F, ) -> AsyncMonitorFuture<'a, WaitTimeoutResult<R>>
where R: Send + 'a, P: FnMut(&Self::State) -> bool + Send + 'a, F: FnOnce(&mut Self::State) -> R + Send + 'a,

Returns a future that waits while the predicate remains true or times out.

Source§

impl<T: Send> AsyncTimeoutNotificationWaiter for MockMonitor<T>

Available on crate feature async only.
Source§

fn async_wait_for<'a>( &'a self, timeout: Duration, ) -> AsyncMonitorFuture<'a, WaitTimeoutStatus>

Returns a future that resolves after notification or mock timeout.

Source§

impl<T> ConditionWaiter for MockMonitor<T>

Source§

fn wait_until<R, P, F>(&self, predicate: P, action: F) -> R
where P: FnMut(&Self::State) -> bool, F: FnOnce(&mut Self::State) -> R,

Blocks until the predicate becomes true, then runs the action.

Source§

fn wait_while<R, P, F>(&self, predicate: P, action: F) -> R
where P: FnMut(&Self::State) -> bool, F: FnOnce(&mut Self::State) -> R,

Blocks while the predicate remains true, then runs the action.

Source§

type State = T

State protected by the monitor.
Source§

impl<T: Default> Default for MockMonitor<T>

Source§

fn default() -> Self

Creates a mock monitor containing T::default().

Source§

impl<T> From<T> for MockMonitor<T>

Source§

fn from(value: T) -> Self

Creates a mock monitor from an initial state value.

Source§

impl<T> NotificationWaiter for MockMonitor<T>

Source§

fn wait(&self)

Blocks until a notification happens after this call starts.

Source§

impl<T> Notifier for MockMonitor<T>

Source§

fn notify_one(&self)

Wakes one waiter if one is blocked.

Source§

fn notify_all(&self)

Wakes all waiters.

Source§

impl<T> TimeoutConditionWaiter for MockMonitor<T>

Source§

fn wait_until_for<R, P, F>( &self, timeout: Duration, predicate: P, action: F, ) -> WaitTimeoutResult<R>
where P: FnMut(&Self::State) -> bool, F: FnOnce(&mut Self::State) -> R,

Blocks until the predicate becomes true or mock elapsed time reaches timeout.

Source§

fn wait_while_for<R, P, F>( &self, timeout: Duration, predicate: P, action: F, ) -> WaitTimeoutResult<R>
where P: FnMut(&Self::State) -> bool, F: FnOnce(&mut Self::State) -> R,

Blocks while the predicate remains true or until mock elapsed time reaches timeout.

Source§

impl<T> TimeoutNotificationWaiter for MockMonitor<T>

Source§

fn wait_for(&self, timeout: Duration) -> WaitTimeoutStatus

Blocks until a notification happens or mock elapsed time reaches timeout.

Auto Trait Implementations§

§

impl<T> !Freeze for MockMonitor<T>

§

impl<T> RefUnwindSafe for MockMonitor<T>

§

impl<T> Send for MockMonitor<T>
where T: Send,

§

impl<T> Sync for MockMonitor<T>
where T: Send,

§

impl<T> Unpin for MockMonitor<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for MockMonitor<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for MockMonitor<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> AsyncMonitor for T

Source§

impl<T> Monitor for T