Re:Mocking time and testing event loops in Go
by rowanseymour on Monday May 25, 2020 @09:30AM
(#23300850)
I initially played with passing an instance of a mockable time thing around everywhere that needed time, but it felt over the top to be passing around so many things, solely for purposes of testing. So now it's just a thing you mock at a global level, e.g.
[ Parent
| on HN ]
Re:Mocking time and testing event loops in Go
by physicles on Monday May 25, 2020 @09:46AM
(#23301037)
I've done this too, because passing a mockable time everywhere would just clutter the code, and this is an instance where a package-level global isn't going to bite you in the ass -- in prod anyway (in tests you might get your wires crossed, but seen another way, a global ensures all parts of your program think it's the same time).
Difference in mine is that dates.SetNowSource returns a reset function that you can defer, like with context.WithCancel().