使用Golang Playground的功能在测试期间模拟时间。

huangapple go评论84阅读模式
英文:

use golang playground features to fake time during testing

问题

golang博客提到了两个Go特性:faketime和timejump:
https://blog.golang.org/playground

我感兴趣的部分是:

修改调度器条件以等待死锁,然后:
- 检查计时器是否挂起
- 将时钟推进到第一个计时器的触发时间

我想知道如何利用这个实现来使用faketime运行单元测试。我的意思是许多使用time.Sleep的单元测试。实时测试是不可行的,因为执行时间会累积到几个小时。在faketime中,测试在几分之一秒内运行。

明确一点:我不计划也不希望干扰运行时。我想构建一个可用的虚拟时钟。

我相当确信上述提到的实现在并发情况下是正确的。如果你有关于如何借用这个实现并构建一个虚拟时钟的想法或建议,那将非常棒。

我的问题是基于这样一个假设:谷歌不会接受一个针对runtime/time.go的拉取请求,将faketime变成Faketime或添加"func Faketime(f int64) {faketime = f}"。

英文:

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}".

答案1

得分: 2

我的问题是基于这样一个假设:谷歌不会接受将runtime/time.go转换为Faketime或添加"func Faketime(f int64) {faketime = f}"的拉取请求。

四年后(2019年9月),这个假设可能会受到挑战。

Brad Fitzpatrick刚刚宣布:

我们将把#golang playground从Native Client迁移到带有gvisor支持的linux/amd64二进制文件,例如。

并且(推文):

Austin的faketime #golang支持已经在主分支(1.14?)中。请参阅golang/go提交的5ff38e4和其前两个提交。

请注意,睡眠是模拟的,而所有写入都以包含虚假时间的二进制头为前缀,从通常的Go纪元开始(play.golang.org)。

请参阅问题30439和问题30324。

上下文是playground。
这是供playground使用的,可能甚至没有记录或支持(保持兼容)。
这样我们就可以在JavaScript中重放睡眠,但在服务器端快速执行。

图片链接:https://i.stack.imgur.com/Wvmcx.jpg

英文:

> 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.

使用Golang Playground的功能在测试期间模拟时间。

> 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.

huangapple
  • 本文由 发表于 2015年12月30日 02:34:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/34516952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定