Skip to content
Myr

Fix context.Copy() race condition (#1020)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Fix context.Copy race condition * Update githubapi_test.go * fix code format

authored by Raphael Gavache · committed by 田欧 · parent 62749f0 · Browse files · 3 files+29 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+29 added−0 removed

  • other:+29 added−0 removed

3 files+29 added−0 removed

modified:context_test.go+2 added
329 unmodified lines
330 assert.Equal(t, cp.index, abortIndex)331 assert.Equal(t, cp.Keys, c.Keys)332 assert.Equal(t, cp.engine, c.engine)333 assert.Equal(t, cp.Params, c.Params)334 cp.Set("foo", "notBar")335 assert.False(t, cp.Keys["foo"] == c.Keys["foo"])336}337338func TestContextHandlerName(t *testing.T) {339 c, _ := CreateTestContext(httptest.NewRecorder())
1436 unmodified lines
modified:context.go+4 added
80 unmodified lines
81 cp.writermem.ResponseWriter = nil82 cp.Writer = &cp.writermem83 cp.index = abortIndex84 cp.handlers = nil85 cp.Keys = map[string]interface{}{}86 for k, v := range c.Keys {87 cp.Keys[k] = v88 }89 return &cp90}9192// HandlerName returns the main handler's name. For example if the handler is "handleGetUsers()",
916 unmodified lines
modified:githubapi_test.go+23 added
344 unmodified lines
345 w1 := performRequest(router, "GET", path1)346 assert.Equal(t, http.StatusBadRequest, w1.Code)347}348349func TestRaceContextCopy(t *testing.T) {350 DefaultWriter = os.Stdout351 router := Default()352 router.GET("/test/copy/race", func(c *Context) {353 c.Set("1", 0)354 c.Set("2", 0)355356 // Sending a copy of the Context to two separate routines357 go readWriteKeys(c.Copy())358 go readWriteKeys(c.Copy())359 c.String(http.StatusOK, "run OK, no panics")360 })361 w := performRequest(router, "GET", "/test/copy/race")362 assert.Equal(t, "run OK, no panics", w.Body.String())363}364365func readWriteKeys(c *Context) {366 for {367 c.Set("1", rand.Int())368 c.Set("2", c.Value("1"))369 }370}371372func githubConfigRouter(router *Engine) {373 for _, route := range githubAPI {374 router.Handle(route.method, route.path, func(c *Context) {375 output := make(map[string]string, len(c.Params)+1)
99 unmodified lines