TimeoutTest returns a test function that executes f with a timeout, If the test does not complete in time the test is failed. This lets us set a per-test timeout instead of the global `go test -timeout` coarse timeout.
(timeout time.Duration, f func(t *testing.T))
| 41 | // test does not complete in time the test is failed. This lets us set a |
| 42 | // per-test timeout instead of the global `go test -timeout` coarse timeout. |
| 43 | func TimeoutTest(timeout time.Duration, f func(t *testing.T)) func(t *testing.T) { |
| 44 | // Raise the timeout if we're run under the race detector. |
| 45 | timeout *= RaceDetectorMultiplier |
| 46 | // If we're in a CI environment, raise the timeout by 10x. This mimics the |
| 47 | // timeout global flag set in the Makefile. |
| 48 | if os.Getenv("CI") == "true" { |
| 49 | timeout *= 10 |
| 50 | } |
| 51 | return func(t *testing.T) { |
| 52 | t.Helper() |
| 53 | done := make(chan bool) |
| 54 | go func() { |
| 55 | t.Helper() |
| 56 | defer close(done) |
| 57 | f(t) |
| 58 | }() |
| 59 | select { |
| 60 | case <-time.After(timeout): |
| 61 | buf := make([]byte, 1<<20) |
| 62 | stacklen := runtime.Stack(buf, true) |
| 63 | t.Fatalf("timed out after %s\n%s", timeout, buf[:stacklen]) |
| 64 | case <-done: |
| 65 | } |
| 66 | } |
| 67 | } |
no outgoing calls