Skip to content
Myr

fix(gin): data race warning for gin mode (#1580)

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

* fix: data race warning (#1180) * Fix the tests * refactor: remove unnecessary imports and optimize codebase - Remove unnecessary import of `flag` Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com> * test: refactor test assertions for mode settings - Update test assertions for mode setting in `mode_test.go` Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com> ---------

authored by Kostadin Plachkov · committed by GitHub · parent f5f5da8 · Browse files · 3 files+17 added−25 removed
This commit

State

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

Trailers

  • Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>

Diff stats

3 files+17 added−25 removed

  • other:+17 added−25 removed

3 files+17 added−25 removed

modified:debug.go+2 added-1 removed
8 unmodified lines
9 "html/template"10 "runtime"11 "strconv"12 "strings"13 "sync/atomic"14)1516const ginSupportMinGoVer = 181718// IsDebugging returns true if the framework is running in debug mode.19// Use SetMode(gin.ReleaseMode) to disable debug mode.20func IsDebugging() bool {20 return ginMode == debugCode21 return atomic.LoadInt32(&ginMode) == debugCode22}2324// DebugPrintRouteFunc indicates debug log output format.25var DebugPrintRouteFunc func(httpMethod, absolutePath, handlerName string, nuHandlers int)
87 unmodified lines
modified:mode_test.go+6 added-13 removed
3 unmodified lines
45package gin67import (8 "flag"8 "os"9 "sync/atomic"10 "testing"1112 "github.com/gin-gonic/gin/binding"13 "github.com/stretchr/testify/assert"
3 unmodified lines
17 os.Setenv(EnvGinMode, TestMode)18}1920func TestSetMode(t *testing.T) {21 assert.Equal(t, testCode, ginMode)21 assert.Equal(t, int32(testCode), atomic.LoadInt32(&ginMode))22 assert.Equal(t, TestMode, Mode())23 os.Unsetenv(EnvGinMode)2425 SetMode("")26 assert.Equal(t, testCode, ginMode)26 assert.Equal(t, int32(testCode), atomic.LoadInt32(&ginMode))27 assert.Equal(t, TestMode, Mode())2829 tmp := flag.CommandLine30 flag.CommandLine = flag.NewFlagSet("", flag.ContinueOnError)31 SetMode("")32 assert.Equal(t, debugCode, ginMode)33 assert.Equal(t, DebugMode, Mode())34 flag.CommandLine = tmp3529 SetMode(DebugMode)37 assert.Equal(t, debugCode, ginMode)30 assert.Equal(t, int32(debugCode), atomic.LoadInt32(&ginMode))31 assert.Equal(t, DebugMode, Mode())3233 SetMode(ReleaseMode)41 assert.Equal(t, releaseCode, ginMode)34 assert.Equal(t, int32(releaseCode), atomic.LoadInt32(&ginMode))35 assert.Equal(t, ReleaseMode, Mode())3637 SetMode(TestMode)45 assert.Equal(t, testCode, ginMode)38 assert.Equal(t, int32(testCode), atomic.LoadInt32(&ginMode))39 assert.Equal(t, TestMode, Mode())4041 assert.Panics(t, func() { SetMode("unknown") })42}
20 unmodified lines
modified:mode.go+9 added-11 removed
6 unmodified lines
7import (8 "flag"9 "io"10 "os"11 "sync/atomic"1213 "github.com/gin-gonic/gin/binding"14)15
27 unmodified lines
4344// DefaultErrorWriter is the default io.Writer used by Gin to debug errors45var DefaultErrorWriter io.Writer = os.Stderr4646var (47 ginMode = debugCode48 modeName = DebugMode49)47var ginMode int32 = debugCode48var modeName atomic.Value4950func init() {51 mode := os.Getenv(EnvGinMode)52 SetMode(mode)
9 unmodified lines
62 }63 }6465 switch value {67 case DebugMode:68 ginMode = debugCode66 case DebugMode, "":67 atomic.StoreInt32(&ginMode, debugCode)68 case ReleaseMode:70 ginMode = releaseCode69 atomic.StoreInt32(&ginMode, releaseCode)70 case TestMode:72 ginMode = testCode71 atomic.StoreInt32(&ginMode, testCode)72 default:73 panic("gin mode unknown: " + value + " (available mode: debug release test)")74 }7677 modeName = value75 modeName.Store(value)76}7778// DisableBindValidation closes the default validator.79func DisableBindValidation() {
13 unmodified lines
93}9495// Mode returns current gin mode.96func Mode() string {99 return modeName97 return modeName.Load().(string)98}