A brittle test depends on incidental timing rather than controlled behavior. A cache test that creates events with time.Now(), trims after 15 milliseconds, and expects two events can fail when the test machine becomes busy. section 11.6
Time-dependent cache test
Replace moving timestamps with fixed values and inject the clock: configure Cache.now to return 12:00:00.06, then trim by 15 milliseconds. The test can assert the result without sleeps or races against time.Now(). section 11.6
Reduce brittleness by changing where time enters the design:
- Inject a clock dependency, using time.Now in production and a fixed function in tests. section 11.6
- Pass the current time into TrimOlderThan when callers can provide it explicitly. 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 {
// ...
}
Shows fixed event timestamps and an injected clock producing deterministic cache behavior. section 11.6


