mirror of
https://github.com/Blah-IM/blahrs.git
synced 2025-06-30 19:45:34 +00:00
feat(types,blahd): allow using mock clock for testing
This commit is contained in:
parent
1a4980ebba
commit
31dc3e33c6
8 changed files with 61 additions and 13 deletions
|
@ -1,7 +1,7 @@
|
|||
//! Room feed generation.
|
||||
use std::fmt;
|
||||
use std::num::NonZero;
|
||||
use std::time::{Duration, SystemTime};
|
||||
use std::time::Duration;
|
||||
|
||||
use axum::http::header;
|
||||
use axum::response::{IntoResponse, Response};
|
||||
|
@ -43,7 +43,8 @@ pub trait FeedType {
|
|||
}
|
||||
|
||||
fn timestamp_to_rfc3339(timestamp: u64) -> impl fmt::Display {
|
||||
humantime::format_rfc3339(SystemTime::UNIX_EPOCH + Duration::from_secs(timestamp))
|
||||
// This only for formatting, thus always use the non-mock `SystemTime`.
|
||||
humantime::format_rfc3339(std::time::SystemTime::UNIX_EPOCH + Duration::from_secs(timestamp))
|
||||
}
|
||||
|
||||
/// See:
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
use std::cell::Cell;
|
||||
|
||||
/// Id generation.
|
||||
/// Ref: https://en.wikipedia.org/wiki/Snowflake_ID
|
||||
/// FIXME: Currently we assume no more than one request in a single millisecond.
|
||||
use std::time::SystemTime;
|
||||
|
||||
/// FIXME: Handle multi-threaded runtime.
|
||||
use blah_types::Id;
|
||||
|
||||
use crate::utils::SystemTime;
|
||||
|
||||
pub fn timestamp_of_id(id: Id) -> u64 {
|
||||
(id.0 as u64 >> 16) / 1000
|
||||
}
|
||||
|
@ -16,17 +18,32 @@ pub trait IdExt {
|
|||
fn is_peer_chat(&self) -> bool;
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static LAST_ID: Cell<i64> = const { Cell::new(0) };
|
||||
}
|
||||
|
||||
impl IdExt for Id {
|
||||
fn gen() -> Self {
|
||||
let timestamp = SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.expect("after UNIX epoch");
|
||||
let timestamp_ms = timestamp.as_millis();
|
||||
assert!(
|
||||
0 < timestamp_ms && timestamp_ms < (1 << 48),
|
||||
"invalid timestamp",
|
||||
);
|
||||
Id((timestamp_ms as i64) << 16)
|
||||
assert!(timestamp_ms < (1 << 48), "invalid timestamp");
|
||||
let timestamp_ms = timestamp_ms as i64;
|
||||
let id = timestamp_ms << 16;
|
||||
LAST_ID.with(|last_id| {
|
||||
let prev = last_id.get();
|
||||
if prev >> 16 != timestamp_ms {
|
||||
// If not in the same millisecond, use the new timestamp as id.
|
||||
last_id.set(id);
|
||||
Id(id)
|
||||
} else {
|
||||
// Otherwise, try to increse the trailing counter.
|
||||
assert!(prev < (1 << 16), "id counter overflow");
|
||||
last_id.set(prev + 1);
|
||||
Id(prev + 1)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn gen_peer_chat_rid() -> Self {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use std::num::NonZero;
|
||||
use std::time::{Duration, Instant};
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{anyhow, ensure};
|
||||
use axum::http::{HeaderMap, HeaderName, StatusCode};
|
||||
|
@ -15,6 +15,7 @@ use serde::Deserialize;
|
|||
use sha2::{Digest, Sha256};
|
||||
|
||||
use crate::database::TransactionOps;
|
||||
use crate::utils::Instant;
|
||||
use crate::{ApiError, AppState, SERVER_AND_VERSION};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
|
||||
|
|
|
@ -1,6 +1,12 @@
|
|||
use std::collections::{HashSet, VecDeque};
|
||||
use std::hash::Hash;
|
||||
use std::time::{Duration, Instant};
|
||||
use std::time::Duration;
|
||||
|
||||
#[cfg(not(feature = "unsafe_use_mock_instant_for_testing"))]
|
||||
pub use std::time::{Instant, SystemTime};
|
||||
|
||||
#[cfg(feature = "unsafe_use_mock_instant_for_testing")]
|
||||
pub use mock_instant::thread_local::{Instant, SystemTime};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ExpiringSet<T> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue