broker_tokio/time/
clock.rs1cfg_not_test_util! {
8 use crate::time::{Duration, Instant};
9
10 #[derive(Debug, Clone)]
11 pub(crate) struct Clock {}
12
13 pub(crate) fn now() -> Instant {
14 Instant::from_std(std::time::Instant::now())
15 }
16
17 impl Clock {
18 pub(crate) fn new() -> Clock {
19 Clock {}
20 }
21
22 pub(crate) fn now(&self) -> Instant {
23 now()
24 }
25
26 pub(crate) fn is_frozen(&self) -> bool {
27 false
28 }
29
30 pub(crate) fn advance(&self, _dur: Duration) {
31 unreachable!();
32 }
33 }
34}
35
36cfg_test_util! {
37 use crate::time::{Duration, Instant};
38 use std::sync::{Arc, Mutex};
39 use crate::runtime::context;
40
41 #[derive(Debug, Clone)]
43 pub(crate) struct Clock {
44 inner: Arc<Inner>,
45 }
46
47 #[derive(Debug)]
48 struct Inner {
49 start: std::time::Instant,
51
52 frozen: Mutex<Option<Duration>>,
54 }
55
56 pub fn pause() {
67 let clock = context::clock().expect("time cannot be frozen from outside the Tokio runtime");
68 let mut frozen = clock.inner.frozen.lock().unwrap();
69 if frozen.is_some() {
70 panic!("time is already frozen");
71 }
72 *frozen = Some(clock.inner.start.elapsed());
73 }
74
75 pub fn resume() {
85 let clock = context::clock().expect("time cannot be frozen from outside the Tokio runtime");
86 let mut frozen = clock.inner.frozen.lock().unwrap();
87
88 if frozen.is_none() {
89 panic!("time is not frozen");
90 }
91
92 *frozen = None;
93 }
94
95 pub async fn advance(duration: Duration) {
105 let clock = context::clock().expect("time cannot be frozen from outside the Tokio runtime");
106 clock.advance(duration);
107 crate::task::yield_now().await;
108 }
109
110 pub(crate) fn now() -> Instant {
112 if let Some(clock) = context::clock() {
113 if let Some(frozen) = *clock.inner.frozen.lock().unwrap() {
114 Instant::from_std(clock.inner.start + frozen)
115 } else {
116 Instant::from_std(std::time::Instant::now())
117 }
118 } else {
119 Instant::from_std(std::time::Instant::now())
120 }
121 }
122
123 impl Clock {
124 pub(crate) fn new() -> Clock {
127 Clock {
128 inner: Arc::new(Inner {
129 start: std::time::Instant::now(),
130 frozen: Mutex::new(None),
131 }),
132 }
133 }
134
135 #[cfg(all(test, not(loom)))]
137 pub(crate) fn new_frozen() -> Clock {
140 Clock {
141 inner: Arc::new(Inner {
142 start: std::time::Instant::now(),
143 frozen: Mutex::new(Some(Duration::from_millis(0))),
144 }),
145 }
146 }
147
148 pub(crate) fn is_frozen(&self) -> bool {
149 self.inner.frozen.lock().unwrap().is_some()
150 }
151
152 pub(crate) fn advance(&self, duration: Duration) {
153 let mut frozen = self.inner.frozen.lock().unwrap();
154
155 if let Some(ref mut elapsed) = *frozen {
156 *elapsed += duration;
157 } else {
158 panic!("time is not frozen");
159 }
160 }
161
162 #[cfg(all(test, not(loom)))]
164 pub(crate) fn advanced(&self) -> Duration {
165 self.inner.frozen.lock().unwrap().unwrap()
166 }
167
168 pub(crate) fn now(&self) -> Instant {
169 Instant::from_std(if let Some(frozen) = *self.inner.frozen.lock().unwrap() {
170 self.inner.start + frozen
171 } else {
172 std::time::Instant::now()
173 })
174 }
175 }
176}