Skip to content

Commit

Permalink
Add some tests (#591)
Browse files Browse the repository at this point in the history
* Extend tick-tock-mock coverage

* remove absolute method

* update crates

* use codecov format export

* Revert "use codecov format export"

This reverts commit 7876b83.

* uppies?
  • Loading branch information
aumetra authored Sep 26, 2024
1 parent 4d9bf10 commit db4dbf0
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 116 deletions.
183 changes: 90 additions & 93 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion crates/kitsune-activitypub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ version.workspace = true
license.workspace = true

[dependencies]
async-trait = "0.1.82"
async-trait = "0.1.83"
base64-simd = "0.8.0"
diesel = { workspace = true }
diesel-async = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/kitsune-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ version.workspace = true
license.workspace = true

[dependencies]
async-trait = "0.1.82"
async-trait = "0.1.83"
const_format = "0.2.33"
git-version = "0.3.9"
kitsune-db = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions crates/kitsune-http-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ http-body = "1.0.1"
http-body-util = "0.1.2"
http-signatures = { workspace = true }
hyper = "1.4.1"
hyper-util = { version = "0.1.8", features = [
hyper-util = { version = "0.1.9", features = [
"client-legacy",
"http1",
"http2",
Expand All @@ -35,7 +35,7 @@ serde = "1.0.210"
simdutf8 = { workspace = true }
sonic-rs = { workspace = true }
tower = { version = "0.5.1", features = ["util"] }
tower-http = { version = "0.6.0", features = [
tower-http = { version = "0.6.1", features = [
# Explicitly exclude `zstd`
# It's not widely adopted and takes a long time to build
"decompression-br",
Expand Down
2 changes: 1 addition & 1 deletion crates/kitsune-observability/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ version.workspace = true
license.workspace = true

[dependencies]
async-trait = "0.1.82"
async-trait = "0.1.83"
eyre = "0.6.12"
http-body-util = "0.1.2"
hyper = { version = "1.4.1", default-features = false }
Expand Down
1 change: 0 additions & 1 deletion crates/kitsune-oidc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ license.workspace = true
[dependencies]
enum_dispatch = "0.3.13"
fred = { workspace = true }
http = "1.1.0"
http-body-util = "0.1.2"
kitsune-config = { workspace = true }
kitsune-derive = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/kitsune-search/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ version.workspace = true
license.workspace = true

[dependencies]
async-trait = "0.1.82"
async-trait = "0.1.83"
bytes = "1.7.2"
diesel = { workspace = true }
diesel-async = { workspace = true }
Expand Down
6 changes: 3 additions & 3 deletions crates/kitsune-wasm-mrf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license.workspace = true
build = "build.rs"

[dependencies]
async-trait = "0.1.82"
async-trait = "0.1.83"
blake3 = "1.5.4"
color-eyre = "0.6.3"
derive_more = { version = "1.0.0", features = ["from"] }
Expand All @@ -29,7 +29,7 @@ tokio = { workspace = true, features = ["fs"] }
tracing = "0.1.40"
triomphe = { workspace = true }
walkdir = "2.5.0"
wasmtime = { version = "25.0.0", default-features = false, features = [
wasmtime = { version = "25.0.1", default-features = false, features = [
"addr2line",
"async",
"component-model",
Expand All @@ -38,7 +38,7 @@ wasmtime = { version = "25.0.0", default-features = false, features = [
"pooling-allocator",
"runtime",
] }
wasmtime-wasi = { version = "25.0.0", default-features = false }
wasmtime-wasi = { version = "25.0.1", default-features = false }

[dev-dependencies]
tempfile = "3.12.0"
Expand Down
2 changes: 1 addition & 1 deletion crates/kitsune-webfinger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ version.workspace = true
license.workspace = true

[dependencies]
async-trait = "0.1.82"
async-trait = "0.1.83"
fred = { workspace = true }
futures-util = "0.3.30"
http = "1.1.0"
Expand Down
9 changes: 4 additions & 5 deletions kitsune/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ athena = { workspace = true }
argon2 = { version = "0.5.3", features = ["std"] }
askama = { workspace = true }
askama_axum = "0.4.0"
async-trait = "0.1.82"
async-trait = "0.1.83"
axum = { version = "0.7.6", features = ["macros", "multipart"] }
axum-extra = { version = "0.9.4", features = [
"cookie",
Expand Down Expand Up @@ -88,7 +88,7 @@ tokio-util = { version = "0.7.12", features = ["io"] }
tower = { version = "0.5.1", features = ["util"] }
tower-stop-using-brave = { workspace = true }
tower-x-clacks-overhead = { workspace = true }
tower-http = { version = "0.6.0", features = [
tower-http = { version = "0.6.1", features = [
"catch-panic",
"cors",
"fs",
Expand All @@ -106,14 +106,14 @@ url = "2.5.2"
# --- Optional dependencies ---

# "graphql" feature
async-graphql = { version = "7.0.9", default-features = false, features = [
async-graphql = { version = "7.0.10", default-features = false, features = [
"graphiql",
"tempfile",
"time",
"tracing",
"uuid",
], optional = true }
async-graphql-axum = { version = "7.0.9", optional = true }
async-graphql-axum = { version = "7.0.10", optional = true }

# "mastodon-api" feature
kitsune-mastodon = { workspace = true, optional = true }
Expand All @@ -129,7 +129,6 @@ kitsune-scss-compiler = { workspace = true }
[dev-dependencies]
kitsune-http-client = { workspace = true }
kitsune-test = { workspace = true }
pretty_assertions = "1.4.1"
fred = { workspace = true }

[features]
Expand Down
2 changes: 1 addition & 1 deletion lib/athena/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ required-features = ["redis"]

[dependencies]
ahash = "0.8.11"
async-trait = "0.1.82"
async-trait = "0.1.83"
either = { version = "1.13.0", default-features = false, optional = true }
futures-util = { version = "0.3.30", default-features = false }
iso8601-timestamp = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion lib/cursiv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ triomphe = { workspace = true }
zeroize = { version = "1.8.1", features = ["derive"] }

# `axum` feature
async-trait = { version = "0.1.82", optional = true }
async-trait = { version = "0.1.83", optional = true }
axum-core = { version = "0.4.4", optional = true }

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion lib/geomjeungja/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ version.workspace = true
license = "MIT OR Apache-2.0"

[dependencies]
async-trait = "0.1.82"
async-trait = "0.1.83"
hickory-resolver = "0.24.1"
rand = "0.8.5"
serde = { version = "1.0.210", features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion lib/speedy-uuid/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ version.workspace = true
license = "MIT OR Apache-2.0"

[dependencies]
async-graphql = { version = "7.0.9", default-features = false, optional = true }
async-graphql = { version = "7.0.10", default-features = false, optional = true }
diesel = { version = "2.2.4", default-features = false, features = [
"postgres_backend",
"uuid",
Expand Down
34 changes: 31 additions & 3 deletions lib/tick-tock-mock/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub struct MockHandle {

impl MockHandle {
/// Adjust the delta by the duration in the direction specified
#[inline]
pub fn adjust(&self, direction: DeltaDirection, delta: Duration) {
let Some(delta_handle) = self.delta.upgrade() else {
return;
Expand All @@ -48,10 +49,11 @@ impl MockHandle {
delta_handle.fetch_add(delta, Ordering::AcqRel);
}

/// Set the delta to the absolute value
pub fn set_delta(&self, delta: i64) {
/// Reset the offset to 0
#[inline]
pub fn reset(&self) {
if let Some(delta_handle) = self.delta.upgrade() {
delta_handle.store(delta, Ordering::Release);
delta_handle.store(0, Ordering::Release);
}
}
}
Expand All @@ -75,6 +77,7 @@ pub struct Clock {

impl Clock {
/// Construct a new clock without an internal delta
#[inline]
#[must_use]
pub fn new() -> Self {
Self::default()
Expand All @@ -83,6 +86,7 @@ impl Clock {
/// Construct a mockable clock
///
/// This clock returns a handle which you can use to adjust the delta
#[inline]
#[must_use]
pub fn mockable() -> (Self, MockHandle) {
let delta = Arc::new(AtomicI64::default());
Expand All @@ -98,6 +102,7 @@ impl Clock {
/// Enter a context where this clock is installed into the thread-local context
///
/// As long as the guard is kept live, the [`now`] function will read the time of this clock
#[inline]
#[must_use]
pub fn enter(&self) -> ClockGuard {
let old_clock = THREAD_CLOCK.with(|clock| clock.replace(self.clone()));
Expand All @@ -108,6 +113,7 @@ impl Clock {
}

/// Read the current time from the system clock and apply the delta
#[inline]
#[must_use]
pub fn now(&self) -> SystemTime {
let mut now = SystemTime::now();
Expand All @@ -126,6 +132,7 @@ impl Clock {
}

/// Read the current time from the thread-local clock
#[inline]
#[must_use]
pub fn now() -> SystemTime {
THREAD_CLOCK.with(|clock| clock.borrow().now())
Expand Down Expand Up @@ -161,4 +168,25 @@ mod test {
let delta = now.duration_since(after).unwrap();
assert_eq!(delta.as_secs_f32().round() as u8, 1);
}

#[test]
fn can_reset() {
let (clock, mock) = Clock::mockable();
let _clock_guard = clock.enter();

let now = crate::now();
mock.adjust(DeltaDirection::Add, Duration::from_secs(10));
let adjusted = crate::now();
mock.reset();
let reset = crate::now();

// "adjusted" should be ahead of "now" and "reset"
assert!(now < adjusted && reset < adjusted);

// "now" and "reset" should be pretty much equal
//
// Technically this very much depends on the system clock and execution speed,
// but it should pretty much always hold true
assert_eq!(reset.duration_since(now).unwrap().as_secs(), 0);
}
}

0 comments on commit db4dbf0

Please sign in to comment.