Consistently polls the fetch function for a duration and fails if the value ever changes from the expected value. Use sparingly to avoid flakiness, as this is inherently timing-dependent. This is useful for asserting that no events occur (e.g., that a value stays at 0). Default duration is 20ms with
(t test.Failer, fetch func() T, expected T, duration ...time.Duration)
| 117 | // events occur (e.g., that a value stays at 0). Default duration is 20ms with |
| 118 | // 2ms polling interval. |
| 119 | func Consistently[T any](t test.Failer, fetch func() T, expected T, duration ...time.Duration) { |
| 120 | t.Helper() |
| 121 | d := time.Millisecond * 20 |
| 122 | if len(duration) > 0 { |
| 123 | d = duration[0] |
| 124 | } |
| 125 | interval := time.Millisecond * 2 |
| 126 | deadline := time.Now().Add(d) |
| 127 | |
| 128 | for time.Now().Before(deadline) { |
| 129 | a := fetch() |
| 130 | if !cmp.Equal(a, expected, opts(expected)...) { |
| 131 | t.Fatalf("value changed unexpectedly: %v\nGot: %v\nWant: %v", cmp.Diff(a, expected, opts(expected)...), a, expected) |
| 132 | } |
| 133 | time.Sleep(interval) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // Error asserts the provided err is non-nil |
| 138 | func Error(t test.Failer, err error) { |
searching dependent graphs…