1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#[cfg(test)]
use std::ops::Add;
#[cfg(test)]
use std::time::Duration;
use std::time::Instant;
use crate::MetricValue;
#[derive(Debug, Copy, Clone)]
pub struct TimeHandle(Instant);
impl TimeHandle {
pub fn now() -> TimeHandle {
TimeHandle(now())
}
pub fn elapsed_us(self) -> u64 {
let duration = now() - self.0;
(duration.as_secs() * 1_000_000) + u64::from(duration.subsec_micros())
}
pub fn elapsed_ms(self) -> MetricValue {
(self.elapsed_us() / 1000) as isize
}
}
impl Default for TimeHandle {
fn default() -> Self {
TimeHandle::now()
}
}
use std::cell::RefCell;
thread_local! {
static MOCK_CLOCK: RefCell<Instant> = RefCell::new(Instant::now());
}
#[cfg(test)]
pub fn mock_clock_reset() {
if !cfg!(not(test)) {
warn!("Mock clock used outside of cfg[]tests has no effect")
}
MOCK_CLOCK.with(|now| {
*now.borrow_mut() = Instant::now();
})
}
#[cfg(test)]
pub fn mock_clock_advance(period: Duration) {
MOCK_CLOCK.with(|now| {
let mut now = now.borrow_mut();
*now = now.add(period);
})
}
#[cfg(not(test))]
fn now() -> Instant {
Instant::now()
}
#[cfg(test)]
fn now() -> Instant {
MOCK_CLOCK.with(|now| *now.borrow())
}