MCPcopy
hub / github.com/istio/istio / Consistently

Function Consistently

pkg/test/util/assert/assert.go:119–135  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

117// events occur (e.g., that a value stays at 0). Default duration is 20ms with
118// 2ms polling interval.
119func 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
138func Error(t test.Failer, err error) {

Callers 3

testJoinRegisterBatchFunction · 0.92

Calls 8

fetchFunction · 0.85
optsFunction · 0.85
NowMethod · 0.80
HelperMethod · 0.65
AddMethod · 0.65
EqualMethod · 0.65
FatalfMethod · 0.65
DiffMethod · 0.45

Tested by 3

testJoinRegisterBatchFunction · 0.74

Used in the wild real call sites across dependent graphs

searching dependent graphs…