There is an object that relies on timing to function correctly. Unfortunately the timing duration itself is too long to realistically test it in real time and shortening the duration defeats the purpose of the testing due to the nature of the object.
What is the best way to test such an object? Ideally, there would be some virtual clock that runs arbitrarily fast that can be used.
type Obj struct{}
func (o Obj) TimeCriticalFunc(d time.Duration) bool {
//do stuff
//possibly calling multiple times time.Now() or other real time related functions
}
func TestTimeCriticalFunc(t *testing.T) {
if !Obj{}.TimeCriticalFunc(10 * 24 * time.Hour) {
t.Fail()
}
}
This was actually answered in Andrew Gerrand's Testing Techniques talk. In your code do
var (
timeNow = time.Now
timeAfter = time.After
)
// ...
type Obj struct{}
func (o Obj) TimeCriticalFunc(d time.Duration) bool {
// Call timeAfter and timeNow.
}
And in your tests do
func TestTimeCriticalFunc(t *testing.T) {
timeNow = func() time.Time {
return myTime // Some time that you need
}
// "Redefine" timeAfter etc.
if !Obj{}.TimeCriticalFunc(10 * 24 * time.Hour) {
t.Fail()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With