mirror of
https://github.com/chatmail/core.git
synced 2026-05-16 21:36:30 +03:00
refactor: Make SystemTimeTools::shift() async
It's a more stable interface, if `shift()` suddenly will need to wait for smth like finalisation of parallel running critical sections assuming there are no time jumps, we don't need to fix all its usages.
This commit is contained in:
@@ -6,3 +6,4 @@ edition = "2021"
|
||||
license = "MPL-2.0"
|
||||
|
||||
[dependencies]
|
||||
tokio = { workspace = true }
|
||||
|
||||
@@ -13,11 +13,11 @@ impl SystemTimeTools {
|
||||
pub const UNIX_EPOCH: SystemTime = SystemTime::UNIX_EPOCH;
|
||||
|
||||
pub fn now() -> SystemTime {
|
||||
return SystemTime::now() + *SYSTEM_TIME_SHIFT.read().unwrap();
|
||||
SystemTime::now() + *SYSTEM_TIME_SHIFT.read().unwrap()
|
||||
}
|
||||
|
||||
/// Simulates a system clock forward adjustment by `duration`.
|
||||
pub fn shift(duration: Duration) {
|
||||
pub async fn shift(duration: Duration) {
|
||||
*SYSTEM_TIME_SHIFT.write().unwrap() += duration;
|
||||
}
|
||||
}
|
||||
@@ -26,9 +26,9 @@ impl SystemTimeTools {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
SystemTimeTools::shift(Duration::from_secs(60));
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn it_works() {
|
||||
SystemTimeTools::shift(Duration::from_secs(60)).await;
|
||||
let t = SystemTimeTools::now();
|
||||
assert!(t > SystemTime::now());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user