When Cache.TrimOlderThan calls time.Now directly, tests depend on wall-clock timing and can become brittle. Inject a func() time.Time that returns a fixed instant to make trimming tests deterministic. Prefer a Cache dependency over a mutable global clock, which shares state and prevents tests from running in parallel. section 11.6
func TestCache_TrimOlderThan(t *testing.T) {
events := []Event{ #1
{Timestamp: parseTime(t, "2020-01-01T12:00:00.04Z")},
{Timestamp: parseTime(t, "2020-01-01T12:00:00.05Z")},
{Timestamp: parseTime(t, "2020-01-01T12:00:00.06Z")},
}
cache := &Cache{now: func() time.Time { #2
return parseTime(t, "2020-01-01T12:00:00.06Z")
}}
cache.Add(events)
cache.TrimOlderThan(15 * time.Millisecond)
// ...
}
func parseTime(t *testing.T, timestamp string) time.Time {
// ...
}
Fix the clock at a known instant while testing event trimming. section 11.6





