mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-14 04:08:15 +08:00
Merge 6f3c71e2611fef92d36b2fa495a878d1d59fbc10 into c3d1092b3b48addf6f9cd00fe274ec3bd14650eb
This commit is contained in:
commit
159b211eb8
3
debug.go
3
debug.go
@ -10,7 +10,6 @@ import (
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
const ginSupportMinGoVer = 23
|
||||
@ -18,7 +17,7 @@ const ginSupportMinGoVer = 23
|
||||
// IsDebugging returns true if the framework is running in debug mode.
|
||||
// Use SetMode(gin.ReleaseMode) to disable debug mode.
|
||||
func IsDebugging() bool {
|
||||
return atomic.LoadInt32(&ginMode) == debugCode
|
||||
return ginMode.Load() == debugCode
|
||||
}
|
||||
|
||||
// DebugPrintRouteFunc indicates debug log output format.
|
||||
|
8
mode.go
8
mode.go
@ -45,7 +45,7 @@ var DefaultWriter io.Writer = os.Stdout
|
||||
var DefaultErrorWriter io.Writer = os.Stderr
|
||||
|
||||
var (
|
||||
ginMode int32 = debugCode
|
||||
ginMode atomic.Int32
|
||||
modeName atomic.Value
|
||||
)
|
||||
|
||||
@ -66,11 +66,11 @@ func SetMode(value string) {
|
||||
|
||||
switch value {
|
||||
case DebugMode:
|
||||
atomic.StoreInt32(&ginMode, debugCode)
|
||||
ginMode.Store(debugCode)
|
||||
case ReleaseMode:
|
||||
atomic.StoreInt32(&ginMode, releaseCode)
|
||||
ginMode.Store(releaseCode)
|
||||
case TestMode:
|
||||
atomic.StoreInt32(&ginMode, testCode)
|
||||
ginMode.Store(testCode)
|
||||
default:
|
||||
panic("gin mode unknown: " + value + " (available mode: debug release test)")
|
||||
}
|
||||
|
11
mode_test.go
11
mode_test.go
@ -6,7 +6,6 @@ package gin
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
@ -18,24 +17,24 @@ func init() {
|
||||
}
|
||||
|
||||
func TestSetMode(t *testing.T) {
|
||||
assert.Equal(t, int32(testCode), atomic.LoadInt32(&ginMode))
|
||||
assert.Equal(t, int32(testCode), ginMode.Load())
|
||||
assert.Equal(t, TestMode, Mode())
|
||||
os.Unsetenv(EnvGinMode)
|
||||
|
||||
SetMode("")
|
||||
assert.Equal(t, int32(testCode), atomic.LoadInt32(&ginMode))
|
||||
assert.Equal(t, int32(testCode), ginMode.Load())
|
||||
assert.Equal(t, TestMode, Mode())
|
||||
|
||||
SetMode(DebugMode)
|
||||
assert.Equal(t, int32(debugCode), atomic.LoadInt32(&ginMode))
|
||||
assert.Equal(t, int32(debugCode), ginMode.Load())
|
||||
assert.Equal(t, DebugMode, Mode())
|
||||
|
||||
SetMode(ReleaseMode)
|
||||
assert.Equal(t, int32(releaseCode), atomic.LoadInt32(&ginMode))
|
||||
assert.Equal(t, int32(releaseCode), ginMode.Load())
|
||||
assert.Equal(t, ReleaseMode, Mode())
|
||||
|
||||
SetMode(TestMode)
|
||||
assert.Equal(t, int32(testCode), atomic.LoadInt32(&ginMode))
|
||||
assert.Equal(t, int32(testCode), ginMode.Load())
|
||||
assert.Equal(t, TestMode, Mode())
|
||||
|
||||
assert.Panics(t, func() { SetMode("unknown") })
|
||||
|
Loading…
x
Reference in New Issue
Block a user