-
Notifications
You must be signed in to change notification settings - Fork 8k
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
feat(context): add ContextWithFallback feature flag (#3166) #3172
Conversation
Codecov Report
@@ Coverage Diff @@
## master #3172 +/- ##
=======================================
Coverage 98.22% 98.22%
=======================================
Files 43 43
Lines 3148 3148
=======================================
Hits 3092 3092
Misses 44 44
Partials 12 12
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
@wei840222 From #3166 (comment) comment
We can add a new flag and let the developer decide when to turn on the feature. so we don't need to re-open all related issues. |
oh I see, let me do it |
@appleboy |
Testing code func TestGinContextCancel(t *testing.T) {
serv := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
return
}))
defer serv.Close()
wg := &sync.WaitGroup{}
r := gin.New()
r.GET("/", func(ginctx *gin.Context) {
wg.Add(1)
ginctx = ginctx.Copy()
// start async goroutine for calling serv
go func() {
defer wg.Done()
req, err := http.NewRequestWithContext(ginctx, http.MethodGet, serv.URL, nil)
if err != nil {
panic(err)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
// context is always canceled with gin v1.8.0, it is big breaking change with gin v1.7
t.Error("context is always canceled with gin v1.8.0, it is big breaking change with gin v1.7", err)
return
}
if res.StatusCode != http.StatusOK {
log.Println("unexpected status code ", res.Status)
return
}
}()
})
go func() {
err := r.Run(":8080")
if err != nil {
panic(err)
}
}()
res, err := http.Get("http://127.0.0.1:8080/")
if err != nil {
panic(err)
}
if res.StatusCode != http.StatusOK {
panic(err)
}
wg.Wait()
} |
maybe to avoid flawky test by ensuring that:
you can use this: func TestGinContextCancel(t *testing.T) {
serv := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
return
}))
defer serv.Close()
ensureRequestIsOver := make(chan struct{})
wg := &sync.WaitGroup{}
r := gin.New()
r.GET("/", func(ginctx *gin.Context) {
wg.Add(1)
ginctx = ginctx.Copy()
// start async goroutine for calling serv
go func() {
defer wg.Done()
<-ensureRequestIsOver // ensure request on http://127.0.0.1:8080/ is done
req, err := http.NewRequestWithContext(ginctx, http.MethodGet, serv.URL, nil)
if err != nil {
panic(err)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
// context is always canceled with gin v1.8.0, it is big breaking change with gin v1.7
t.Error("context is always canceled with gin v1.8.0, it is big breaking change with gin v1.7", err)
return
}
if res.StatusCode != http.StatusOK {
log.Println("unexpected status code ", res.Status)
return
}
}()
})
l, err := net.Listen("tcp", ":8080")
if err != nil {
panic(err)
}
go func() {
s := &http.Server{
Handler: r,
}
if err := s.Serve(l); err != nil {
panic(err)
}
}()
res, err := http.Get("http://127.0.0.1:8080/")
if err != nil {
panic(err)
}
close(ensureRequestIsOver)
if res.StatusCode != http.StatusOK {
panic(err)
}
wg.Wait()
} |
…ck Context.Deadline(), Context.Done(), Context.Err() and Context.Value()
Hi @appleboy and @jerome-laforge |
Hi @wei840222, |
due to breaking change in #3166