Testing timeouts is easy when you have all the time in the world!

I am pretty sure that, at some point, you have written some code that relies on the time-keeping functions provided by the standard library's time package. Perhaps it's some code that periodically polls a remote endpoint – a great case for using time.NewTicker – or maybe you are using time.After to implement a timeout mechanism inside a go-routine that waits for an event to occur. In a slightly different scenario, using time.NewTimer to provide your server code with ample time to drain all its connections before shutting down would also be a stellar idea.

However, testing code that uses any of these patterns is not a trivial thing. For example, let's say that you are trying ...

Get Hands-On Software Engineering with Golang now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.