Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fallback Context.Deadline() Context.Done() Context.Err() to Context.Request.Context() #2769

Merged
merged 2 commits into from
Jul 31, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -1160,22 +1160,28 @@ func (c *Context) SetAccepted(formats ...string) {
/***** GOLANG.ORG/X/NET/CONTEXT *****/
/************************************/

// Deadline always returns that there is no deadline (ok==false),
// maybe you want to use Request.Context().Deadline() instead.
// Deadline returns that there is no deadline (ok==false) when c.Request with no Context.
wei840222 marked this conversation as resolved.
Show resolved Hide resolved
func (c *Context) Deadline() (deadline time.Time, ok bool) {
return
if c.Request == nil || c.Request.Context() == nil {
return
}
return c.Request.Context().Deadline()
}

// Done always returns nil (chan which will wait forever),
// if you want to abort your work when the connection was closed
// you should use Request.Context().Done() instead.
// Done returns nil (chan which will wait forever) when c.Request with no Context.
func (c *Context) Done() <-chan struct{} {
return nil
if c.Request == nil || c.Request.Context() == nil {
return nil
}
return c.Request.Context().Done()
}

// Err always returns nil, maybe you want to use Request.Context().Err() instead.
// Err returns nil when c.Request with no Context.
func (c *Context) Err() error {
return nil
if c.Request == nil || c.Request.Context() == nil {
return nil
}
return c.Request.Context().Err()
}

// Value returns the value associated with this context for key, or nil
Expand Down
42 changes: 42 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2058,6 +2058,48 @@ func TestRemoteIPFail(t *testing.T) {
assert.False(t, trust)
}

func TestContextWithFallbackDeadlineFromRequestContext(t *testing.T) {
c := &Context{}
deadline, ok := c.Deadline()
assert.Zero(t, deadline)
assert.False(t, ok)

c2 := &Context{}
c2.Request, _ = http.NewRequest(http.MethodGet, "/", nil)
d := time.Now().Add(time.Second)
ctx, cancel := context.WithDeadline(context.Background(), d)
defer cancel()
c2.Request = c2.Request.WithContext(ctx)
deadline, ok = c2.Deadline()
assert.Equal(t, d, deadline)
assert.True(t, ok)
}

func TestContextWithFallbackDoneFromRequestContext(t *testing.T) {
c := &Context{}
assert.Nil(t, c.Done())

c2 := &Context{}
c2.Request, _ = http.NewRequest(http.MethodGet, "/", nil)
ctx, cancel := context.WithCancel(context.Background())
c2.Request = c2.Request.WithContext(ctx)
cancel()
assert.NotNil(t, <-c2.Done())
}

func TestContextWithFallbackErrFromRequestContext(t *testing.T) {
c := &Context{}
assert.Nil(t, c.Err())

c2 := &Context{}
c2.Request, _ = http.NewRequest(http.MethodGet, "/", nil)
ctx, cancel := context.WithCancel(context.Background())
c2.Request = c2.Request.WithContext(ctx)
cancel()

assert.EqualError(t, c2.Err(), context.Canceled.Error())
}

func TestContextWithFallbackValueFromRequestContext(t *testing.T) {
tests := []struct {
name string
Expand Down