| 25 | ) |
| 26 | |
| 27 | func WithTimeout(t *testing.T, f func() string, timeouts ...time.Duration) { |
| 28 | timeout := ConnectTimeout |
| 29 | if len(timeouts) > 0 { |
| 30 | timeout = timeouts[0] |
| 31 | } |
| 32 | ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| 33 | defer cancel() |
| 34 | lastErr := "" |
| 35 | for { |
| 36 | select { |
| 37 | case <-ctx.Done(): |
| 38 | if lastErr != "" { |
| 39 | t.Fatalf("did not reach expected state after %v: %s", timeout, lastErr) |
| 40 | } |
| 41 | case <-time.After(10 * time.Millisecond): |
| 42 | lastErr = f() |
| 43 | if lastErr == "" { |
| 44 | return |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |