From dc4427f3727804ded270bc6a7a8066ccb3c151d0 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Thu, 19 May 2016 18:08:43 +0000 Subject: [PATCH] context: make DeadlineExceeded have a Timeout method Fixes #14238 Change-Id: I1538bfb5cfa63e36a89df1f6eb9f5a0dcafb6ce5 Reviewed-on: https://go-review.googlesource.com/23256 Reviewed-by: Dave Cheney Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- src/context/context.go | 8 +++++++- src/context/context_test.go | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/context/context.go b/src/context/context.go index 9ff19503b2..169db74f57 100644 --- a/src/context/context.go +++ b/src/context/context.go @@ -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. diff --git a/src/context/context_test.go b/src/context/context_test.go index 99456b188d..90e78e57ec 100644 --- a/src/context/context_test.go +++ b/src/context/context_test.go @@ -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") + } +} -- 2.48.1