omaha_client::time::time_source

Struct MockTimeSource

Source
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

use omaha_client::time::MockTimeSource;
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
use omaha_client::time::{ComplexTime, MockTimeSource, TimeSource};
use std::time::{Duration, Instant, SystemTime};
let mut mock_source = MockTimeSource::new_from_now();
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
use omaha_client::time::{ComplexTime, MockTimeSource, TimeSource};
use std::time::{Duration, Instant, SystemTime};
let mut one_mock_source = MockTimeSource::new_from_now();
let initial_time: ComplexTime = 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 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

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

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

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

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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

Source§

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

§

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

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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