Poll repeatedly calls a function until the function returns the correct response or until poll timeout.
(t testing.TB, d time.Duration, want interface{}, have func() interface{})
| 8 | |
| 9 | // Poll repeatedly calls a function until the function returns the correct response or until poll timeout. |
| 10 | func Poll(t testing.TB, d time.Duration, want interface{}, have func() interface{}) { |
| 11 | t.Helper() |
| 12 | deadline := time.Now().Add(d) |
| 13 | for !time.Now().After(deadline) { |
| 14 | |
| 15 | if reflect.DeepEqual(want, have()) { |
| 16 | return |
| 17 | } |
| 18 | time.Sleep(d / 100) |
| 19 | } |
| 20 | h := have() |
| 21 | if !reflect.DeepEqual(want, h) { |
| 22 | t.Fatalf("expected %v, got %v", want, h) |
| 23 | } |
| 24 | } |