File
Blob: util/testcond/wait.go
| 1 | package testcond |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | // taken from https://yangwwei.github.io/2020/05/12/flaky-unit-tests-on-github.html |
| 9 | func WaitForCondition(eval func() bool, interval time.Duration, timeout time.Duration) error { |
| 10 | deadline := time.Now().Add(timeout) |
| 11 | for { |
| 12 | if eval() { |
| 13 | return nil |
| 14 | } |
| 15 | |
| 16 | if time.Now().After(deadline) { |
| 17 | return fmt.Errorf("timeout waiting for condition") |
| 18 | } |
| 19 | |
| 20 | time.Sleep(interval) |
| 21 | } |
| 22 | } |