brokapi_runtime/clock.rs
1//! The clock the connectors read.
2//!
3//! The sans-io cores take the current time as an argument (`std::time::Instant`). The connectors read
4//! it at the edge, where the network is. They read it here, not from `Instant::now()`, so that
5//! a test can freeze and move the time with `tokio::time::pause` and `tokio::time::advance`, and
6//! the rate limiter, the command timeouts and the waits of the connector all follow it.
7//!
8//! Outside a paused test the value is the same as `std::time::Instant::now()`.
9
10use std::time::Instant;
11
12/// The current instant of the runtime clock.
13///
14/// Equal to `std::time::Instant::now()` in a program. In a test with a paused tokio clock it is the
15/// virtual instant, so a rate limit that refills after one second refills when the test calls
16/// `tokio::time::advance(Duration::from_secs(1))`, without a real pause.
17///
18/// # Examples
19///
20/// ```
21/// use std::time::Duration;
22///
23/// # #[tokio::main(flavor = "current_thread", start_paused = true)]
24/// # async fn main() {
25/// let before = brokapi_runtime::now();
26/// tokio::time::advance(Duration::from_secs(30)).await;
27/// assert!(brokapi_runtime::now() - before >= Duration::from_secs(30));
28/// # }
29/// ```
30#[must_use]
31pub fn now() -> Instant {
32 tokio::time::Instant::now().into_std()
33}