在测试期间使用golang游乐场功能伪造时间

The golang blog mentions two Go features faketime and timejump: https://blog.golang.org/playground

The part that interests me is:

modify scheduler condition to wait for deadlock, then:
- check if timers are pending
- advance clock to trigger-time of first timer

I would like to know how I can leverage this implementation to run unittests with faketime. By this I mean many unitests that use time.Sleep. Testing in realtime is prohibitive since execution time adds up to hours. In faketime the tests run within split-seconds.

To be clear: I do not plan or want to mess up the runtime. I want to build a fake clock that works.

I am pretty convinced that the above referenced implementation works correctly in the concurrent case. If you have a idea, a tip or two on how to borrow this implementation and build a fake clock from it would be great.

My question is based on the assumption that google won't accept a pull request for runtime/time.go to turn faketime into Faketime or to add "func Faketime(f int64) {faketime = f}".

My question is based on the assumption that google won't accept a pull request for runtime/time.go to turn faketime into Faketime or to add "func Faketime(f int64) {faketime = f}".

4 years later (Sept. 2019), that assumption might be challenged.

Brad Fitzpatrick just announced:

So we're moving the #golang playground from Native Client to linux/amd64 binaries with runtime faketime support under gvisor. e.g.

And (tweet):

Austin's faketime #golang support is in master (1.14?).
See golang/go commit 5ff38e4 & its prior two commits.

Note that the sleep is simulated and instead all writes are prefixed by a binary header containing the fake time, starting from usual Go epoch (play.golang.org).

See issue 30439 and issue 30324.

https://pbs.twimg.com/media/EDpDthtU8AAXXd2?format=jpg&name=large

The context is the playground.
This is for use by the playground and might not even be documented or supported (kept compatible).
This is so we can replay sleeps in JavaScript but execute quickly server side.