diff --git a/database/gdb/gdb.go b/database/gdb/gdb.go index 40280f893..1c9936c96 100644 --- a/database/gdb/gdb.go +++ b/database/gdb/gdb.go @@ -182,12 +182,13 @@ type Map = map[string]interface{} type List = []Map const ( - gINSERT_OPTION_DEFAULT = 0 - gINSERT_OPTION_REPLACE = 1 - gINSERT_OPTION_SAVE = 2 - gINSERT_OPTION_IGNORE = 3 - gDEFAULT_BATCH_NUM = 10 // Per count for batch insert/replace/save - gDEFAULT_CONN_MAX_LIFE_TIME = 30 // Max life time for per connection in pool in seconds. + gINSERT_OPTION_DEFAULT = 0 + gINSERT_OPTION_REPLACE = 1 + gINSERT_OPTION_SAVE = 2 + gINSERT_OPTION_IGNORE = 3 + gDEFAULT_BATCH_NUM = 10 // Per count for batch insert/replace/save + gDEFAULT_CONN_MAX_IDLE_COUNT = 10 // Max idle connection count in pool. + gDEFAULT_CONN_MAX_LIFE_TIME = 30 // Max life time for per connection in pool in seconds. ) var ( @@ -226,13 +227,14 @@ func New(name ...string) (db DB, err error) { if _, ok := configs.config[group]; ok { if node, err := getConfigNodeByGroup(group, true); err == nil { c := &Core{ - group: group, - debug: gtype.NewBool(), - cache: gcache.New(), - schema: gtype.NewString(), - logger: glog.New(), - prefix: node.Prefix, - maxConnLifetime: gDEFAULT_CONN_MAX_LIFE_TIME, // Default max connection life time if user does not configure. + group: group, + debug: gtype.NewBool(), + cache: gcache.New(), + schema: gtype.NewString(), + logger: glog.New(), + prefix: node.Prefix, + maxIdleConnCount: gDEFAULT_CONN_MAX_IDLE_COUNT, + maxConnLifetime: gDEFAULT_CONN_MAX_LIFE_TIME, // Default max connection life time if user does not configure. } if v, ok := driverMap[node.Type]; ok { c.DB, err = v.New(c, node) diff --git a/debug/gdebug/gdebug.go b/debug/gdebug/gdebug.go index e4d897a7f..4771ee648 100644 --- a/debug/gdebug/gdebug.go +++ b/debug/gdebug/gdebug.go @@ -6,272 +6,3 @@ // Package gdebug contains facilities for programs to debug themselves while they are running. package gdebug - -import ( - "bytes" - "fmt" - "io/ioutil" - "os" - "os/exec" - "path/filepath" - "reflect" - "runtime" - "strconv" - "strings" - - "github.com/gogf/gf/encoding/ghash" - - "github.com/gogf/gf/crypto/gmd5" -) - -const ( - gMAX_DEPTH = 1000 - gFILTER_KEY = "/debug/gdebug/gdebug" -) - -var ( - goRootForFilter = runtime.GOROOT() // goRootForFilter is used for stack filtering purpose. - binaryVersion = "" // The version of current running binary(uint64 hex). - binaryVersionMd5 = "" // The version of current running binary(MD5). - selfPath = "" // Current running binary absolute path. -) - -func init() { - if goRootForFilter != "" { - goRootForFilter = strings.Replace(goRootForFilter, "\\", "/", -1) - } - // Initialize internal package variable: selfPath. - selfPath, _ := exec.LookPath(os.Args[0]) - if selfPath != "" { - selfPath, _ = filepath.Abs(selfPath) - } - if selfPath == "" { - selfPath, _ = filepath.Abs(os.Args[0]) - } -} - -// BinVersion returns the version of current running binary. -// It uses ghash.BKDRHash+BASE36 algorithm to calculate the unique version of the binary. -func BinVersion() string { - if binaryVersion == "" { - binaryContent, _ := ioutil.ReadFile(selfPath) - binaryVersion = strconv.FormatInt( - int64(ghash.BKDRHash(binaryContent)), - 36, - ) - } - return binaryVersion -} - -// BinVersionMd5 returns the version of current running binary. -// It uses MD5 algorithm to calculate the unique version of the binary. -func BinVersionMd5() string { - if binaryVersionMd5 == "" { - binaryVersionMd5, _ = gmd5.EncryptFile(selfPath) - } - return binaryVersionMd5 -} - -// PrintStack prints to standard error the stack trace returned by runtime.Stack. -func PrintStack(skip ...int) { - fmt.Print(Stack(skip...)) -} - -// Stack returns a formatted stack trace of the goroutine that calls it. -// It calls runtime.Stack with a large enough buffer to capture the entire trace. -func Stack(skip ...int) string { - return StackWithFilter("", skip...) -} - -// StackWithFilter returns a formatted stack trace of the goroutine that calls it. -// It calls runtime.Stack with a large enough buffer to capture the entire trace. -// -// The parameter is used to filter the path of the caller. -func StackWithFilter(filter string, skip ...int) string { - return StackWithFilters([]string{filter}, skip...) -} - -// StackWithFilters returns a formatted stack trace of the goroutine that calls it. -// It calls runtime.Stack with a large enough buffer to capture the entire trace. -// -// The parameter is a slice of strings, which are used to filter the path of the caller. -func StackWithFilters(filters []string, skip ...int) string { - number := 0 - if len(skip) > 0 { - number = skip[0] - } - name := "" - space := " " - index := 1 - buffer := bytes.NewBuffer(nil) - filtered := false - ok := true - pc, file, line, start := callerFromIndex(filters) - for i := start + number; i < gMAX_DEPTH; i++ { - if i != start { - pc, file, line, ok = runtime.Caller(i) - } - if ok { - if goRootForFilter != "" && len(file) >= len(goRootForFilter) && file[0:len(goRootForFilter)] == goRootForFilter { - continue - } - filtered = false - for _, filter := range filters { - if filter != "" && strings.Contains(file, filter) { - filtered = true - break - } - } - if filtered { - continue - } - if strings.Contains(file, gFILTER_KEY) { - continue - } - if fn := runtime.FuncForPC(pc); fn == nil { - name = "unknown" - } else { - name = fn.Name() - } - if index > 9 { - space = " " - } - buffer.WriteString(fmt.Sprintf("%d.%s%s\n %s:%d\n", index, space, name, file, line)) - index++ - } else { - break - } - } - return buffer.String() -} - -// CallerPath returns the function name and the absolute file path along with its line number of the caller. -func Caller(skip ...int) (function string, path string, line int) { - return CallerWithFilter("", skip...) -} - -// CallerPathWithFilter returns the function name and the absolute file path along with its line number of the caller. -// -// The parameter is used to filter the path of the caller. -func CallerWithFilter(filter string, skip ...int) (function string, path string, line int) { - number := 0 - if len(skip) > 0 { - number = skip[0] - } - ok := true - pc, file, line, start := callerFromIndex([]string{filter}) - if start != -1 { - for i := start + number; i < gMAX_DEPTH; i++ { - if i != start { - pc, file, line, ok = runtime.Caller(i) - } - if ok { - if filter != "" && strings.Contains(file, filter) { - continue - } - if strings.Contains(file, gFILTER_KEY) { - continue - } - function := "" - if fn := runtime.FuncForPC(pc); fn == nil { - function = "unknown" - } else { - function = fn.Name() - } - return function, file, line - } else { - break - } - } - } - return "", "", -1 -} - -// callerFromIndex returns the caller position and according information exclusive of the debug package. -func callerFromIndex(filters []string) (pc uintptr, file string, line int, index int) { - var filtered, ok bool - for index = 0; index < gMAX_DEPTH; index++ { - if pc, file, line, ok = runtime.Caller(index); ok { - filtered = false - for _, filter := range filters { - if filter != "" && strings.Contains(file, filter) { - filtered = true - break - } - } - if filtered { - continue - } - if strings.Contains(file, gFILTER_KEY) { - continue - } - return - } - } - return 0, "", -1, -1 -} - -// CallerPackage returns the package name of the caller. -func CallerPackage() string { - function, _, _ := Caller() - indexSplit := strings.LastIndexByte(function, '/') - if indexSplit == -1 { - return function[:strings.IndexByte(function, '.')] - } else { - leftPart := function[:indexSplit+1] - rightPart := function[indexSplit+1:] - indexDot := strings.IndexByte(function, '.') - rightPart = rightPart[:indexDot-1] - return leftPart + rightPart - } -} - -// CallerFunction returns the function name of the caller. -func CallerFunction() string { - function, _, _ := Caller() - function = function[strings.LastIndexByte(function, '/')+1:] - function = function[strings.IndexByte(function, '.')+1:] - return function -} - -// CallerFilePath returns the file path of the caller. -func CallerFilePath() string { - _, path, _ := Caller() - return path -} - -// CallerDirectory returns the directory of the caller. -func CallerDirectory() string { - _, path, _ := Caller() - return filepath.Dir(path) -} - -// CallerFileLine returns the file path along with the line number of the caller. -func CallerFileLine() string { - _, path, line := Caller() - return fmt.Sprintf(`%s:%d`, path, line) -} - -// CallerFileLineShort returns the file name along with the line number of the caller. -func CallerFileLineShort() string { - _, path, line := Caller() - return fmt.Sprintf(`%s:%d`, filepath.Base(path), line) -} - -// FuncPath returns the complete function path of given . -func FuncPath(f interface{}) string { - return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name() -} - -// FuncName returns the function name of given . -func FuncName(f interface{}) string { - path := FuncPath(f) - if path == "" { - return "" - } - index := strings.LastIndexByte(path, '/') - if index < 0 { - index = strings.LastIndexByte(path, '\\') - } - return path[index+1:] -} diff --git a/debug/gdebug/gdebug_caller.go b/debug/gdebug/gdebug_caller.go new file mode 100644 index 000000000..13f7391cf --- /dev/null +++ b/debug/gdebug/gdebug_caller.go @@ -0,0 +1,174 @@ +// Copyright 2019-2020 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gdebug + +import ( + "fmt" + "os" + "os/exec" + "path/filepath" + "reflect" + "runtime" + "strings" +) + +const ( + gMAX_DEPTH = 1000 + gFILTER_KEY = "/debug/gdebug/gdebug" +) + +var ( + goRootForFilter = runtime.GOROOT() // goRootForFilter is used for stack filtering purpose. + binaryVersion = "" // The version of current running binary(uint64 hex). + binaryVersionMd5 = "" // The version of current running binary(MD5). + selfPath = "" // Current running binary absolute path. +) + +func init() { + if goRootForFilter != "" { + goRootForFilter = strings.Replace(goRootForFilter, "\\", "/", -1) + } + // Initialize internal package variable: selfPath. + selfPath, _ := exec.LookPath(os.Args[0]) + if selfPath != "" { + selfPath, _ = filepath.Abs(selfPath) + } + if selfPath == "" { + selfPath, _ = filepath.Abs(os.Args[0]) + } +} + +// CallerPath returns the function name and the absolute file path along with its line number of the caller. +func Caller(skip ...int) (function string, path string, line int) { + return CallerWithFilter("", skip...) +} + +// CallerPathWithFilter returns the function name and the absolute file path along with its line number of the caller. +// +// The parameter is used to filter the path of the caller. +func CallerWithFilter(filter string, skip ...int) (function string, path string, line int) { + number := 0 + if len(skip) > 0 { + number = skip[0] + } + ok := true + pc, file, line, start := callerFromIndex([]string{filter}) + if start != -1 { + for i := start + number; i < gMAX_DEPTH; i++ { + if i != start { + pc, file, line, ok = runtime.Caller(i) + } + if ok { + if filter != "" && strings.Contains(file, filter) { + continue + } + if strings.Contains(file, gFILTER_KEY) { + continue + } + function := "" + if fn := runtime.FuncForPC(pc); fn == nil { + function = "unknown" + } else { + function = fn.Name() + } + return function, file, line + } else { + break + } + } + } + return "", "", -1 +} + +// callerFromIndex returns the caller position and according information exclusive of the debug package. +func callerFromIndex(filters []string) (pc uintptr, file string, line int, index int) { + var filtered, ok bool + for index = 0; index < gMAX_DEPTH; index++ { + if pc, file, line, ok = runtime.Caller(index); ok { + filtered = false + for _, filter := range filters { + if filter != "" && strings.Contains(file, filter) { + filtered = true + break + } + } + if filtered { + continue + } + if strings.Contains(file, gFILTER_KEY) { + continue + } + return + } + } + return 0, "", -1, -1 +} + +// CallerPackage returns the package name of the caller. +func CallerPackage() string { + function, _, _ := Caller() + indexSplit := strings.LastIndexByte(function, '/') + if indexSplit == -1 { + return function[:strings.IndexByte(function, '.')] + } else { + leftPart := function[:indexSplit+1] + rightPart := function[indexSplit+1:] + indexDot := strings.IndexByte(function, '.') + rightPart = rightPart[:indexDot-1] + return leftPart + rightPart + } +} + +// CallerFunction returns the function name of the caller. +func CallerFunction() string { + function, _, _ := Caller() + function = function[strings.LastIndexByte(function, '/')+1:] + function = function[strings.IndexByte(function, '.')+1:] + return function +} + +// CallerFilePath returns the file path of the caller. +func CallerFilePath() string { + _, path, _ := Caller() + return path +} + +// CallerDirectory returns the directory of the caller. +func CallerDirectory() string { + _, path, _ := Caller() + return filepath.Dir(path) +} + +// CallerFileLine returns the file path along with the line number of the caller. +func CallerFileLine() string { + _, path, line := Caller() + return fmt.Sprintf(`%s:%d`, path, line) +} + +// CallerFileLineShort returns the file name along with the line number of the caller. +func CallerFileLineShort() string { + _, path, line := Caller() + return fmt.Sprintf(`%s:%d`, filepath.Base(path), line) +} + +// FuncPath returns the complete function path of given . +func FuncPath(f interface{}) string { + return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name() +} + +// FuncName returns the function name of given . +func FuncName(f interface{}) string { + path := FuncPath(f) + if path == "" { + return "" + } + index := strings.LastIndexByte(path, '/') + if index < 0 { + index = strings.LastIndexByte(path, '\\') + } + return path[index+1:] +} diff --git a/debug/gdebug/gdebug_stack.go b/debug/gdebug/gdebug_stack.go new file mode 100644 index 000000000..498f03bf2 --- /dev/null +++ b/debug/gdebug/gdebug_stack.go @@ -0,0 +1,87 @@ +// Copyright 2019-2020 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gdebug + +import ( + "bytes" + "fmt" + "runtime" + "strings" +) + +// PrintStack prints to standard error the stack trace returned by runtime.Stack. +func PrintStack(skip ...int) { + fmt.Print(Stack(skip...)) +} + +// Stack returns a formatted stack trace of the goroutine that calls it. +// It calls runtime.Stack with a large enough buffer to capture the entire trace. +func Stack(skip ...int) string { + return StackWithFilter("", skip...) +} + +// StackWithFilter returns a formatted stack trace of the goroutine that calls it. +// It calls runtime.Stack with a large enough buffer to capture the entire trace. +// +// The parameter is used to filter the path of the caller. +func StackWithFilter(filter string, skip ...int) string { + return StackWithFilters([]string{filter}, skip...) +} + +// StackWithFilters returns a formatted stack trace of the goroutine that calls it. +// It calls runtime.Stack with a large enough buffer to capture the entire trace. +// +// The parameter is a slice of strings, which are used to filter the path of the caller. +func StackWithFilters(filters []string, skip ...int) string { + number := 0 + if len(skip) > 0 { + number = skip[0] + } + name := "" + space := " " + index := 1 + buffer := bytes.NewBuffer(nil) + filtered := false + ok := true + pc, file, line, start := callerFromIndex(filters) + for i := start + number; i < gMAX_DEPTH; i++ { + if i != start { + pc, file, line, ok = runtime.Caller(i) + } + if ok { + if goRootForFilter != "" && len(file) >= len(goRootForFilter) && file[0:len(goRootForFilter)] == goRootForFilter { + continue + } + filtered = false + for _, filter := range filters { + if filter != "" && strings.Contains(file, filter) { + filtered = true + break + } + } + if filtered { + continue + } + if strings.Contains(file, gFILTER_KEY) { + continue + } + if fn := runtime.FuncForPC(pc); fn == nil { + name = "unknown" + } else { + name = fn.Name() + } + if index > 9 { + space = " " + } + buffer.WriteString(fmt.Sprintf("%d.%s%s\n %s:%d\n", index, space, name, file, line)) + index++ + } else { + break + } + } + return buffer.String() +} diff --git a/debug/gdebug/gdebug_testdata.go b/debug/gdebug/gdebug_testdata.go new file mode 100644 index 000000000..e7f209970 --- /dev/null +++ b/debug/gdebug/gdebug_testdata.go @@ -0,0 +1,17 @@ +// Copyright 2019-2020 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gdebug + +import ( + "path/filepath" +) + +// TestDataPath retrieves and returns the testdata path of current package. +// It is used for unit testing cases only. +func TestDataPath() string { + return CallerDirectory() + string(filepath.Separator) + "testdata" +} diff --git a/debug/gdebug/gdebug_version.go b/debug/gdebug/gdebug_version.go new file mode 100644 index 000000000..69a5fc97e --- /dev/null +++ b/debug/gdebug/gdebug_version.go @@ -0,0 +1,36 @@ +// Copyright 2019-2020 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gdebug + +import ( + "github.com/gogf/gf/crypto/gmd5" + "github.com/gogf/gf/encoding/ghash" + "io/ioutil" + "strconv" +) + +// BinVersion returns the version of current running binary. +// It uses ghash.BKDRHash+BASE36 algorithm to calculate the unique version of the binary. +func BinVersion() string { + if binaryVersion == "" { + binaryContent, _ := ioutil.ReadFile(selfPath) + binaryVersion = strconv.FormatInt( + int64(ghash.BKDRHash(binaryContent)), + 36, + ) + } + return binaryVersion +} + +// BinVersionMd5 returns the version of current running binary. +// It uses MD5 algorithm to calculate the unique version of the binary. +func BinVersionMd5() string { + if binaryVersionMd5 == "" { + binaryVersionMd5, _ = gmd5.EncryptFile(selfPath) + } + return binaryVersionMd5 +} diff --git a/debug/gdebug/gdebug_bench_test.go b/debug/gdebug/gdebug_z_bench_test.go similarity index 100% rename from debug/gdebug/gdebug_bench_test.go rename to debug/gdebug/gdebug_z_bench_test.go diff --git a/encoding/gbase64/gbase64_test.go b/encoding/gbase64/gbase64_test.go index 378166193..f13393587 100644 --- a/encoding/gbase64/gbase64_test.go +++ b/encoding/gbase64/gbase64_test.go @@ -67,7 +67,7 @@ func Test_Basic(t *testing.T) { } func Test_File(t *testing.T) { - path := gfile.Join(gdebug.CallerDirectory(), "testdata", "test") + path := gfile.Join(gdebug.TestDataPath(), "test") expect := "dGVzdA==" gtest.Case(t, func() { b, err := gbase64.EncodeFile(path) diff --git a/encoding/gcompress/gcompress_z_unit_gzip_test.go b/encoding/gcompress/gcompress_z_unit_gzip_test.go index 14ffab44a..694105344 100644 --- a/encoding/gcompress/gcompress_z_unit_gzip_test.go +++ b/encoding/gcompress/gcompress_z_unit_gzip_test.go @@ -43,7 +43,7 @@ func Test_Gzip_UnGzip(t *testing.T) { } func Test_Gzip_UnGzip_File(t *testing.T) { - srcPath := gfile.Join(gdebug.CallerDirectory(), "testdata", "gzip", "file.txt") + srcPath := gfile.Join(gdebug.TestDataPath(), "gzip", "file.txt") dstPath1 := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr(), "gzip.zip") dstPath2 := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr(), "file.txt") diff --git a/encoding/gcompress/gcompress_z_unit_zip_test.go b/encoding/gcompress/gcompress_z_unit_zip_test.go index 9fb0f3b1f..399bc2783 100644 --- a/encoding/gcompress/gcompress_z_unit_zip_test.go +++ b/encoding/gcompress/gcompress_z_unit_zip_test.go @@ -20,8 +20,8 @@ import ( func Test_ZipPath(t *testing.T) { // file gtest.Case(t, func() { - srcPath := gfile.Join(gdebug.CallerDirectory(), "testdata", "zip", "path1", "1.txt") - dstPath := gfile.Join(gdebug.CallerDirectory(), "testdata", "zip", "zip.zip") + srcPath := gfile.Join(gdebug.TestDataPath(), "zip", "path1", "1.txt") + dstPath := gfile.Join(gdebug.TestDataPath(), "zip", "zip.zip") gtest.Assert(gfile.Exists(dstPath), false) err := gcompress.ZipPath(srcPath, dstPath) @@ -44,8 +44,8 @@ func Test_ZipPath(t *testing.T) { }) // directory gtest.Case(t, func() { - srcPath := gfile.Join(gdebug.CallerDirectory(), "testdata", "zip") - dstPath := gfile.Join(gdebug.CallerDirectory(), "testdata", "zip", "zip.zip") + srcPath := gfile.Join(gdebug.TestDataPath(), "zip") + dstPath := gfile.Join(gdebug.TestDataPath(), "zip", "zip.zip") pwd := gfile.Pwd() err := gfile.Chdir(srcPath) @@ -77,10 +77,10 @@ func Test_ZipPath(t *testing.T) { }) // multiple paths joined using char ',' gtest.Case(t, func() { - srcPath := gfile.Join(gdebug.CallerDirectory(), "testdata", "zip") - srcPath1 := gfile.Join(gdebug.CallerDirectory(), "testdata", "zip", "path1") - srcPath2 := gfile.Join(gdebug.CallerDirectory(), "testdata", "zip", "path2") - dstPath := gfile.Join(gdebug.CallerDirectory(), "testdata", "zip", "zip.zip") + srcPath := gfile.Join(gdebug.TestDataPath(), "zip") + srcPath1 := gfile.Join(gdebug.TestDataPath(), "zip", "path1") + srcPath2 := gfile.Join(gdebug.TestDataPath(), "zip", "path2") + dstPath := gfile.Join(gdebug.TestDataPath(), "zip", "zip.zip") pwd := gfile.Pwd() err := gfile.Chdir(srcPath) @@ -116,9 +116,9 @@ func Test_ZipPath(t *testing.T) { func Test_ZipPathWriter(t *testing.T) { gtest.Case(t, func() { - srcPath := gfile.Join(gdebug.CallerDirectory(), "testdata", "zip") - srcPath1 := gfile.Join(gdebug.CallerDirectory(), "testdata", "zip", "path1") - srcPath2 := gfile.Join(gdebug.CallerDirectory(), "testdata", "zip", "path2") + srcPath := gfile.Join(gdebug.TestDataPath(), "zip") + srcPath1 := gfile.Join(gdebug.TestDataPath(), "zip", "path1") + srcPath2 := gfile.Join(gdebug.TestDataPath(), "zip", "path2") pwd := gfile.Pwd() err := gfile.Chdir(srcPath) diff --git a/frame/gins/gins_z_unit_basic_test.go b/frame/gins/gins_z_unit_basic_test.go index 8322dacb7..5820720e8 100644 --- a/frame/gins/gins_z_unit_basic_test.go +++ b/frame/gins/gins_z_unit_basic_test.go @@ -4,9 +4,10 @@ // If a copy of the MIT was not distributed with this file, // You can obtain one at https://github.com/gogf/gf. -package gins +package gins_test import ( + "github.com/gogf/gf/frame/gins" "testing" "github.com/gogf/gf/test/gtest" @@ -14,30 +15,30 @@ import ( func Test_SetGet(t *testing.T) { gtest.Case(t, func() { - Set("test-user", 1) - gtest.Assert(Get("test-user"), 1) - gtest.Assert(Get("none-exists"), nil) + gins.Set("test-user", 1) + gtest.Assert(gins.Get("test-user"), 1) + gtest.Assert(gins.Get("none-exists"), nil) }) gtest.Case(t, func() { - gtest.Assert(GetOrSet("test-1", 1), 1) - gtest.Assert(Get("test-1"), 1) + gtest.Assert(gins.GetOrSet("test-1", 1), 1) + gtest.Assert(gins.Get("test-1"), 1) }) gtest.Case(t, func() { - gtest.Assert(GetOrSetFunc("test-2", func() interface{} { + gtest.Assert(gins.GetOrSetFunc("test-2", func() interface{} { return 2 }), 2) - gtest.Assert(Get("test-2"), 2) + gtest.Assert(gins.Get("test-2"), 2) }) gtest.Case(t, func() { - gtest.Assert(GetOrSetFuncLock("test-3", func() interface{} { + gtest.Assert(gins.GetOrSetFuncLock("test-3", func() interface{} { return 3 }), 3) - gtest.Assert(Get("test-3"), 3) + gtest.Assert(gins.Get("test-3"), 3) }) gtest.Case(t, func() { - gtest.Assert(SetIfNotExist("test-4", 4), true) - gtest.Assert(Get("test-4"), 4) - gtest.Assert(SetIfNotExist("test-4", 5), false) - gtest.Assert(Get("test-4"), 4) + gtest.Assert(gins.SetIfNotExist("test-4", 4), true) + gtest.Assert(gins.Get("test-4"), 4) + gtest.Assert(gins.SetIfNotExist("test-4", 5), false) + gtest.Assert(gins.Get("test-4"), 4) }) } diff --git a/frame/gins/gins_z_unit_config_test.go b/frame/gins/gins_z_unit_config_test.go index 412d4957f..74c03d401 100644 --- a/frame/gins/gins_z_unit_config_test.go +++ b/frame/gins/gins_z_unit_config_test.go @@ -4,10 +4,12 @@ // If a copy of the MIT was not distributed with this file, // You can obtain one at https://github.com/gogf/gf. -package gins +package gins_test import ( "fmt" + "github.com/gogf/gf/debug/gdebug" + "github.com/gogf/gf/frame/gins" "testing" "time" @@ -18,157 +20,186 @@ import ( "github.com/gogf/gf/test/gtest" ) -func Test_Config(t *testing.T) { - config := ` -# 模板引擎目录 -viewpath = "/home/www/templates/" -test = "v=1" -# MySQL数据库配置 -[database] - [[database.default]] - host = "127.0.0.1" - port = "3306" - user = "root" - pass = "" - name = "test" - type = "mysql" - role = "master" - charset = "utf8" - priority = "1" - [[database.default]] - host = "127.0.0.1" - port = "3306" - user = "root" - pass = "8692651" - name = "test" - type = "mysql" - role = "master" - charset = "utf8" - priority = "1" -# Redis数据库配置 -[redis] - disk = "127.0.0.1:6379,0" - cache = "127.0.0.1:6379,1" -` - gtest.Case(t, func() { - gtest.AssertNE(Config(), nil) - }) +var ( + configContent = gfile.GetContents( + gfile.Join(gdebug.TestDataPath(), "config", "config.toml"), + ) +) +func Test_Config1(t *testing.T) { + gtest.Case(t, func() { + gtest.AssertNE(configContent, "") + }) + gtest.Case(t, func() { + gtest.AssertNE(gins.Config(), nil) + }) +} + +func Test_Config2(t *testing.T) { // relative path gtest.Case(t, func() { - path := "config.toml" - err := gfile.PutContents(path, config) + var err error + dirPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr()) + err = gfile.Mkdir(dirPath) gtest.Assert(err, nil) - defer gfile.Remove(path) - defer Config().Clear() - gtest.Assert(Config().Get("test"), "v=1") - gtest.Assert(Config().Get("database.default.1.host"), "127.0.0.1") - gtest.Assert(Config().Get("redis.disk"), "127.0.0.1:6379,0") + defer gfile.Remove(dirPath) + + name := "config.toml" + err = gfile.PutContents(gfile.Join(dirPath, name), configContent) + gtest.Assert(err, nil) + + err = gins.Config().AddPath(dirPath) + gtest.Assert(err, nil) + + defer gins.Config().Clear() + + gtest.Assert(gins.Config().Get("test"), "v=1") + gtest.Assert(gins.Config().Get("database.default.1.host"), "127.0.0.1") + gtest.Assert(gins.Config().Get("redis.disk"), "127.0.0.1:6379,0") }) // for gfsnotify callbacks to refresh cache of config file time.Sleep(500 * time.Millisecond) // relative path, config folder gtest.Case(t, func() { - path := "config/config.toml" - err := gfile.PutContents(path, config) + var err error + dirPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr()) + err = gfile.Mkdir(dirPath) gtest.Assert(err, nil) - defer gfile.Remove(path) - defer Config().Clear() - gtest.Assert(Config().Get("test"), "v=1") - gtest.Assert(Config().Get("database.default.1.host"), "127.0.0.1") - gtest.Assert(Config().Get("redis.disk"), "127.0.0.1:6379,0") - }) - // for gfsnotify callbacks to refresh cache of config file - time.Sleep(500 * time.Millisecond) + defer gfile.Remove(dirPath) - gtest.Case(t, func() { - path := "test.toml" - err := gfile.PutContents(path, config) + name := "config/config.toml" + err = gfile.PutContents(gfile.Join(dirPath, name), configContent) gtest.Assert(err, nil) - defer gfile.Remove(path) - defer Config("test").Clear() - Config("test").SetFileName("test.toml") - gtest.Assert(Config("test").Get("test"), "v=1") - gtest.Assert(Config("test").Get("database.default.1.host"), "127.0.0.1") - gtest.Assert(Config("test").Get("redis.disk"), "127.0.0.1:6379,0") - }) - // for gfsnotify callbacks to refresh cache of config file - time.Sleep(500 * time.Millisecond) - gtest.Case(t, func() { - path := "config/test.toml" - err := gfile.PutContents(path, config) + err = gins.Config().AddPath(dirPath) gtest.Assert(err, nil) - defer gfile.Remove(path) - defer Config("test").Clear() - Config("test").SetFileName("test.toml") - gtest.Assert(Config("test").Get("test"), "v=1") - gtest.Assert(Config("test").Get("database.default.1.host"), "127.0.0.1") - gtest.Assert(Config("test").Get("redis.disk"), "127.0.0.1:6379,0") - }) - // for gfsnotify callbacks to refresh cache of config file - time.Sleep(500 * time.Millisecond) - // absolute path - gtest.Case(t, func() { - path := fmt.Sprintf(`%s/%d`, gfile.TempDir(), gtime.TimestampNano()) - file := fmt.Sprintf(`%s/%s`, path, "config.toml") - err := gfile.PutContents(file, config) - gtest.Assert(err, nil) - defer gfile.Remove(file) - defer Config().Clear() - gtest.Assert(Config().AddPath(path), nil) - gtest.Assert(Config().Get("test"), "v=1") - gtest.Assert(Config().Get("database.default.1.host"), "127.0.0.1") - gtest.Assert(Config().Get("redis.disk"), "127.0.0.1:6379,0") - }) - time.Sleep(500 * time.Millisecond) + defer gins.Config().Clear() - gtest.Case(t, func() { - path := fmt.Sprintf(`%s/%d/config`, gfile.TempDir(), gtime.TimestampNano()) - file := fmt.Sprintf(`%s/%s`, path, "config.toml") - err := gfile.PutContents(file, config) - gtest.Assert(err, nil) - defer gfile.Remove(file) - defer Config().Clear() - gtest.Assert(Config().AddPath(path), nil) - gtest.Assert(Config().Get("test"), "v=1") - gtest.Assert(Config().Get("database.default.1.host"), "127.0.0.1") - gtest.Assert(Config().Get("redis.disk"), "127.0.0.1:6379,0") - }) - time.Sleep(500 * time.Millisecond) + gtest.Assert(gins.Config().Get("test"), "v=1") + gtest.Assert(gins.Config().Get("database.default.1.host"), "127.0.0.1") + gtest.Assert(gins.Config().Get("redis.disk"), "127.0.0.1:6379,0") - gtest.Case(t, func() { - path := fmt.Sprintf(`%s/%d`, gfile.TempDir(), gtime.TimestampNano()) - file := fmt.Sprintf(`%s/%s`, path, "test.toml") - err := gfile.PutContents(file, config) - gtest.Assert(err, nil) - defer gfile.Remove(file) - defer Config("test").Clear() - Config("test").SetFileName("test.toml") - gtest.Assert(Config("test").AddPath(path), nil) - gtest.Assert(Config("test").Get("test"), "v=1") - gtest.Assert(Config("test").Get("database.default.1.host"), "127.0.0.1") - gtest.Assert(Config("test").Get("redis.disk"), "127.0.0.1:6379,0") - }) - time.Sleep(500 * time.Millisecond) - - gtest.Case(t, func() { - path := fmt.Sprintf(`%s/%d/config`, gfile.TempDir(), gtime.TimestampNano()) - file := fmt.Sprintf(`%s/%s`, path, "test.toml") - err := gfile.PutContents(file, config) - gtest.Assert(err, nil) - defer gfile.Remove(file) - defer Config().Clear() - Config("test").SetFileName("test.toml") - gtest.Assert(Config("test").AddPath(path), nil) - gtest.Assert(Config("test").Get("test"), "v=1") - gtest.Assert(Config("test").Get("database.default.1.host"), "127.0.0.1") - gtest.Assert(Config("test").Get("redis.disk"), "127.0.0.1:6379,0") + // for gfsnotify callbacks to refresh cache of config file + time.Sleep(500 * time.Millisecond) }) } +func Test_Config3(t *testing.T) { + gtest.Case(t, func() { + gtest.Case(t, func() { + var err error + dirPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr()) + err = gfile.Mkdir(dirPath) + gtest.Assert(err, nil) + defer gfile.Remove(dirPath) + + name := "test.toml" + err = gfile.PutContents(gfile.Join(dirPath, name), configContent) + gtest.Assert(err, nil) + + err = gins.Config("test").AddPath(dirPath) + gtest.Assert(err, nil) + + defer gins.Config("test").Clear() + gins.Config("test").SetFileName("test.toml") + + gtest.Assert(gins.Config("test").Get("test"), "v=1") + gtest.Assert(gins.Config("test").Get("database.default.1.host"), "127.0.0.1") + gtest.Assert(gins.Config("test").Get("redis.disk"), "127.0.0.1:6379,0") + }) + // for gfsnotify callbacks to refresh cache of config file + time.Sleep(500 * time.Millisecond) + + gtest.Case(t, func() { + var err error + dirPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr()) + err = gfile.Mkdir(dirPath) + gtest.Assert(err, nil) + defer gfile.Remove(dirPath) + + name := "config/test.toml" + err = gfile.PutContents(gfile.Join(dirPath, name), configContent) + gtest.Assert(err, nil) + + err = gins.Config("test").AddPath(dirPath) + gtest.Assert(err, nil) + + defer gins.Config("test").Clear() + gins.Config("test").SetFileName("test.toml") + + gtest.Assert(gins.Config("test").Get("test"), "v=1") + gtest.Assert(gins.Config("test").Get("database.default.1.host"), "127.0.0.1") + gtest.Assert(gins.Config("test").Get("redis.disk"), "127.0.0.1:6379,0") + }) + // for gfsnotify callbacks to refresh cache of config file for next unit testing case. + time.Sleep(500 * time.Millisecond) + }) +} + +func Test_Config4(t *testing.T) { + gtest.Case(t, func() { + // absolute path + gtest.Case(t, func() { + path := fmt.Sprintf(`%s/%d`, gfile.TempDir(), gtime.TimestampNano()) + file := fmt.Sprintf(`%s/%s`, path, "config.toml") + err := gfile.PutContents(file, configContent) + gtest.Assert(err, nil) + defer gfile.Remove(file) + defer gins.Config().Clear() + + gtest.Assert(gins.Config().AddPath(path), nil) + gtest.Assert(gins.Config().Get("test"), "v=1") + gtest.Assert(gins.Config().Get("database.default.1.host"), "127.0.0.1") + gtest.Assert(gins.Config().Get("redis.disk"), "127.0.0.1:6379,0") + }) + time.Sleep(500 * time.Millisecond) + + gtest.Case(t, func() { + path := fmt.Sprintf(`%s/%d/config`, gfile.TempDir(), gtime.TimestampNano()) + file := fmt.Sprintf(`%s/%s`, path, "config.toml") + err := gfile.PutContents(file, configContent) + gtest.Assert(err, nil) + defer gfile.Remove(file) + defer gins.Config().Clear() + gtest.Assert(gins.Config().AddPath(path), nil) + gtest.Assert(gins.Config().Get("test"), "v=1") + gtest.Assert(gins.Config().Get("database.default.1.host"), "127.0.0.1") + gtest.Assert(gins.Config().Get("redis.disk"), "127.0.0.1:6379,0") + }) + time.Sleep(500 * time.Millisecond) + + gtest.Case(t, func() { + path := fmt.Sprintf(`%s/%d`, gfile.TempDir(), gtime.TimestampNano()) + file := fmt.Sprintf(`%s/%s`, path, "test.toml") + err := gfile.PutContents(file, configContent) + gtest.Assert(err, nil) + defer gfile.Remove(file) + defer gins.Config("test").Clear() + gins.Config("test").SetFileName("test.toml") + gtest.Assert(gins.Config("test").AddPath(path), nil) + gtest.Assert(gins.Config("test").Get("test"), "v=1") + gtest.Assert(gins.Config("test").Get("database.default.1.host"), "127.0.0.1") + gtest.Assert(gins.Config("test").Get("redis.disk"), "127.0.0.1:6379,0") + }) + time.Sleep(500 * time.Millisecond) + + gtest.Case(t, func() { + path := fmt.Sprintf(`%s/%d/config`, gfile.TempDir(), gtime.TimestampNano()) + file := fmt.Sprintf(`%s/%s`, path, "test.toml") + err := gfile.PutContents(file, configContent) + gtest.Assert(err, nil) + defer gfile.Remove(file) + defer gins.Config().Clear() + gins.Config("test").SetFileName("test.toml") + gtest.Assert(gins.Config("test").AddPath(path), nil) + gtest.Assert(gins.Config("test").Get("test"), "v=1") + gtest.Assert(gins.Config("test").Get("database.default.1.host"), "127.0.0.1") + gtest.Assert(gins.Config("test").Get("redis.disk"), "127.0.0.1:6379,0") + }) + }) +} func Test_Basic2(t *testing.T) { config := `log-path = "logs"` gtest.Case(t, func() { @@ -179,6 +210,6 @@ func Test_Basic2(t *testing.T) { _ = gfile.Remove(path) }() - gtest.Assert(Config().Get("log-path"), "logs") + gtest.Assert(gins.Config().Get("log-path"), "logs") }) } diff --git a/frame/gins/gins_z_unit_database_test.go b/frame/gins/gins_z_unit_database_test.go index 9e652c881..16d2b3a03 100644 --- a/frame/gins/gins_z_unit_database_test.go +++ b/frame/gins/gins_z_unit_database_test.go @@ -4,9 +4,12 @@ // If a copy of the MIT was not distributed with this file, // You can obtain one at https://github.com/gogf/gf. -package gins +package gins_test import ( + "github.com/gogf/gf/debug/gdebug" + "github.com/gogf/gf/frame/gins" + "github.com/gogf/gf/os/gtime" "testing" "time" @@ -15,42 +18,22 @@ import ( ) func Test_Database(t *testing.T) { - config := ` -# 模板引擎目录 -viewpath = "/home/www/templates/" -test = "v=2" -# MySQL数据库配置 -[database] - [[database.default]] - host = "127.0.0.1" - port = "3306" - user = "root" - pass = "12345678" - name = "test" - type = "mysql" - role = "master" - weight = "1" - charset = "utf8" - [[database.test]] - host = "127.0.0.1" - port = "3306" - user = "root" - pass = "12345678" - name = "test" - type = "mysql" - role = "master" - weight = "1" - charset = "utf8" -# Redis数据库配置 -[redis] - default = "127.0.0.1:6379,0" - cache = "127.0.0.1:6379,1" -` - path := "config.toml" - err := gfile.PutContents(path, config) + databaseContent := gfile.GetContents(gfile.Join(gdebug.TestDataPath(), "database", "config.toml")) + + var err error + dirPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr()) + err = gfile.Mkdir(dirPath) gtest.Assert(err, nil) - defer gfile.Remove(path) - defer Config().Clear() + defer gfile.Remove(dirPath) + + name := "config.toml" + err = gfile.PutContents(gfile.Join(dirPath, name), databaseContent) + gtest.Assert(err, nil) + + err = gins.Config().AddPath(dirPath) + gtest.Assert(err, nil) + + defer gins.Config().Clear() // for gfsnotify callbacks to refresh cache of config file time.Sleep(500 * time.Millisecond) @@ -58,8 +41,8 @@ test = "v=2" gtest.Case(t, func() { //fmt.Println("gins Test_Database", Config().Get("test")) - dbDefault := Database() - dbTest := Database("test") + dbDefault := gins.Database() + dbTest := gins.Database("test") gtest.AssertNE(dbDefault, nil) gtest.AssertNE(dbTest, nil) diff --git a/frame/gins/gins_z_unit_redis_test.go b/frame/gins/gins_z_unit_redis_test.go index 933203b49..1101a81a8 100644 --- a/frame/gins/gins_z_unit_redis_test.go +++ b/frame/gins/gins_z_unit_redis_test.go @@ -4,9 +4,12 @@ // If a copy of the MIT was not distributed with this file, // You can obtain one at https://github.com/gogf/gf. -package gins +package gins_test import ( + "github.com/gogf/gf/debug/gdebug" + "github.com/gogf/gf/frame/gins" + "github.com/gogf/gf/os/gtime" "testing" "time" @@ -15,45 +18,22 @@ import ( ) func Test_Redis(t *testing.T) { - config := ` -# 模板引擎目录 -viewpath = "/home/www/templates/" -test = "v=3" -# MySQL数据库配置 -[database] - [[database.default]] - host = "127.0.0.1" - port = "3306" - user = "root" - pass = "" - # pass = "12345678" - name = "test" - type = "mysql" - role = "master" - charset = "utf8" - priority = "1" - [[database.test]] - host = "127.0.0.1" - port = "3306" - user = "root" - pass = "" - # pass = "12345678" - name = "test" - type = "mysql" - role = "master" - charset = "utf8" - priority = "1" -# Redis数据库配置 -[redis] - default = "127.0.0.1:6379,7" - cache = "127.0.0.1:6379,8" - disk = "127.0.0.1:6379,9,?maxIdle=1&maxActive=10&idleTimeout=10&maxConnLifetime=10" -` - path := "config.toml" - err := gfile.PutContents(path, config) + redisContent := gfile.GetContents(gfile.Join(gdebug.TestDataPath(), "redis", "config.toml")) + + var err error + dirPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr()) + err = gfile.Mkdir(dirPath) gtest.Assert(err, nil) - defer gfile.Remove(path) - defer Config().Clear() + defer gfile.Remove(dirPath) + + name := "config.toml" + err = gfile.PutContents(gfile.Join(dirPath, name), redisContent) + gtest.Assert(err, nil) + + err = gins.Config().AddPath(dirPath) + gtest.Assert(err, nil) + + defer gins.Config().Clear() // for gfsnotify callbacks to refresh cache of config file time.Sleep(500 * time.Millisecond) @@ -61,9 +41,9 @@ test = "v=3" gtest.Case(t, func() { //fmt.Println("gins Test_Redis", Config().Get("test")) - redisDefault := Redis() - redisCache := Redis("cache") - redisDisk := Redis("disk") + redisDefault := gins.Redis() + redisCache := gins.Redis("cache") + redisDisk := gins.Redis("disk") gtest.AssertNE(redisDefault, nil) gtest.AssertNE(redisCache, nil) gtest.AssertNE(redisDisk, nil) diff --git a/frame/gins/gins_z_unit_view_test.go b/frame/gins/gins_z_unit_view_test.go index d156bb398..a3ea55bb6 100644 --- a/frame/gins/gins_z_unit_view_test.go +++ b/frame/gins/gins_z_unit_view_test.go @@ -52,7 +52,7 @@ func Test_View(t *testing.T) { func Test_View_Config(t *testing.T) { // view1 test1 gtest.Case(t, func() { - dirPath := gfile.Join(gdebug.CallerDirectory(), "testdata", "view1") + dirPath := gfile.Join(gdebug.TestDataPath(), "view1") gcfg.SetContent(gfile.GetContents(gfile.Join(dirPath, "config.toml"))) defer gcfg.ClearContent() defer instances.Clear() @@ -74,7 +74,7 @@ func Test_View_Config(t *testing.T) { }) // view1 test2 gtest.Case(t, func() { - dirPath := gfile.Join(gdebug.CallerDirectory(), "testdata", "view1") + dirPath := gfile.Join(gdebug.TestDataPath(), "view1") gcfg.SetContent(gfile.GetContents(gfile.Join(dirPath, "config.toml"))) defer gcfg.ClearContent() defer instances.Clear() @@ -96,7 +96,7 @@ func Test_View_Config(t *testing.T) { }) // view2 gtest.Case(t, func() { - dirPath := gfile.Join(gdebug.CallerDirectory(), "testdata", "view2") + dirPath := gfile.Join(gdebug.TestDataPath(), "view2") gcfg.SetContent(gfile.GetContents(gfile.Join(dirPath, "config.toml"))) defer gcfg.ClearContent() defer instances.Clear() @@ -118,7 +118,7 @@ func Test_View_Config(t *testing.T) { }) // view2 gtest.Case(t, func() { - dirPath := gfile.Join(gdebug.CallerDirectory(), "testdata", "view2") + dirPath := gfile.Join(gdebug.TestDataPath(), "view2") gcfg.SetContent(gfile.GetContents(gfile.Join(dirPath, "config.toml"))) defer gcfg.ClearContent() defer instances.Clear() diff --git a/frame/gins/testdata/config/config.toml b/frame/gins/testdata/config/config.toml new file mode 100644 index 000000000..7d6ad4b03 --- /dev/null +++ b/frame/gins/testdata/config/config.toml @@ -0,0 +1,29 @@ +# 模板引擎目录 +viewpath = "/home/www/templates/" +test = "v=1" +# MySQL数据库配置 +[database] + [[database.default]] + host = "127.0.0.1" + port = "3306" + user = "root" + pass = "" + name = "test" + type = "mysql" + role = "master" + charset = "utf8" + priority = "1" + [[database.default]] + host = "127.0.0.1" + port = "3306" + user = "root" + pass = "8692651" + name = "test" + type = "mysql" + role = "master" + charset = "utf8" + priority = "1" +# Redis数据库配置 +[redis] + disk = "127.0.0.1:6379,0" + cache = "127.0.0.1:6379,1" \ No newline at end of file diff --git a/frame/gins/testdata/database/config.toml b/frame/gins/testdata/database/config.toml new file mode 100644 index 000000000..d2b72adfb --- /dev/null +++ b/frame/gins/testdata/database/config.toml @@ -0,0 +1,29 @@ +# 模板引擎目录 +viewpath = "/home/www/templates/" +test = "v=2" +# MySQL数据库配置 +[database] + [[database.default]] + host = "127.0.0.1" + port = "3306" + user = "root" + pass = "12345678" + name = "test" + type = "mysql" + role = "master" + weight = "1" + charset = "utf8" + [[database.test]] + host = "127.0.0.1" + port = "3306" + user = "root" + pass = "12345678" + name = "test" + type = "mysql" + role = "master" + weight = "1" + charset = "utf8" +# Redis数据库配置 +[redis] + default = "127.0.0.1:6379,0" + cache = "127.0.0.1:6379,1" \ No newline at end of file diff --git a/frame/gins/testdata/redis/config.toml b/frame/gins/testdata/redis/config.toml new file mode 100644 index 000000000..624ba1abb --- /dev/null +++ b/frame/gins/testdata/redis/config.toml @@ -0,0 +1,32 @@ +# 模板引擎目录 +viewpath = "/home/www/templates/" +test = "v=3" +# MySQL数据库配置 +[database] + [[database.default]] + host = "127.0.0.1" + port = "3306" + user = "root" + pass = "" + # pass = "12345678" + name = "test" + type = "mysql" + role = "master" + charset = "utf8" + priority = "1" + [[database.test]] + host = "127.0.0.1" + port = "3306" + user = "root" + pass = "" + # pass = "12345678" + name = "test" + type = "mysql" + role = "master" + charset = "utf8" + priority = "1" +# Redis数据库配置 +[redis] + default = "127.0.0.1:6379,7" + cache = "127.0.0.1:6379,8" + disk = "127.0.0.1:6379,9,?maxIdle=1&maxActive=10&idleTimeout=10&maxConnLifetime=10" \ No newline at end of file diff --git a/net/ghttp/ghttp_unit_middleware_basic_test.go b/net/ghttp/ghttp_unit_middleware_basic_test.go index 0ff2c4beb..aa4eec40e 100644 --- a/net/ghttp/ghttp_unit_middleware_basic_test.go +++ b/net/ghttp/ghttp_unit_middleware_basic_test.go @@ -178,7 +178,7 @@ func Test_Middleware_With_Static(t *testing.T) { }) s.SetPort(p) s.SetDumpRouterMap(false) - s.SetServerRoot(gfile.Join(gdebug.CallerDirectory(), "testdata", "static1")) + s.SetServerRoot(gfile.Join(gdebug.TestDataPath(), "static1")) s.Start() defer s.Shutdown() time.Sleep(100 * time.Millisecond) @@ -250,7 +250,7 @@ func Test_Middleware_Hook_With_Static(t *testing.T) { }) s.SetPort(p) //s.SetDumpRouterMap(false) - s.SetServerRoot(gfile.Join(gdebug.CallerDirectory(), "testdata", "static1")) + s.SetServerRoot(gfile.Join(gdebug.TestDataPath(), "static1")) s.Start() defer s.Shutdown() time.Sleep(100 * time.Millisecond) diff --git a/net/ghttp/ghttp_unit_param_file_test.go b/net/ghttp/ghttp_unit_param_file_test.go index 357eb3e4c..de0dd51ba 100644 --- a/net/ghttp/ghttp_unit_param_file_test.go +++ b/net/ghttp/ghttp_unit_param_file_test.go @@ -45,7 +45,7 @@ func Test_Params_File_Single(t *testing.T) { client := ghttp.NewClient() client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) - srcPath := gfile.Join(gdebug.CallerDirectory(), "testdata", "upload", "file1.txt") + srcPath := gfile.Join(gdebug.TestDataPath(), "upload", "file1.txt") dstPath := gfile.Join(dstDirPath, "file1.txt") content := client.PostContent("/upload/single", g.Map{ "file": "@file:" + srcPath, @@ -61,7 +61,7 @@ func Test_Params_File_Single(t *testing.T) { client := ghttp.NewClient() client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) - srcPath := gfile.Join(gdebug.CallerDirectory(), "testdata", "upload", "file2.txt") + srcPath := gfile.Join(gdebug.TestDataPath(), "upload", "file2.txt") content := client.PostContent("/upload/single", g.Map{ "file": "@file:" + srcPath, "randomlyRename": true, @@ -98,7 +98,7 @@ func Test_Params_File_CustomName(t *testing.T) { client := ghttp.NewClient() client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) - srcPath := gfile.Join(gdebug.CallerDirectory(), "testdata", "upload", "file1.txt") + srcPath := gfile.Join(gdebug.TestDataPath(), "upload", "file1.txt") dstPath := gfile.Join(dstDirPath, "my.txt") content := client.PostContent("/upload/single", g.Map{ "file": "@file:" + srcPath, @@ -135,8 +135,8 @@ func Test_Params_File_Batch(t *testing.T) { client := ghttp.NewClient() client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) - srcPath1 := gfile.Join(gdebug.CallerDirectory(), "testdata", "upload", "file1.txt") - srcPath2 := gfile.Join(gdebug.CallerDirectory(), "testdata", "upload", "file2.txt") + srcPath1 := gfile.Join(gdebug.TestDataPath(), "upload", "file1.txt") + srcPath2 := gfile.Join(gdebug.TestDataPath(), "upload", "file2.txt") dstPath1 := gfile.Join(dstDirPath, "file1.txt") dstPath2 := gfile.Join(dstDirPath, "file2.txt") content := client.PostContent("/upload/batch", g.Map{ @@ -155,8 +155,8 @@ func Test_Params_File_Batch(t *testing.T) { client := ghttp.NewClient() client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) - srcPath1 := gfile.Join(gdebug.CallerDirectory(), "testdata", "upload", "file1.txt") - srcPath2 := gfile.Join(gdebug.CallerDirectory(), "testdata", "upload", "file2.txt") + srcPath1 := gfile.Join(gdebug.TestDataPath(), "upload", "file1.txt") + srcPath2 := gfile.Join(gdebug.TestDataPath(), "upload", "file2.txt") content := client.PostContent("/upload/batch", g.Map{ "file[0]": "@file:" + srcPath1, "file[1]": "@file:" + srcPath2, diff --git a/net/ghttp/ghttp_unit_static_test.go b/net/ghttp/ghttp_unit_static_test.go index 1ca72d502..361c82959 100644 --- a/net/ghttp/ghttp_unit_static_test.go +++ b/net/ghttp/ghttp_unit_static_test.go @@ -66,7 +66,7 @@ func Test_Static_ServerRoot_Security(t *testing.T) { gtest.Case(t, func() { p := ports.PopRand() s := g.Server(p) - s.SetServerRoot(gfile.Join(gdebug.CallerDirectory(), "testdata", "static1")) + s.SetServerRoot(gfile.Join(gdebug.TestDataPath(), "static1")) s.SetPort(p) s.Start() defer s.Shutdown() diff --git a/net/ghttp/ghttp_unit_template_test.go b/net/ghttp/ghttp_unit_template_test.go index bf2d1e956..c42609a94 100644 --- a/net/ghttp/ghttp_unit_template_test.go +++ b/net/ghttp/ghttp_unit_template_test.go @@ -24,7 +24,7 @@ import ( func Test_Template_Layout1(t *testing.T) { gtest.Case(t, func() { - v := gview.New(gfile.Join(gdebug.CallerDirectory(), "testdata", "template", "layout1")) + v := gview.New(gfile.Join(gdebug.TestDataPath(), "template", "layout1")) p := ports.PopRand() s := g.Server(p) s.SetView(v) @@ -54,7 +54,7 @@ func Test_Template_Layout1(t *testing.T) { func Test_Template_Layout2(t *testing.T) { gtest.Case(t, func() { - v := gview.New(gfile.Join(gdebug.CallerDirectory(), "testdata", "template", "layout2")) + v := gview.New(gfile.Join(gdebug.TestDataPath(), "template", "layout2")) p := ports.PopRand() s := g.Server(p) s.SetView(v) diff --git a/os/gcfg/gcfg_z_unit_test.go b/os/gcfg/gcfg_z_unit_test.go index 08a3582a5..e9b16a645 100644 --- a/os/gcfg/gcfg_z_unit_test.go +++ b/os/gcfg/gcfg_z_unit_test.go @@ -440,7 +440,7 @@ func TestCfg_Instance(t *testing.T) { }) gtest.Case(t, func() { pwd := gfile.Pwd() - gfile.Chdir(gfile.Join(gdebug.CallerDirectory(), "testdata")) + gfile.Chdir(gfile.Join(gdebug.TestDataPath())) defer gfile.Chdir(pwd) gtest.Assert(gcfg.Instance("c1") != nil, true) gtest.Assert(gcfg.Instance("c1").Get("my-config"), "1") diff --git a/os/gfile/gfile_z_time_test.go b/os/gfile/gfile_z_time_test.go index 8e5e7a152..843c1ad1e 100644 --- a/os/gfile/gfile_z_time_test.go +++ b/os/gfile/gfile_z_time_test.go @@ -48,7 +48,10 @@ func Test_MTimeMillisecond(t *testing.T) { gtest.Assert(err, nil) time.Sleep(time.Millisecond * 100) - gtest.AssertGE(gfile.MTimeMillisecond(testpath()+file1), fileobj.ModTime().UnixNano()/1000000) + gtest.AssertGE( + gfile.MTimeMillisecond(testpath()+file1), + fileobj.ModTime().UnixNano()/1000000, + ) gtest.Assert(gfile.MTimeMillisecond(""), 0) }) } diff --git a/os/gview/gview_unit_config_test.go b/os/gview/gview_unit_config_test.go index aac9bf88a..162873454 100644 --- a/os/gview/gview_unit_config_test.go +++ b/os/gview/gview_unit_config_test.go @@ -18,7 +18,7 @@ import ( func Test_Config(t *testing.T) { gtest.Case(t, func() { config := gview.Config{ - Paths: []string{gfile.Join(gdebug.CallerDirectory(), "testdata", "config")}, + Paths: []string{gfile.Join(gdebug.TestDataPath(), "config")}, Data: g.Map{ "name": "gf", }, @@ -45,7 +45,7 @@ func Test_ConfigWithMap(t *testing.T) { gtest.Case(t, func() { view := gview.New() err := view.SetConfigWithMap(g.Map{ - "Paths": []string{gfile.Join(gdebug.CallerDirectory(), "testdata", "config")}, + "Paths": []string{gfile.Join(gdebug.TestDataPath(), "config")}, "DefaultFile": "test.html", "Delimiters": []string{"${", "}"}, "Data": g.Map{ diff --git a/test/gtest/gtest.go b/test/gtest/gtest.go index 489e62240..2ffe21ed2 100644 --- a/test/gtest/gtest.go +++ b/test/gtest/gtest.go @@ -132,16 +132,20 @@ func AssertGE(value, expect interface{}) { passed = gconv.String(value) >= gconv.String(expect) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - passed = gconv.Int(value) >= gconv.Int(expect) + passed = gconv.Int64(value) >= gconv.Int64(expect) case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - passed = gconv.Uint(value) >= gconv.Uint(expect) + passed = gconv.Uint64(value) >= gconv.Uint64(expect) case reflect.Float32, reflect.Float64: passed = gconv.Float64(value) >= gconv.Float64(expect) } if !passed { - panic(fmt.Sprintf(`[ASSERT] EXPECT %v >= %v`, value, expect)) + panic(fmt.Sprintf( + `[ASSERT] EXPECT %v(%v) >= %v(%v)`, + value, reflect.ValueOf(value).Kind(), + expect, reflect.ValueOf(expect).Kind(), + )) } }