feat(types,blahd): allow using mock clock for testing

This commit is contained in:
oxalica 2024-09-28 21:22:38 -04:00
parent 1a4980ebba
commit 31dc3e33c6
8 changed files with 61 additions and 13 deletions

View file

@ -3,6 +3,10 @@ name = "blah-types"
version = "0.0.0"
edition = "2021"
[features]
default = []
unsafe_use_mock_instant_for_testing = ["dep:mock_instant"]
[[bench]]
name = "crypto_ops"
harness = false
@ -13,6 +17,7 @@ bitflags_serde_shim = "0.2"
ed25519-dalek = "2"
hex = { version = "0.4", features = ["serde"] }
html-escape = "0.2"
mock_instant = { version = "0.5", optional = true }
rand = "0.8"
rusqlite = { version = "0.32", optional = true }
serde = { version = "1", features = ["derive"] }

View file

@ -2,7 +2,6 @@
use std::fmt;
use std::str::FromStr;
use std::time::SystemTime;
use ed25519_dalek::{
Signature, SignatureError, Signer, SigningKey, VerifyingKey, PUBLIC_KEY_LENGTH,
@ -104,6 +103,12 @@ impl<T: Serialize> SignExt for T {
}
pub fn get_timestamp() -> u64 {
#[cfg(not(feature = "unsafe_use_mock_instant_for_testing"))]
use std::time::SystemTime;
#[cfg(feature = "unsafe_use_mock_instant_for_testing")]
use mock_instant::thread_local::SystemTime;
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.expect("after UNIX epoch")