Skip to content

Commit

Permalink
Fix: Revert "fallback Context.Deadline() Context.Done() Context.Err()…
Browse files Browse the repository at this point in the history
… to Context.Request.Context() (#2769)"

due to breaking change in issue #3166
  • Loading branch information
wei840222 committed Jun 2, 2022
1 parent 58303bd commit 045553e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 59 deletions.
24 changes: 9 additions & 15 deletions context.go
Expand Up @@ -1156,28 +1156,22 @@ func (c *Context) SetAccepted(formats ...string) {
/***** GOLANG.ORG/X/NET/CONTEXT *****/
/************************************/

// Deadline returns that there is no deadline (ok==false) when c.Request has no Context.
// Deadline always returns that there is no deadline (ok==false),
// maybe you want to use Request.Context().Deadline() instead.
func (c *Context) Deadline() (deadline time.Time, ok bool) {
if c.Request == nil || c.Request.Context() == nil {
return
}
return c.Request.Context().Deadline()
return
}

// Done returns nil (chan which will wait forever) when c.Request has no Context.
// 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.
func (c *Context) Done() <-chan struct{} {
if c.Request == nil || c.Request.Context() == nil {
return nil
}
return c.Request.Context().Done()
return nil
}

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

// Value returns the value associated with this context for key, or nil
Expand Down
46 changes: 2 additions & 44 deletions context_test.go
Expand Up @@ -2096,51 +2096,9 @@ 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())
}

type contextKey string

func TestContextWithFallbackValueFromRequestContext(t *testing.T) {
type contextKey string

tests := []struct {
name string
getContextAndKey func() (*Context, any)
Expand Down

0 comments on commit 045553e

Please sign in to comment.