mirror of
https://github.com/golang/go
synced 2024-11-23 16:30:06 -07:00
context: make DeadlineExceeded have a Timeout method
Fixes #14238 Change-Id: I1538bfb5cfa63e36a89df1f6eb9f5a0dcafb6ce5 Reviewed-on: https://go-review.googlesource.com/23256 Reviewed-by: Dave Cheney <dave@cheney.net> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
0b80659832
commit
dc4427f372
@ -144,7 +144,13 @@ var Canceled = errors.New("context canceled")
|
||||
|
||||
// DeadlineExceeded is the error returned by Context.Err when the context's
|
||||
// deadline passes.
|
||||
var DeadlineExceeded = errors.New("context deadline exceeded")
|
||||
var DeadlineExceeded error = deadlineExceededError{}
|
||||
|
||||
type deadlineExceededError struct{}
|
||||
|
||||
func (deadlineExceededError) Error() string { return "context deadline exceeded" }
|
||||
|
||||
func (deadlineExceededError) Timeout() bool { return true }
|
||||
|
||||
// An emptyCtx is never canceled, has no values, and has no deadline. It is not
|
||||
// struct{}, since vars of this type must have distinct addresses.
|
||||
|
@ -594,3 +594,15 @@ func recoveredValue(fn func()) (v interface{}) {
|
||||
fn()
|
||||
return
|
||||
}
|
||||
|
||||
func TestDeadlineExceededSupportsTimeout(t *testing.T) {
|
||||
i, ok := DeadlineExceeded.(interface {
|
||||
Timeout() bool
|
||||
})
|
||||
if !ok {
|
||||
t.Fatal("DeadlineExceeded does not support Timeout interface")
|
||||
}
|
||||
if !i.Timeout() {
|
||||
t.Fatal("wrong value for timeout")
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user