Skip to content

Commit c81e7f2

Browse files
committed
context: deflake example
Got a trybot failure in https://storage.googleapis.com/go-build-log/8aee0b8b/linux-amd64_38d0be3e.log Change-Id: I867a658968a0088c9f3f76ff235816e4179f0bec Reviewed-on: https://go-review.googlesource.com/47095 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
1 parent d4223d6 commit c81e7f2

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

context/withtimeout_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ import (
1111
"golang.org/x/net/context"
1212
)
1313

14+
var workComplete <-chan int
15+
1416
func ExampleWithTimeout() {
1517
// Pass a context with a timeout to tell a blocking function that it
1618
// should abandon its work after the timeout elapses.
17-
ctx, _ := context.WithTimeout(context.Background(), 100*time.Millisecond)
19+
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
1820
select {
19-
case <-time.After(200 * time.Millisecond):
21+
case <-workComplete:
22+
cancel()
2023
fmt.Println("overslept")
2124
case <-ctx.Done():
2225
fmt.Println(ctx.Err()) // prints "context deadline exceeded"

0 commit comments

Comments
 (0)