I have the following very simple code:
for i := 0; i < 100; i++ {
tt := time.AfterFunc(10*time.Microsecond, func() { log.Fatal("bleh!") })
time.Sleep(2 * time.Microsecond)
tt.Stop()
}
Now, it should seem that it would do nothing, and when I run it on the Go playground, it just quietly exits as expected, but when I run this on my Macbook, I invariably get errors, like:
go run example.go
2017/03/02 22:21:50 bleh!
2017/03/02 22:21:50 bleh!
exit status 1
And I can put that time on the AfterFunc
quite high indeed, even as high as 50 or 100 μs and I'll still get the occasional "bleh!" and death. What are the guarantees around time.Sleep
in Go? And what is a better way of testing that something ran within a specific amount of time?