MockTimeSource

Struct MockTimeSource 

Source
pub struct MockTimeSource;
Expand description

Mock time source for deterministic testing.

MockTimeSource provides a controllable time source where elapsed time can be manually set and advanced. This enables deterministic testing of rate limiting behavior without depending on real system time.

§Time Model

MockTimeSource maintains an elapsed Duration since an implicit starting point. Time starts at Duration::ZERO and can be advanced or set to specific values. Time is always monotonic - attempting to set time backward will be ignored.

§Thread Safety

MockTimeSource uses thread-local storage, so each thread has its own independent mock time. This is perfect for testing scenarios where you want isolated time control per test.

§Examples

§Basic Usage

use rate_guard::time_source::{TimeSource, MockTimeSource};
use std::time::Duration;

let time_source = MockTimeSource::new();

// Check initial state
assert_eq!(time_source.now(), Duration::ZERO);

// Advance time
time_source.advance(Duration::from_millis(50));
assert_eq!(time_source.now(), Duration::from_millis(50));

// Advance more
time_source.advance(Duration::from_millis(30));
assert_eq!(time_source.now(), Duration::from_millis(80));

§Absolute Time Setting

use rate_guard::time_source::{TimeSource, MockTimeSource};
use std::time::Duration;

let time_source = MockTimeSource::new();

// Jump to specific time
time_source.set_elapsed(Duration::from_secs(10));
assert_eq!(time_source.now(), Duration::from_secs(10));

// Reset to zero
time_source.reset();
assert_eq!(time_source.now(), Duration::ZERO);

§Rate Limiter Integration

use rate_guard::time_source::{TimeSource, MockTimeSource};
use std::time::Duration;

let time_source = MockTimeSource::new();

// Simulate token bucket refill timing
let refill_interval = Duration::from_millis(100);
let initial_time = time_source.now();

// Advance past refill interval
time_source.advance(refill_interval);
let elapsed = time_source.now() - initial_time;
assert!(elapsed >= refill_interval);

Implementations§

Source§

impl MockTimeSource

Source

pub fn new() -> Self

Creates a new MockTimeSource.

The time source starts with elapsed time of Duration::ZERO. Each instance shares the same thread-local time state within the current thread.

§Examples
use rate_guard::time_source::{TimeSource, MockTimeSource};
use std::time::Duration;

let time_source = MockTimeSource::new();
assert_eq!(time_source.now(), Duration::ZERO);
Source

pub fn set_elapsed(&self, elapsed: Duration)

Sets the elapsed time to a specific value.

This method allows tests to jump to any point in time. The time must be greater than or equal to the current elapsed time to maintain monotonicity. Attempts to set time backward are ignored.

§Arguments
  • elapsed - The new elapsed time value
§Examples
use rate_guard::time_source::{TimeSource, MockTimeSource};
use std::time::Duration;

let time_source = MockTimeSource::new();

time_source.set_elapsed(Duration::from_millis(1000));
assert_eq!(time_source.now(), Duration::from_millis(1000));

// Attempting to go backward is ignored
time_source.set_elapsed(Duration::from_millis(500));
assert_eq!(time_source.now(), Duration::from_millis(1000));
Source

pub fn advance(&self, duration: Duration)

Advances the elapsed time by the specified duration.

This method adds the specified duration to the current elapsed time, simulating the passage of time in tests. The advancement is always forward, maintaining monotonicity.

§Arguments
  • duration - Duration to advance the time by
§Examples
use rate_guard::time_source::{TimeSource, MockTimeSource};
use std::time::Duration;

let time_source = MockTimeSource::new();

time_source.advance(Duration::from_millis(50));
assert_eq!(time_source.now(), Duration::from_millis(50));

time_source.advance(Duration::from_millis(25));
assert_eq!(time_source.now(), Duration::from_millis(75));
Source

pub fn reset(&self)

Resets the elapsed time to zero.

This is a convenience method equivalent to set_elapsed(Duration::ZERO). Useful for resetting time state between tests.

§Examples
use rate_guard::time_source::{TimeSource, MockTimeSource};
use std::time::Duration;

let time_source = MockTimeSource::new();
time_source.advance(Duration::from_secs(5));
assert_eq!(time_source.now(), Duration::from_secs(5));

time_source.reset();
assert_eq!(time_source.now(), Duration::ZERO);
Source

pub fn elapsed(&self) -> Duration

Returns the current elapsed time.

This is equivalent to calling the now() method from the TimeSource trait, but provided as a convenience method with a more descriptive name.

§Examples
use rate_guard::time_source::MockTimeSource;
use std::time::Duration;

let time_source = MockTimeSource::new();
time_source.advance(Duration::from_millis(100));

assert_eq!(time_source.elapsed(), Duration::from_millis(100));
assert_eq!(time_source.elapsed(), time_source.now());
Source

pub fn now(&self) -> Duration

Trait Implementations§

Source§

impl Clone for MockTimeSource

Source§

fn clone(&self) -> MockTimeSource

Returns a duplicate 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 Default for MockTimeSource

Source§

fn default() -> Self

Creates a new MockTimeSource with default settings.

Equivalent to MockTimeSource::new().

Source§

impl TimeSource for MockTimeSource

Source§

fn now(&self) -> Duration

Returns the current elapsed time since the mock starting point.

This method returns the elapsed Duration that has been set or advanced through the mock time control methods.

§Examples
use rate_guard::time_source::{TimeSource, MockTimeSource};
use std::time::Duration;

let time_source = MockTimeSource::new();
assert_eq!(time_source.now(), Duration::ZERO);

time_source.advance(Duration::from_millis(123));
assert_eq!(time_source.now(), Duration::from_millis(123));

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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> 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.