有没有一种简单的方法可以在测试期间在golang中全局地存储time.Now()? [英] Is there an easy way to stub out time.Now() globally in golang during test?

查看:261
本文介绍了有没有一种简单的方法可以在测试期间在golang中全局地存储time.Now()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的部分代码是时间敏感的,我们需要能够保留某些内容,然后在30-60秒内释放它,我们可以执行 time.Sleep(60 * time.Second )



我刚刚实现了时间接口,并且在测试过程中使用了时间接口的stubbed实现,类似于这个golang-nuts讨论



然而,在多个站点中调用 time.Now(),这意味着我们需要传递一个变量来跟踪我们实际上睡了多少时间。



我想知道是否有另一种方法可以全局地存取 time.Now()。也许是系统调用改变系统时钟?

也许我们可以编写我们自己的时间包,它基本上封装了时间包,但允许我们改变它?



我们目前的实施效果很好,我是一个初学者,我很好奇看看有没有人有其他想法?

解决方案

通过实施自定义界面,已经在正确的路上。我认为你使用了你发布的golang nuts线程的以下建议:






 类型Clock接口{
Now()time.Time
After(d time.Duration)< -chan time.Time
}




并提供具体实现




 类型realClock struct {} 
func(realClock)Now()time.Time {return time.Now()}
func(realClock)After d time.Duration)< -chan time.Time {return time.After(d)}




和一个测试实现。







原创



更改系统在进行测试时(或者一般情况下)是一个糟糕的主意
在执行测试时,您不知道什么取决于系统时间,并且您不想通过花费几天的调试来找出困难的方式。不要这样做。



全球时间套餐也没有办法做到这一点,并且做不到
您无法使用接口解决方案做任何事情。你可以编写你自己的时间包
,它使用标准库,并提供一个函数切换到一个模拟时间库以进行
测试,如果它是你需要传递的时间对象,打扰你。

设计和测试你的代码的最好方法可能是让尽可能多的代码尽可能无状态。
将您的功能分解为可测试的无状态部分。分开测试这些组件要容易得多。此外,较少的副作用意味着使代码同时运行更容易。


Part of our code is time sensitive and we need to able to reserve something and then release it in 30-60 seconds etc, which we can just do a time.Sleep(60 * time.Second)

I have just implemented time interface and during test use a stubbed implementation of the time interface, similar to this golang-nuts discussion.

However, time.Now() is called in multiple sites which means we need to pass a variable around to keep track of how much time we have actually slept.

I was wondering if there is an alternative way to stub out time.Now() globally. Maybe making a system call to change the system clock?

Maybe we can write our own time package which basically wraps around the time package but allows us to change it?

Our current implementation works well, I am a go beginner and I am curious to see if anyone has other ideas?

解决方案

With implementing a custom interface you are already on the right way. I take it you use the following advise from the golang nuts thread you've posted:


type Clock interface {
  Now() time.Time
  After(d time.Duration) <-chan time.Time
}

and provide a concrete implementation

type realClock struct{}
func (realClock) Now() time.Time { return time.Now() }
func (realClock) After(d time.Duration) <-chan time.Time { return time.After(d) }

and a testing implementation.


Original

Changing the system time while making tests (or in general) is a bad idea. You don't know what depends on the system time while executing tests and you don't want to find out the hard way by spending days of debugging into that. Just don't do it.

There is also no way to shadow the time package globally and doing that would not do anything more you couldn't do with the interface solution. You can write your own time package which uses the standard library and provides a function to switch to a mock time library for testing if it is the time object you need to pass around with the interface solution that is bothering you.

The best way to design and test your code would probably be to make as much code stateless as possible. Split your functionality in testable, stateless parts. Testing these components separately is much easier then. Also, less side effects means that it is much easier to make the code run concurrently.

这篇关于有没有一种简单的方法可以在测试期间在golang中全局地存储time.Now()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆