tracing_throttle/infrastructure/
clock.rs

1//! Clock adapters for time operations.
2//!
3//! Provides SystemClock implementation for production use.
4//!
5//! # Testing
6//!
7//! See `MockClock` (in `crate::infrastructure::mocks`) for a controllable test clock.
8//! Available with the `test-helpers` feature or in test builds:
9//!
10//! ```toml
11//! [dev-dependencies]
12//! tracing-throttle = { version = "*", features = ["test-helpers"] }
13//! ```
14
15use crate::application::ports::Clock;
16use std::time::Instant;
17
18/// System clock implementation using `Instant::now()`.
19#[derive(Debug, Clone, Copy, Default)]
20pub struct SystemClock;
21
22impl SystemClock {
23    /// Create a new system clock.
24    pub fn new() -> Self {
25        Self
26    }
27}
28
29impl Clock for SystemClock {
30    fn now(&self) -> Instant {
31        Instant::now()
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38    use std::time::Duration;
39
40    #[test]
41    fn test_system_clock() {
42        let clock = SystemClock::new();
43        let t1 = clock.now();
44        std::thread::sleep(Duration::from_millis(10));
45        let t2 = clock.now();
46
47        assert!(t2 > t1);
48    }
49}