diff options
| author | Bryan C. Mills <bcmills@google.com> | 2020-03-30 14:04:08 -0400 |
|---|---|---|
| committer | Bryan C. Mills <bcmills@google.com> | 2020-03-31 17:59:03 +0000 |
| commit | 5d2ddcd3f51c1ff7aa0a84604b1d8610a17a7933 (patch) | |
| tree | ee5d94a3252af4b9503728f979a6d21dc9759627 /src/context/example_test.go | |
| parent | 5db079d2e5f97952be288c28a3a0690a523efdce (diff) | |
| download | go-5d2ddcd3f51c1ff7aa0a84604b1d8610a17a7933.tar.xz | |
context: fix a flaky timeout in TestLayersTimeout
In CL 223019, I reduced the short timeout in the testLayers helper to
be even shorter than it was. That exposed a racy (time-dependent)
select later in the function, which failed in one of the slower
builders (android-386-emu).
Also streamline the test to make it easier to test with a very high -count flag:
- Run tests that sleep for shortDuration in parallel to reduce latency.
- Use shorter durations in examples to reduce test running time.
- Avoid mutating global state (in package math/rand) in testLayers.
After this change (but not before it),
'go test -run=TestLayersTimeout -count=100000 context' passes on my workstation.
Fixes #38161
Change-Id: Iaf4abe7ac308b2100d8828267cda9f4f8ae4be82
Reviewed-on: https://go-review.googlesource.com/c/go/+/226457
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/context/example_test.go')
| -rw-r--r-- | src/context/example_test.go | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/src/context/example_test.go b/src/context/example_test.go index b91a8acef3..72ac5d2e49 100644 --- a/src/context/example_test.go +++ b/src/context/example_test.go @@ -10,6 +10,8 @@ import ( "time" ) +const shortDuration = 1 * time.Millisecond // a reasonable duration to block in an example + // This example demonstrates the use of a cancelable context to prevent a // goroutine leak. By the end of the example function, the goroutine started // by gen will return without leaking. @@ -55,7 +57,7 @@ func ExampleWithCancel() { // This example passes a context with an arbitrary deadline to tell a blocking // function that it should abandon its work as soon as it gets to it. func ExampleWithDeadline() { - d := time.Now().Add(50 * time.Millisecond) + d := time.Now().Add(shortDuration) ctx, cancel := context.WithDeadline(context.Background(), d) // Even though ctx will be expired, it is good practice to call its @@ -79,7 +81,7 @@ func ExampleWithDeadline() { func ExampleWithTimeout() { // Pass a context with a timeout to tell a blocking function that it // should abandon its work after the timeout elapses. - ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) + ctx, cancel := context.WithTimeout(context.Background(), shortDuration) defer cancel() select { |
