pub struct MockTimeSource { /* private fields */ }
Expand description

A mock TimeSource that can be manipulated as needed.

Meant to be used when writing tests. As SystemTime is partly opaque and is Instant completely are opaque, it needs to be initially constructed from a real source of those objects.

Example

let mock_source = MockTimeSource::new_from_now();

The MockTimeSource uses Clone-able interior mutability to allow it to be manipulated and used concurrently.

Implementations§

source§

impl MockTimeSource

source

pub fn new(t: impl Into<ComplexTime>) -> Self

Create a new MockTimeSource from a ComplexTime to be it’s initial time.

Example
let current_time = StandardTimeSource.now();
let mock_source = MockTimeSource.from(current_time);
mock_source.advance(Duration::from_secs(3600))
let next_time = mock_source.now();
source

pub fn new_from_now() -> Self

Create a new MockTimeSource, initialized to the values from SystemTime and Instant

source

pub fn advance(&mut self, duration: Duration)

Advance the mock time source forward (e.g. during a test)

If the MockTimeSource has been Cloned, this will advance both.

Example
let one_mock_source = MockTimeSource::from_now();
let initial_time = one_mock_source.now();
let two_mock_source = one_mock_source.clone();

let one_hour = Duration::from_secs(3600);
one_mock_source.advance(one_hour);
let later_time = one_mock_source.now();

assert_eq!(one_mock_source.now(), two_mock_source.now());
assert_eq!(one_mock_source.now(), initial_time + one_hour);
assert_eq!(two_mock_source.now(), initial_time + one_hour);

This method uses a mutable reference for self for clarity. The interior mutability of the time does not require it.

source

pub fn truncate_submicrosecond_walltime(&mut self)

Truncate the submicrosecond part of the walltime in mock time source. This is useful for tests that involves storing time in storage, which will lose submicrosecond precision.

Trait Implementations§

source§

impl Clone for MockTimeSource

source§

fn clone(&self) -> MockTimeSource

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for MockTimeSource

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl TimeSource for MockTimeSource

source§

fn now_in_walltime(&self) -> SystemTime

Returns the current wall time.
source§

fn now_in_monotonic(&self) -> Instant

Returns the current montonic time.
source§

fn now(&self) -> ComplexTime

Returns the current ComplexTime (both wall and monotonic times).

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere 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> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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 Twhere U: TryFrom<T>,

§

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> Unless for T

source§

fn unless(self, option: Option<T>) -> T

§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more