feat(infra/clock): injectable Clock with Real and Fake implementations

This commit is contained in:
2026-05-05 14:16:32 +08:00
parent 967f390b1e
commit c47d99d3d8
3 changed files with 110 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
package clock
import (
"sync"
"time"
)
// FakeClock is a controllable Clock for tests. Construct with NewFake; advance
// time with Advance; jump to a specific time with Set. Safe for concurrent use.
type FakeClock struct {
mu sync.Mutex
now time.Time
}
// NewFake returns a FakeClock initialized to t.
func NewFake(t time.Time) *FakeClock {
return &FakeClock{now: t}
}
func (c *FakeClock) Now() time.Time {
c.mu.Lock()
defer c.mu.Unlock()
return c.now
}
// Advance moves the clock forward by d.
func (c *FakeClock) Advance(d time.Duration) {
c.mu.Lock()
defer c.mu.Unlock()
c.now = c.now.Add(d)
}
// Set jumps the clock to t.
func (c *FakeClock) Set(t time.Time) {
c.mu.Lock()
defer c.mu.Unlock()
c.now = t
}