Files
weave-scope/test/poll.go
Tom Wilkie 9fc02d941e Fix flaky tests
- Make poll take interfaces, do diff on error
- Use poll in TestRegistryEvents
- Improve the locking to prevent deadlocks and data races in registry_test.go
2015-07-08 13:54:09 +00:00

26 lines
459 B
Go

package test
import (
"reflect"
"testing"
"time"
)
// Poll repeatedly evaluates condition until we either timeout, or it suceeds.
func Poll(t *testing.T, d time.Duration, want interface{}, have func() interface{}) {
deadline := time.Now().Add(d)
for {
if time.Now().After(deadline) {
break
}
if reflect.DeepEqual(want, have()) {
return
}
time.Sleep(d / 10)
}
h := have()
if reflect.DeepEqual(want, h) {
t.Fatal(Diff(want, h))
}
}