diff --git a/test/poll.go b/test/poll.go new file mode 100644 index 000000000..05ba41235 --- /dev/null +++ b/test/poll.go @@ -0,0 +1,26 @@ +package test + +import ( + "reflect" + "testing" + "time" +) + +// Poll repeatedly calls a function until the function returns the correct response or until poll timeout. +func Poll(t testing.TB, d time.Duration, want interface{}, have func() interface{}) { + t.Helper() + deadline := time.Now().Add(d) + for { + if time.Now().After(deadline) { + break + } + if reflect.DeepEqual(want, have()) { + return + } + time.Sleep(d / 100) + } + h := have() + if !reflect.DeepEqual(want, h) { + t.Fatalf("expected %v, got %v", want, h) + } +}