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
impl MockTimeSource
Sourcepub fn new(t: impl Into<ComplexTime>) -> Self
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();
Sourcepub fn new_from_now() -> Self
pub fn new_from_now() -> Self
Create a new MockTimeSource
, initialized to the values from SystemTime
and Instant
Sourcepub fn advance(&mut self, duration: Duration)
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.
Sourcepub fn truncate_submicrosecond_walltime(&mut self)
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
impl Clone for MockTimeSource
Source§fn clone(&self) -> MockTimeSource
fn clone(&self) -> MockTimeSource
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for MockTimeSource
impl Debug for MockTimeSource
Source§impl TimeSource for MockTimeSource
impl TimeSource for MockTimeSource
Source§fn now_in_walltime(&self) -> SystemTime
fn now_in_walltime(&self) -> SystemTime
Source§fn now_in_monotonic(&self) -> Instant
fn now_in_monotonic(&self) -> Instant
Source§fn now(&self) -> ComplexTime
fn now(&self) -> ComplexTime
Auto Trait Implementations§
impl Freeze for MockTimeSource
impl RefUnwindSafe for MockTimeSource
impl Send for MockTimeSource
impl Sync for MockTimeSource
impl Unpin for MockTimeSource
impl UnwindSafe for MockTimeSource
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)