Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang testing programs that involves time

Tags:

testing

go

timer

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()
    }
}
like image 853
Passer By Avatar asked Sep 20 '16 09:09

Passer By


1 Answers

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()
    }
}
like image 56
Ainar-G Avatar answered Nov 15 '22 10:11

Ainar-G