Re: [go-nuts] Changing wall clock in unit test

193 views
Skip to first unread message

David Finkel

unread,
Jul 30, 2021, 3:32:14 PM7/30/21
to Carl, golang-nuts


On Fri, Jul 30, 2021 at 12:13 AM Carl <carl...@gmail.com> wrote:
Hi,

Is there a way to write a unit test that simulates a change in the wall clock for a time.Time value?

I use a combination of two approaches to simulate the passage of time (often both work together):
Full disclosure: I wrote github.com/vimeo/go-clocks, so I might be a bit biased -- it is based on one of Google's internal packages for C++, though.

One additional benefit of using a fake clock is that it allows for fine-grained synchronization between tests and code if you're sleeping in goroutines, as the fake clock in go-clocks has methods for awaiting a certain number of sleepers, etc.
 

Cheers,
Carl

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/f306417d-46f9-4ccd-8e7c-69a1dbd14969n%40googlegroups.com.

Kurtis Rader

unread,
Jul 30, 2021, 10:12:58 PM7/30/21
to Carl, golang-nuts
On Thu, Jul 29, 2021 at 9:13 PM Carl <carl...@gmail.com> wrote:
Is there a way to write a unit test that simulates a change in the wall clock for a time.Time value?

Here is an example of how the Elvish shell stubs the time.After() function to verify its builtin "sleep" command behaves correctly without having to sleep for arbitrary intervals. It doesn't exactly do what you asked but illustrates one approach for doing such things.




--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank
Reply all
Reply to author
Forward
0 new messages