pub struct MockClock { /* private fields */ }Expand description
Mock clock for testing that allows controlling time
This clock implementation allows tests to:
- Set a fixed time point
- Advance time manually without actual delays
- Make time-dependent tests deterministic
The clock uses interior mutability to allow advancing time
while implementing the Clock trait which takes &self.
§Example
use torrust_tracker_deployer_lib::testing::MockClock;
use torrust_tracker_deployer_lib::shared::Clock;
use chrono::{TimeZone, Utc};
let fixed_time = Utc.with_ymd_and_hms(2025, 10, 7, 12, 0, 0).unwrap();
let clock = MockClock::new(fixed_time);
// Time is fixed
assert_eq!(clock.now(), fixed_time);
assert_eq!(clock.now(), fixed_time); // Still the same
// Advance time
clock.advance_secs(60);
let expected = Utc.with_ymd_and_hms(2025, 10, 7, 12, 1, 0).unwrap();
assert_eq!(clock.now(), expected);Implementations§
Source§impl MockClock
impl MockClock
Sourcepub fn new(initial_time: DateTime<Utc>) -> Self
pub fn new(initial_time: DateTime<Utc>) -> Self
Create a new mock clock with a fixed starting time
§Example
use torrust_tracker_deployer_lib::testing::MockClock;
use chrono::{TimeZone, Utc};
let fixed_time = Utc.with_ymd_and_hms(2025, 10, 7, 12, 0, 0).unwrap();
let clock = MockClock::new(fixed_time);Sourcepub fn advance(&self, duration: Duration)
pub fn advance(&self, duration: Duration)
Advance the clock by the specified duration
§Panics
Panics if the internal mutex is poisoned (which would indicate a panic occurred while holding the lock in another thread).
§Example
use torrust_tracker_deployer_lib::testing::MockClock;
use torrust_tracker_deployer_lib::shared::Clock;
use chrono::{Duration, TimeZone, Utc};
let initial = Utc.with_ymd_and_hms(2025, 10, 7, 12, 0, 0).unwrap();
let clock = MockClock::new(initial);
clock.advance(Duration::hours(2));
let expected = Utc.with_ymd_and_hms(2025, 10, 7, 14, 0, 0).unwrap();
assert_eq!(clock.now(), expected);Sourcepub fn advance_secs(&self, secs: i64)
pub fn advance_secs(&self, secs: i64)
Advance the clock by the specified number of seconds
Convenience method for advancing time without creating a Duration.
§Example
use torrust_tracker_deployer_lib::testing::MockClock;
use torrust_tracker_deployer_lib::shared::Clock;
use chrono::{TimeZone, Utc};
let initial = Utc.with_ymd_and_hms(2025, 10, 7, 12, 0, 0).unwrap();
let clock = MockClock::new(initial);
clock.advance_secs(30);
let expected = Utc.with_ymd_and_hms(2025, 10, 7, 12, 0, 30).unwrap();
assert_eq!(clock.now(), expected);Sourcepub fn set_time(&self, time: DateTime<Utc>)
pub fn set_time(&self, time: DateTime<Utc>)
Set the clock to a specific time
§Panics
Panics if the internal mutex is poisoned (which would indicate a panic occurred while holding the lock in another thread).
§Example
use torrust_tracker_deployer_lib::testing::MockClock;
use torrust_tracker_deployer_lib::shared::Clock;
use chrono::{TimeZone, Utc};
let initial = Utc.with_ymd_and_hms(2025, 10, 7, 12, 0, 0).unwrap();
let clock = MockClock::new(initial);
let new_time = Utc.with_ymd_and_hms(2025, 12, 25, 18, 30, 0).unwrap();
clock.set_time(new_time);
assert_eq!(clock.now(), new_time);Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for MockClock
impl !UnwindSafe for MockClock
impl Freeze for MockClock
impl Send for MockClock
impl Sync for MockClock
impl Unpin for MockClock
impl UnsafeUnpin for MockClock
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request