Passer By
Passer By

Reputation: 21131

Golang testing programs that involves time

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()
    }
}

Upvotes: 11

Views: 7650

Answers (1)

Ainar-G
Ainar-G

Reputation: 36199

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()
    }
}

Upvotes: 13

Related Questions