Technical notes on web development, DevOps, and AI integration.
1 article
Swap the clock in Go tests: a rate limiter proven without sleep, plus when to graduate to testing/synctest.
TL;DR: Rate limiter tests shouldn't chase real time with time.Sleep, which makes them slow and flaky. Instead, expose a Now func() time.Time field on the struct and swap in a fixed clock, letting tests advance time instantly with no sleeps. For code that sleeps internally, Go's stable testing/synctest bubble fakes the clock.