MCPcopy
hub / github.com/unkeyed/unkey / Clock

Interface Clock

pkg/clock/interface.go:12–25  ·  view source on GitHub ↗

Clock is an interface for getting the current time and creating tickers. By abstracting time operations behind this interface, code can be written that works with both the system clock in production and controlled time in tests, enabling deterministic testing of time-dependent logic. This approach

Source from the content-addressed store, hash-verified

10// This approach helps avoid flaky tests caused by timing dependencies and
11// allows for simulating time-based scenarios without waiting for real time to pass.
12type Clock interface {
13 // Now returns the current time.
14 // In production implementations, this returns the system time.
15 // In test implementations, this returns a controlled time that
16 // can be manipulated for testing purposes.
17 Now() time.Time
18
19 // NewTicker returns a Ticker that fires every d. Production
20 // implementations delegate to time.NewTicker. Test implementations
21 // fire when simulated time advances past the ticker period, so a
22 // background goroutine driven by NewTicker observes the same notion
23 // of time as the rest of the test instead of running on real time.
24 NewTicker(d time.Duration) Ticker
25}
26
27// Ticker delivers ticks on a channel at a regular cadence. It mirrors
28// the surface of [time.Ticker] needed by callers that work against the

Callers 1

RunAPIMethod · 0.92

Implementers 4

CachedClockpkg/clock/cached_clock.go
RealClockpkg/clock/real_clock.go
TestClockpkg/clock/test_clock.go
MonotonicClockpkg/clock/monotonic_clock.go

Calls

no outgoing calls

Tested by

no test coverage detected