WaitOrTimeout tries to wait on the given channel for a value to come through, and returns it if one does, but times out after a reasonable amount of time. Useful to guarantee that test cases don't hang forever, even in the event of something wrong.
(tb testing.TB, waitChan <-chan T)
| 275 | // Useful to guarantee that test cases don't hang forever, even in the event of |
| 276 | // something wrong. |
| 277 | func WaitOrTimeout[T any](tb testing.TB, waitChan <-chan T) T { |
| 278 | tb.Helper() |
| 279 | |
| 280 | timeout := WaitTimeout() |
| 281 | |
| 282 | select { |
| 283 | case value := <-waitChan: |
| 284 | return value |
| 285 | case <-time.After(timeout): |
| 286 | require.FailNowf(tb, "WaitOrTimeout timed out", |
| 287 | "WaitOrTimeout timed out after waiting %s", timeout) |
| 288 | } |
| 289 | return *new(T) // unreachable |
| 290 | } |
| 291 | |
| 292 | // WaitOrTimeoutN tries to wait on the given channel for N values to come |
| 293 | // through, and returns it if they do, but times out after a reasonable amount |
searching dependent graphs…