mardi 20 septembre 2016

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

Aucun commentaire:

Enregistrer un commentaire