diff --git a/g/net/ghttp/ghttp_server_router_group.go b/g/net/ghttp/ghttp_server_router_group.go index ee65be8cf..be919e0d0 100644 --- a/g/net/ghttp/ghttp_server_router_group.go +++ b/g/net/ghttp/ghttp_server_router_group.go @@ -8,10 +8,11 @@ package ghttp import ( - "github.com/gogf/gf/g/os/glog" - "github.com/gogf/gf/g/util/gconv" "reflect" "strings" + + "github.com/gogf/gf/g/os/glog" + "github.com/gogf/gf/g/util/gconv" ) // 分组路由对象 @@ -55,10 +56,16 @@ func (g *RouterGroup) Bind(items []GroupItem) { if strings.EqualFold(gconv.String(item[0]), "REST") { g.bind("REST", gconv.String(item[0])+":"+gconv.String(item[1]), item[2]) } else { - if len(item) > 3 { - g.bind("HANDLER", gconv.String(item[0])+":"+gconv.String(item[1]), item[2], item[3]) + method := gconv.String(item[0]) + if strings.EqualFold(method, "ALL") { + method = "" } else { - g.bind("HANDLER", gconv.String(item[0])+":"+gconv.String(item[1]), item[2]) + method += ":" + } + if len(item) > 3 { + g.bind("HANDLER", method+gconv.String(item[1]), item[2], item[3]) + } else { + g.bind("HANDLER", method+gconv.String(item[1]), item[2]) } } } @@ -126,10 +133,10 @@ func (g *RouterGroup) bind(bindType string, pattern string, object interface{}, if err != nil { glog.Fatalf("invalid pattern: %s", pattern) } - if bindType == "HANDLER" { - pattern = g.server.serveHandlerKey(method, g.prefix+"/"+strings.TrimLeft(path, "/"), domain) - } else { + if bindType == "REST" { pattern = g.prefix + "/" + strings.TrimLeft(path, "/") + } else { + pattern = g.server.serveHandlerKey(method, g.prefix+"/"+strings.TrimLeft(path, "/"), domain) } } methods := gconv.Strings(params) diff --git a/g/net/ghttp/ghttp_server_router_serve.go b/g/net/ghttp/ghttp_server_router_serve.go index fe63ebf2a..9b4180686 100644 --- a/g/net/ghttp/ghttp_server_router_serve.go +++ b/g/net/ghttp/ghttp_server_router_serve.go @@ -9,8 +9,9 @@ package ghttp import ( "container/list" - "github.com/gogf/gf/g/text/gregex" "strings" + + "github.com/gogf/gf/g/text/gregex" ) // 查询请求处理方法. @@ -117,5 +118,8 @@ func (s *Server) searchServeHandler(method, path, domain string) *handlerParsedI // 生成回调方法查询的Key func (s *Server) serveHandlerKey(method, path, domain string) string { + if method == "" { + return path + "@" + strings.ToLower(domain) + } return strings.ToUpper(method) + ":" + path + "@" + strings.ToLower(domain) } diff --git a/g/net/ghttp/ghttp_server_service_controller.go b/g/net/ghttp/ghttp_server_service_controller.go index b8a344416..f50e7d990 100644 --- a/g/net/ghttp/ghttp_server_service_controller.go +++ b/g/net/ghttp/ghttp_server_service_controller.go @@ -9,18 +9,29 @@ package ghttp import ( "fmt" + "reflect" + "strings" + "github.com/gogf/gf/g/os/gfile" "github.com/gogf/gf/g/os/glog" "github.com/gogf/gf/g/text/gregex" "github.com/gogf/gf/g/text/gstr" - "reflect" - "strings" ) // 绑定控制器,控制器需要实现 gmvc.Controller 接口, // 这种方式绑定的控制器每一次请求都会初始化一个新的控制器对象进行处理,对应不同的请求会话, // 第三个参数methods用以指定需要注册的方法,支持多个方法名称,多个方法以英文“,”号分隔,区分大小写. func (s *Server) BindController(pattern string, c Controller, methods ...string) { + // 当pattern中的method为all时,去掉该method,以便于后续方法判断 + domain, method, path, err := s.parsePattern(pattern) + if err != nil { + glog.Error(err) + return + } + if strings.EqualFold(method, gDEFAULT_METHOD) { + pattern = s.serveHandlerKey("", path, domain) + } + methodMap := (map[string]bool)(nil) if len(methods) > 0 { methodMap = make(map[string]bool) diff --git a/g/net/ghttp/ghttp_server_service_object.go b/g/net/ghttp/ghttp_server_service_object.go index 99d230439..cf63368b1 100644 --- a/g/net/ghttp/ghttp_server_service_object.go +++ b/g/net/ghttp/ghttp_server_service_object.go @@ -9,17 +9,28 @@ package ghttp import ( "fmt" + "reflect" + "strings" + "github.com/gogf/gf/g/os/gfile" "github.com/gogf/gf/g/os/glog" "github.com/gogf/gf/g/text/gregex" "github.com/gogf/gf/g/text/gstr" - "reflect" - "strings" ) // 绑定对象到URI请求处理中,会自动识别方法名称,并附加到对应的URI地址后面 // 第三个参数methods用以指定需要注册的方法,支持多个方法名称,多个方法以英文“,”号分隔,区分大小写 func (s *Server) BindObject(pattern string, obj interface{}, methods ...string) { + // 当pattern中的method为all时,去掉该method,以便于后续方法判断 + domain, method, path, err := s.parsePattern(pattern) + if err != nil { + glog.Error(err) + return + } + if strings.EqualFold(method, gDEFAULT_METHOD) { + pattern = s.serveHandlerKey("", path, domain) + } + methodMap := (map[string]bool)(nil) if len(methods) > 0 { methodMap = make(map[string]bool) diff --git a/g/os/gflock/gflock.go b/g/os/gflock/gflock.go index e629720ca..5b0d3669c 100644 --- a/g/os/gflock/gflock.go +++ b/g/os/gflock/gflock.go @@ -41,6 +41,11 @@ func (l *Locker) IsLocked() bool { return l.flock.Locked() } +// IsRLocked returns whether the locker is rlocked. +func (l *Locker) IsRLocked() bool { + return l.flock.RLocked() +} + // TryLock tries get the writing lock of the locker. // It returns true if success, or else returns false immediately. func (l *Locker) TryLock() bool { @@ -82,7 +87,7 @@ func (l *Locker) Lock() (err error) { // Please note, if your shared lock became an exclusive lock this may // unintentionally drop the exclusive lock if called by the consumer that // believes they have a shared lock. Please see Lock() for more details. -func (l *Locker) UnLock() (err error) { +func (l *Locker) Unlock() (err error) { return l.flock.Unlock() } diff --git a/g/os/gflock/gflock_unit_test.go b/g/os/gflock/gflock_unit_test.go new file mode 100644 index 000000000..689f0a921 --- /dev/null +++ b/g/os/gflock/gflock_unit_test.go @@ -0,0 +1,180 @@ +// Copyright 2019 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 gflock_test + +import ( + "testing" + "time" + + "github.com/gogf/gf/g/container/garray" + "github.com/gogf/gf/g/os/gfile" + "github.com/gogf/gf/g/os/gflock" + "github.com/gogf/gf/g/test/gtest" +) + +func Test_GFlock_Base(t *testing.T) { + gtest.Case(t, func() { + fileName := "test" + lock := gflock.New(fileName) + gtest.Assert(lock.Path(), gfile.TempDir()+gfile.Separator+"gflock"+gfile.Separator+fileName) + gtest.Assert(lock.IsLocked(), false) + lock.Lock() + gtest.Assert(lock.IsLocked(), true) + lock.Unlock() + gtest.Assert(lock.IsLocked(), false) + }) + + gtest.Case(t, func() { + fileName := "test" + lock := gflock.New(fileName) + gtest.Assert(lock.Path(), gfile.TempDir()+gfile.Separator+"gflock"+gfile.Separator+fileName) + gtest.Assert(lock.IsRLocked(), false) + lock.RLock() + gtest.Assert(lock.IsRLocked(), true) + lock.RUnlock() + gtest.Assert(lock.IsRLocked(), false) + }) +} + +func Test_GFlock_Lock(t *testing.T) { + gtest.Case(t, func() { + fileName := "testLock" + array := garray.New() + lock := gflock.New(fileName) + lock2 := gflock.New(fileName) + + go func() { + lock.Lock() + array.Append(1) + time.Sleep(300 * time.Millisecond) + lock.Unlock() + }() + + go func() { + time.Sleep(100 * time.Millisecond) + lock2.Lock() + array.Append(1) + lock2.Unlock() + }() + + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 1) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 1) + time.Sleep(200 * time.Millisecond) + gtest.Assert(array.Len(), 2) + }) +} + +func Test_GFlock_RLock(t *testing.T) { + gtest.Case(t, func() { + fileName := "testRLock" + array := garray.New() + lock := gflock.New(fileName) + lock2 := gflock.New(fileName) + + go func() { + lock.RLock() + array.Append(1) + time.Sleep(400 * time.Millisecond) + lock.RUnlock() + }() + + go func() { + time.Sleep(200 * time.Millisecond) + lock2.RLock() + array.Append(1) + lock2.RUnlock() + }() + + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 1) + time.Sleep(200 * time.Millisecond) + gtest.Assert(array.Len(), 2) + }) +} + +func Test_GFlock_TryLock(t *testing.T) { + gtest.Case(t, func() { + fileName := "testTryLock" + array := garray.New() + lock := gflock.New(fileName) + lock2 := gflock.New(fileName) + + go func() { + lock.TryLock() + array.Append(1) + time.Sleep(200 * time.Millisecond) + lock.Unlock() + }() + + go func() { + time.Sleep(100 * time.Millisecond) + if lock2.TryLock() { + array.Append(1) + lock2.Unlock() + } + }() + + go func() { + time.Sleep(300 * time.Millisecond) + if lock2.TryLock() { + array.Append(1) + lock2.Unlock() + } + }() + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 1) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 1) + time.Sleep(200 * time.Millisecond) + gtest.Assert(array.Len(), 2) + }) +} + +func Test_GFlock_TryRLock(t *testing.T) { + gtest.Case(t, func() { + fileName := "testTryRLock" + array := garray.New() + lock := gflock.New(fileName) + lock2 := gflock.New(fileName) + go func() { + lock.TryRLock() + array.Append(1) + time.Sleep(300 * time.Millisecond) + lock.Unlock() + }() + + go func() { + time.Sleep(200 * time.Millisecond) + if lock2.TryRLock() { + array.Append(1) + lock2.Unlock() + } + }() + + go func() { + time.Sleep(200 * time.Millisecond) + if lock2.TryRLock() { + array.Append(1) + lock2.Unlock() + } + }() + + go func() { + time.Sleep(200 * time.Millisecond) + if lock2.TryRLock() { + array.Append(1) + lock2.Unlock() + } + }() + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 1) + time.Sleep(300 * time.Millisecond) + gtest.Assert(array.Len(), 4) + }) +} diff --git a/g/os/gmlock/gmlock_unit_lock_test.go b/g/os/gmlock/gmlock_unit_lock_test.go index e62be3b7d..c9ce524a8 100644 --- a/g/os/gmlock/gmlock_unit_lock_test.go +++ b/g/os/gmlock/gmlock_unit_lock_test.go @@ -16,33 +16,87 @@ import ( ) func Test_Locker_Lock(t *testing.T) { - //no expire gtest.Case(t, func() { key := "testLock" array := garray.New() go func() { gmlock.Lock(key) array.Append(1) - time.Sleep(50 * time.Millisecond) - array.Append(1) + time.Sleep(300 * time.Millisecond) gmlock.Unlock(key) }() go func() { - time.Sleep(10 * time.Millisecond) - gmlock.Lock(key) - array.Append(1) time.Sleep(100 * time.Millisecond) + gmlock.Lock(key) array.Append(1) gmlock.Unlock(key) }() - time.Sleep(10 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(50 * time.Millisecond) - gtest.Assert(array.Len(), 3) - time.Sleep(50 * time.Millisecond) - gtest.Assert(array.Len(), 3) - time.Sleep(50 * time.Millisecond) - gtest.Assert(array.Len(), 4) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 1) + time.Sleep(200 * time.Millisecond) + gtest.Assert(array.Len(), 2) + gmlock.Remove(key) + }) + + gtest.Case(t, func() { + key := "testLock" + array := garray.New() + lock := gmlock.New() + go func() { + lock.Lock(key) + array.Append(1) + time.Sleep(300 * time.Millisecond) + lock.Unlock(key) + }() + go func() { + time.Sleep(100 * time.Millisecond) + lock.Lock(key) + array.Append(1) + lock.Unlock(key) + }() + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 1) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 1) + time.Sleep(200 * time.Millisecond) + gtest.Assert(array.Len(), 2) + lock.Clear() + }) + +} + +func Test_Locker_TryLock(t *testing.T) { + gtest.Case(t, func() { + key := "testTryLock" + array := garray.New() + go func() { + gmlock.Lock(key) + array.Append(1) + time.Sleep(300 * time.Millisecond) + gmlock.Unlock(key) + }() + go func() { + time.Sleep(150 * time.Millisecond) + if gmlock.TryLock(key) { + array.Append(1) + gmlock.Unlock(key) + } + }() + go func() { + time.Sleep(400 * time.Millisecond) + if gmlock.TryLock(key) { + array.Append(1) + gmlock.Unlock(key) + } + }() + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 1) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 1) + time.Sleep(300 * time.Millisecond) + gtest.Assert(array.Len(), 2) }) } @@ -110,20 +164,20 @@ func Test_Locker_LockFunc(t *testing.T) { go func() { gmlock.LockFunc(key, func() { array.Append(1) - time.Sleep(50 * time.Millisecond) + time.Sleep(300 * time.Millisecond) }) // }() go func() { - time.Sleep(10 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gmlock.LockFunc(key, func() { array.Append(1) }) }() - time.Sleep(10 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(20 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gtest.Assert(array.Len(), 1) // - time.Sleep(50 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gtest.Assert(array.Len(), 2) }) @@ -159,24 +213,24 @@ func Test_Locker_TryLockFunc(t *testing.T) { go func() { gmlock.TryLockFunc(key, func() { array.Append(1) - time.Sleep(50 * time.Millisecond) + time.Sleep(200 * time.Millisecond) }) }() go func() { - time.Sleep(10 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gmlock.TryLockFunc(key, func() { array.Append(1) }) }() go func() { - time.Sleep(70 * time.Millisecond) + time.Sleep(300 * time.Millisecond) gmlock.TryLockFunc(key, func() { array.Append(1) }) }() - time.Sleep(50 * time.Millisecond) + time.Sleep(150 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(100 * time.Millisecond) + time.Sleep(400 * time.Millisecond) gtest.Assert(array.Len(), 2) }) //expire1 diff --git a/g/os/gmlock/gmlock_unit_rlock_test.go b/g/os/gmlock/gmlock_unit_rlock_test.go index 1b34e2be4..3b8b299f5 100644 --- a/g/os/gmlock/gmlock_unit_rlock_test.go +++ b/g/os/gmlock/gmlock_unit_rlock_test.go @@ -23,20 +23,19 @@ func Test_Locker_RLock(t *testing.T) { go func() { gmlock.RLock(key) array.Append(1) - time.Sleep(50 * time.Millisecond) - array.Append(1) + time.Sleep(200 * time.Millisecond) gmlock.RUnlock(key) }() go func() { - time.Sleep(10 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gmlock.Lock(key) array.Append(1) gmlock.Unlock(key) }() - time.Sleep(20 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(80 * time.Millisecond) - gtest.Assert(array.Len(), 3) + time.Sleep(200 * time.Millisecond) + gtest.Assert(array.Len(), 2) }) //Lock before RLock @@ -46,18 +45,18 @@ func Test_Locker_RLock(t *testing.T) { go func() { gmlock.Lock(key) array.Append(1) - time.Sleep(50 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gmlock.Unlock(key) }() go func() { - time.Sleep(10 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gmlock.RLock(key) array.Append(1) gmlock.RUnlock(key) }() - time.Sleep(20 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(50 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gtest.Assert(array.Len(), 2) }) @@ -68,26 +67,26 @@ func Test_Locker_RLock(t *testing.T) { go func() { gmlock.Lock(key) array.Append(1) - time.Sleep(50 * time.Millisecond) + time.Sleep(300 * time.Millisecond) gmlock.Unlock(key) }() go func() { - time.Sleep(10 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gmlock.RLock(key) array.Append(1) - time.Sleep(70 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gmlock.RUnlock(key) }() go func() { - time.Sleep(10 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gmlock.RLock(key) array.Append(1) - time.Sleep(70 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gmlock.RUnlock(key) }() - time.Sleep(20 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(120 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gtest.Assert(array.Len(), 3) }) } @@ -100,19 +99,19 @@ func Test_Locker_TryRLock(t *testing.T) { go func() { gmlock.Lock(key) array.Append(1) - time.Sleep(50 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gmlock.Unlock(key) }() go func() { - time.Sleep(10 * time.Millisecond) + time.Sleep(100 * time.Millisecond) if gmlock.TryRLock(key) { array.Append(1) gmlock.RUnlock(key) } }() - time.Sleep(20 * time.Millisecond) + time.Sleep(150 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(50 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gtest.Assert(array.Len(), 1) }) @@ -123,31 +122,31 @@ func Test_Locker_TryRLock(t *testing.T) { go func() { gmlock.Lock(key) array.Append(1) - time.Sleep(50 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gmlock.Unlock(key) }() go func() { - time.Sleep(10 * time.Millisecond) + time.Sleep(100 * time.Millisecond) if gmlock.TryRLock(key) { array.Append(1) gmlock.RUnlock(key) } }() go func() { - time.Sleep(70 * time.Millisecond) + time.Sleep(300 * time.Millisecond) if gmlock.TryRLock(key) { array.Append(1) gmlock.RUnlock(key) } }() - time.Sleep(20 * time.Millisecond) + time.Sleep(150 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(80 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gtest.Assert(array.Len(), 2) }) } -func Test_Locker_RLockFunc1(t *testing.T) { +func Test_Locker_RLockFunc(t *testing.T) { //RLockFunc before Lock gtest.Case(t, func() { key := "testRLockFuncBeforeLock" @@ -155,21 +154,19 @@ func Test_Locker_RLockFunc1(t *testing.T) { go func() { gmlock.RLockFunc(key, func() { array.Append(1) - time.Sleep(500 * time.Millisecond) - array.Append(1) + time.Sleep(200 * time.Millisecond) }) }() go func() { - time.Sleep(10 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gmlock.Lock(key) array.Append(1) gmlock.Unlock(key) }() - - time.Sleep(200 * time.Millisecond) + time.Sleep(150 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(800 * time.Millisecond) - gtest.Assert(array.Len(), 3) + time.Sleep(200 * time.Millisecond) + gtest.Assert(array.Len(), 2) }) //Lock before RLockFunc @@ -179,25 +176,21 @@ func Test_Locker_RLockFunc1(t *testing.T) { go func() { gmlock.Lock(key) array.Append(1) - time.Sleep(50 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gmlock.Unlock(key) }() go func() { - time.Sleep(10 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gmlock.RLockFunc(key, func() { array.Append(1) }) }() - time.Sleep(20 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(50 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gtest.Assert(array.Len(), 2) }) -} - -func Test_Locker_RLockFunc2(t *testing.T) { - //Lock before RLockFuncs gtest.Case(t, func() { key := "testLockBeforeRLockFuncs" @@ -205,29 +198,26 @@ func Test_Locker_RLockFunc2(t *testing.T) { go func() { gmlock.Lock(key) array.Append(1) - //glog.Println("add1") - time.Sleep(500 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gmlock.Unlock(key) }() go func() { time.Sleep(100 * time.Millisecond) gmlock.RLockFunc(key, func() { array.Append(1) - //glog.Println("add2") - time.Sleep(700 * time.Millisecond) + time.Sleep(200 * time.Millisecond) }) }() go func() { time.Sleep(100 * time.Millisecond) gmlock.RLockFunc(key, func() { array.Append(1) - //glog.Println("add3") - time.Sleep(700 * time.Millisecond) + time.Sleep(200 * time.Millisecond) }) }() - time.Sleep(200 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(700 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gtest.Assert(array.Len(), 3) }) } @@ -240,18 +230,18 @@ func Test_Locker_TryRLockFunc(t *testing.T) { go func() { gmlock.Lock(key) array.Append(1) - time.Sleep(50 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gmlock.Unlock(key) }() go func() { - time.Sleep(10 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gmlock.TryRLockFunc(key, func() { array.Append(1) }) }() - time.Sleep(20 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(50 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gtest.Assert(array.Len(), 1) }) @@ -262,24 +252,24 @@ func Test_Locker_TryRLockFunc(t *testing.T) { go func() { gmlock.Lock(key) array.Append(1) - time.Sleep(50 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gmlock.Unlock(key) }() go func() { - time.Sleep(10 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gmlock.TryRLockFunc(key, func() { array.Append(1) }) }() go func() { - time.Sleep(70 * time.Millisecond) + time.Sleep(300 * time.Millisecond) gmlock.TryRLockFunc(key, func() { array.Append(1) }) }() - time.Sleep(20 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(70 * time.Millisecond) + time.Sleep(300 * time.Millisecond) gtest.Assert(array.Len(), 2) }) } diff --git a/g/os/gmutex/gmutex_uinit_test.go b/g/os/gmutex/gmutex_unit_test.go similarity index 65% rename from g/os/gmutex/gmutex_uinit_test.go rename to g/os/gmutex/gmutex_unit_test.go index c3389c507..74d42edb1 100644 --- a/g/os/gmutex/gmutex_uinit_test.go +++ b/g/os/gmutex/gmutex_unit_test.go @@ -21,11 +21,11 @@ func Test_Mutex_RUnlock(t *testing.T) { for index := 0; index < 1000; index++ { go func() { mu.RLockFunc(func() { - time.Sleep(100 * time.Millisecond) + time.Sleep(200 * time.Millisecond) }) }() } - time.Sleep(10 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gtest.Assert(mu.IsRLocked(), true) gtest.Assert(mu.IsLocked(), true) gtest.Assert(mu.IsWLocked(), false) @@ -34,10 +34,28 @@ func Test_Mutex_RUnlock(t *testing.T) { mu.RUnlock() }() } - time.Sleep(150 * time.Millisecond) + time.Sleep(300 * time.Millisecond) gtest.Assert(mu.IsRLocked(), false) }) + + //RLock before Lock + gtest.Case(t, func() { + mu := gmutex.New() + mu.RLock() + go func() { + mu.Lock() + time.Sleep(300 * time.Millisecond) + mu.Unlock() + }() + time.Sleep(100 * time.Millisecond) + mu.RUnlock() + gtest.Assert(mu.IsRLocked(), false) + time.Sleep(100 * time.Millisecond) + gtest.Assert(mu.IsLocked(), true) + time.Sleep(400 * time.Millisecond) + gtest.Assert(mu.IsLocked(), false) + }) } func Test_Mutex_IsLocked(t *testing.T) { @@ -45,27 +63,27 @@ func Test_Mutex_IsLocked(t *testing.T) { mu := gmutex.New() go func() { mu.LockFunc(func() { - time.Sleep(100 * time.Millisecond) + time.Sleep(200 * time.Millisecond) }) }() - time.Sleep(10 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gtest.Assert(mu.IsLocked(), true) gtest.Assert(mu.IsWLocked(), true) gtest.Assert(mu.IsRLocked(), false) - time.Sleep(110 * time.Millisecond) + time.Sleep(300 * time.Millisecond) gtest.Assert(mu.IsLocked(), false) gtest.Assert(mu.IsWLocked(), false) go func() { mu.RLockFunc(func() { - time.Sleep(100 * time.Millisecond) + time.Sleep(200 * time.Millisecond) }) }() - time.Sleep(10 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gtest.Assert(mu.IsRLocked(), true) gtest.Assert(mu.IsLocked(), true) gtest.Assert(mu.IsWLocked(), false) - time.Sleep(110 * time.Millisecond) + time.Sleep(300 * time.Millisecond) gtest.Assert(mu.IsRLocked(), false) }) } @@ -77,34 +95,33 @@ func Test_Mutex_Unlock(t *testing.T) { go func() { mu.LockFunc(func() { array.Append(1) - time.Sleep(100 * time.Millisecond) + time.Sleep(300 * time.Millisecond) }) }() go func() { - time.Sleep(50 * time.Millisecond) + time.Sleep(100 * time.Millisecond) mu.LockFunc(func() { array.Append(1) }) }() go func() { - time.Sleep(50 * time.Millisecond) + time.Sleep(100 * time.Millisecond) mu.LockFunc(func() { array.Append(1) }) }() go func() { - time.Sleep(60 * time.Millisecond) + time.Sleep(200 * time.Millisecond) + mu.Unlock() mu.Unlock() mu.Unlock() mu.Unlock() }() - time.Sleep(20 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(50 * time.Millisecond) - gtest.Assert(array.Len(), 3) - time.Sleep(50 * time.Millisecond) + time.Sleep(400 * time.Millisecond) gtest.Assert(array.Len(), 3) }) } @@ -116,20 +133,20 @@ func Test_Mutex_LockFunc(t *testing.T) { go func() { mu.LockFunc(func() { array.Append(1) - time.Sleep(100 * time.Millisecond) + time.Sleep(300 * time.Millisecond) }) }() go func() { - time.Sleep(50 * time.Millisecond) + time.Sleep(100 * time.Millisecond) mu.LockFunc(func() { array.Append(1) }) }() - time.Sleep(20 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(50 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(50 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gtest.Assert(array.Len(), 2) }) } @@ -141,26 +158,26 @@ func Test_Mutex_TryLockFunc(t *testing.T) { go func() { mu.LockFunc(func() { array.Append(1) - time.Sleep(100 * time.Millisecond) + time.Sleep(300 * time.Millisecond) }) }() go func() { - time.Sleep(50 * time.Millisecond) + time.Sleep(100 * time.Millisecond) mu.TryLockFunc(func() { array.Append(1) }) }() go func() { - time.Sleep(110 * time.Millisecond) + time.Sleep(400 * time.Millisecond) mu.TryLockFunc(func() { array.Append(1) }) }() - time.Sleep(20 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(50 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(50 * time.Millisecond) + time.Sleep(300 * time.Millisecond) gtest.Assert(array.Len(), 2) }) } @@ -172,21 +189,21 @@ func Test_Mutex_RLockFunc(t *testing.T) { go func() { mu.LockFunc(func() { array.Append(1) - time.Sleep(100 * time.Millisecond) + time.Sleep(300 * time.Millisecond) }) }() go func() { - time.Sleep(50 * time.Millisecond) + time.Sleep(100 * time.Millisecond) mu.RLockFunc(func() { array.Append(1) time.Sleep(100 * time.Millisecond) }) }() - time.Sleep(20 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(50 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(50 * time.Millisecond) + time.Sleep(300 * time.Millisecond) gtest.Assert(array.Len(), 2) }) @@ -194,28 +211,28 @@ func Test_Mutex_RLockFunc(t *testing.T) { mu := gmutex.New() array := garray.New() go func() { - time.Sleep(50 * time.Millisecond) + time.Sleep(100 * time.Millisecond) mu.RLockFunc(func() { array.Append(1) time.Sleep(100 * time.Millisecond) }) }() go func() { - time.Sleep(50 * time.Millisecond) + time.Sleep(100 * time.Millisecond) mu.RLockFunc(func() { array.Append(1) time.Sleep(100 * time.Millisecond) }) }() go func() { - time.Sleep(50 * time.Millisecond) + time.Sleep(100 * time.Millisecond) mu.RLockFunc(func() { array.Append(1) time.Sleep(100 * time.Millisecond) }) }() gtest.Assert(array.Len(), 0) - time.Sleep(80 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gtest.Assert(array.Len(), 3) }) } @@ -227,32 +244,28 @@ func Test_Mutex_TryRLockFunc(t *testing.T) { go func() { mu.LockFunc(func() { array.Append(1) - time.Sleep(500 * time.Millisecond) + time.Sleep(300 * time.Millisecond) }) }() go func() { - time.Sleep(200 * time.Millisecond) - mu.TryRLockFunc(func() { - array.Append(1) - }) - }() - go func() { - time.Sleep(700 * time.Millisecond) - mu.TryRLockFunc(func() { - array.Append(1) - }) - }() - go func() { - time.Sleep(700 * time.Millisecond) + time.Sleep(100 * time.Millisecond) mu.TryRLockFunc(func() { array.Append(1) }) }() + for index := 0; index < 1000; index++ { + go func() { + time.Sleep(400 * time.Millisecond) + mu.TryRLockFunc(func() { + array.Append(1) + }) + }() + } time.Sleep(100 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(500 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(500 * time.Millisecond) - gtest.Assert(array.Len(), 3) + time.Sleep(600 * time.Millisecond) + gtest.Assert(array.Len(), 1001) }) } diff --git a/g/os/grpool/grpool_unit_test.go b/g/os/grpool/grpool_unit_test.go index 372ea03a8..90ce424bd 100644 --- a/g/os/grpool/grpool_unit_test.go +++ b/g/os/grpool/grpool_unit_test.go @@ -9,12 +9,13 @@ package grpool_test import ( - "github.com/gogf/gf/g/container/garray" - "github.com/gogf/gf/g/os/grpool" - "github.com/gogf/gf/g/test/gtest" "sync" "testing" "time" + + "github.com/gogf/gf/g/container/garray" + "github.com/gogf/gf/g/os/grpool" + "github.com/gogf/gf/g/test/gtest" ) func Test_Basic(t *testing.T) { @@ -30,7 +31,10 @@ func Test_Basic(t *testing.T) { }) } wg.Wait() + time.Sleep(100 * time.Millisecond) gtest.Assert(array.Len(), size) + gtest.Assert(grpool.Jobs(), 0) + gtest.Assert(grpool.Size(), 0) }) } @@ -75,6 +79,7 @@ func Test_Limit3(t *testing.T) { array := garray.NewArray() size := 1000 pool := grpool.New(100) + gtest.Assert(pool.Cap(), 100) for i := 0; i < size; i++ { pool.Add(func() { array.Append(1) @@ -90,5 +95,8 @@ func Test_Limit3(t *testing.T) { gtest.Assert(pool.Size(), 0) gtest.Assert(pool.Jobs(), 900) gtest.Assert(array.Len(), 100) + gtest.Assert(pool.IsClosed(), true) + gtest.AssertNE(pool.Add(func() {}), nil) + }) } diff --git a/g/util/gconv/gconv_z_all_test.go b/g/util/gconv/gconv_z_all_test.go index c47eed1e0..d0a92d3f1 100644 --- a/g/util/gconv/gconv_z_all_test.go +++ b/g/util/gconv/gconv_z_all_test.go @@ -48,10 +48,10 @@ func Test_Bool_All(t *testing.T) { var countryCapitalMap = make(map[string]string) /* map插入key - value对,各个国家对应的首都 */ - countryCapitalMap [ "France" ] = "巴黎" - countryCapitalMap [ "Italy" ] = "罗马" - countryCapitalMap [ "Japan" ] = "东京" - countryCapitalMap [ "India " ] = "新德里" + countryCapitalMap["France"] = "巴黎" + countryCapitalMap["Italy"] = "罗马" + countryCapitalMap["Japan"] = "东京" + countryCapitalMap["India "] = "新德里" gtest.AssertEQ(gconv.Bool(countryCapitalMap), true) gtest.AssertEQ(gconv.Bool("1"), true) @@ -83,10 +83,10 @@ func Test_Int_All(t *testing.T) { var countryCapitalMap = make(map[string]string) /* map插入key - value对,各个国家对应的首都 */ - countryCapitalMap [ "France" ] = "巴黎" - countryCapitalMap [ "Italy" ] = "罗马" - countryCapitalMap [ "Japan" ] = "东京" - countryCapitalMap [ "India " ] = "新德里" + countryCapitalMap["France"] = "巴黎" + countryCapitalMap["Italy"] = "罗马" + countryCapitalMap["Japan"] = "东京" + countryCapitalMap["India "] = "新德里" gtest.AssertEQ(gconv.Int(countryCapitalMap), 0) gtest.AssertEQ(gconv.Int("1"), 1) @@ -117,10 +117,10 @@ func Test_Int8_All(t *testing.T) { var countryCapitalMap = make(map[string]string) /* map插入key - value对,各个国家对应的首都 */ - countryCapitalMap [ "France" ] = "巴黎" - countryCapitalMap [ "Italy" ] = "罗马" - countryCapitalMap [ "Japan" ] = "东京" - countryCapitalMap [ "India " ] = "新德里" + countryCapitalMap["France"] = "巴黎" + countryCapitalMap["Italy"] = "罗马" + countryCapitalMap["Japan"] = "东京" + countryCapitalMap["India "] = "新德里" gtest.AssertEQ(gconv.Int8(countryCapitalMap), int8(0)) gtest.AssertEQ(gconv.Int8("1"), int8(1)) @@ -151,10 +151,10 @@ func Test_Int16_All(t *testing.T) { var countryCapitalMap = make(map[string]string) /* map插入key - value对,各个国家对应的首都 */ - countryCapitalMap [ "France" ] = "巴黎" - countryCapitalMap [ "Italy" ] = "罗马" - countryCapitalMap [ "Japan" ] = "东京" - countryCapitalMap [ "India " ] = "新德里" + countryCapitalMap["France"] = "巴黎" + countryCapitalMap["Italy"] = "罗马" + countryCapitalMap["Japan"] = "东京" + countryCapitalMap["India "] = "新德里" gtest.AssertEQ(gconv.Int16(countryCapitalMap), int16(0)) gtest.AssertEQ(gconv.Int16("1"), int16(1)) @@ -185,10 +185,10 @@ func Test_Int32_All(t *testing.T) { var countryCapitalMap = make(map[string]string) /* map插入key - value对,各个国家对应的首都 */ - countryCapitalMap [ "France" ] = "巴黎" - countryCapitalMap [ "Italy" ] = "罗马" - countryCapitalMap [ "Japan" ] = "东京" - countryCapitalMap [ "India " ] = "新德里" + countryCapitalMap["France"] = "巴黎" + countryCapitalMap["Italy"] = "罗马" + countryCapitalMap["Japan"] = "东京" + countryCapitalMap["India "] = "新德里" gtest.AssertEQ(gconv.Int32(countryCapitalMap), int32(0)) gtest.AssertEQ(gconv.Int32("1"), int32(1)) @@ -239,10 +239,10 @@ func Test_Int64_All(t *testing.T) { var countryCapitalMap = make(map[string]string) /* map插入key - value对,各个国家对应的首都 */ - countryCapitalMap [ "France" ] = "巴黎" - countryCapitalMap [ "Italy" ] = "罗马" - countryCapitalMap [ "Japan" ] = "东京" - countryCapitalMap [ "India " ] = "新德里" + countryCapitalMap["France"] = "巴黎" + countryCapitalMap["Italy"] = "罗马" + countryCapitalMap["Japan"] = "东京" + countryCapitalMap["India "] = "新德里" gtest.AssertEQ(gconv.Int64(countryCapitalMap), int64(0)) gtest.AssertEQ(gconv.Int64("1"), int64(1)) @@ -274,10 +274,10 @@ func Test_Uint_All(t *testing.T) { var countryCapitalMap = make(map[string]string) /* map插入key - value对,各个国家对应的首都 */ - countryCapitalMap [ "France" ] = "巴黎" - countryCapitalMap [ "Italy" ] = "罗马" - countryCapitalMap [ "Japan" ] = "东京" - countryCapitalMap [ "India " ] = "新德里" + countryCapitalMap["France"] = "巴黎" + countryCapitalMap["Italy"] = "罗马" + countryCapitalMap["Japan"] = "东京" + countryCapitalMap["India "] = "新德里" gtest.AssertEQ(gconv.Uint(countryCapitalMap), uint(0)) gtest.AssertEQ(gconv.Uint("1"), uint(1)) @@ -309,10 +309,10 @@ func Test_Uint8_All(t *testing.T) { var countryCapitalMap = make(map[string]string) /* map插入key - value对,各个国家对应的首都 */ - countryCapitalMap [ "France" ] = "巴黎" - countryCapitalMap [ "Italy" ] = "罗马" - countryCapitalMap [ "Japan" ] = "东京" - countryCapitalMap [ "India " ] = "新德里" + countryCapitalMap["France"] = "巴黎" + countryCapitalMap["Italy"] = "罗马" + countryCapitalMap["Japan"] = "东京" + countryCapitalMap["India "] = "新德里" gtest.AssertEQ(gconv.Uint8(countryCapitalMap), uint8(0)) gtest.AssertEQ(gconv.Uint8("1"), uint8(1)) @@ -344,10 +344,10 @@ func Test_Uint16_All(t *testing.T) { var countryCapitalMap = make(map[string]string) /* map插入key - value对,各个国家对应的首都 */ - countryCapitalMap [ "France" ] = "巴黎" - countryCapitalMap [ "Italy" ] = "罗马" - countryCapitalMap [ "Japan" ] = "东京" - countryCapitalMap [ "India " ] = "新德里" + countryCapitalMap["France"] = "巴黎" + countryCapitalMap["Italy"] = "罗马" + countryCapitalMap["Japan"] = "东京" + countryCapitalMap["India "] = "新德里" gtest.AssertEQ(gconv.Uint16(countryCapitalMap), uint16(0)) gtest.AssertEQ(gconv.Uint16("1"), uint16(1)) @@ -379,10 +379,10 @@ func Test_Uint32_All(t *testing.T) { var countryCapitalMap = make(map[string]string) /* map插入key - value对,各个国家对应的首都 */ - countryCapitalMap [ "France" ] = "巴黎" - countryCapitalMap [ "Italy" ] = "罗马" - countryCapitalMap [ "Japan" ] = "东京" - countryCapitalMap [ "India " ] = "新德里" + countryCapitalMap["France"] = "巴黎" + countryCapitalMap["Italy"] = "罗马" + countryCapitalMap["Japan"] = "东京" + countryCapitalMap["India "] = "新德里" gtest.AssertEQ(gconv.Uint32(countryCapitalMap), uint32(0)) gtest.AssertEQ(gconv.Uint32("1"), uint32(1)) @@ -433,10 +433,10 @@ func Test_Uint64_All(t *testing.T) { var countryCapitalMap = make(map[string]string) /* map插入key - value对,各个国家对应的首都 */ - countryCapitalMap [ "France" ] = "巴黎" - countryCapitalMap [ "Italy" ] = "罗马" - countryCapitalMap [ "Japan" ] = "东京" - countryCapitalMap [ "India " ] = "新德里" + countryCapitalMap["France"] = "巴黎" + countryCapitalMap["Italy"] = "罗马" + countryCapitalMap["Japan"] = "东京" + countryCapitalMap["India "] = "新德里" gtest.AssertEQ(gconv.Uint64(countryCapitalMap), uint64(0)) gtest.AssertEQ(gconv.Uint64("1"), uint64(1)) @@ -467,10 +467,10 @@ func Test_Float32_All(t *testing.T) { var countryCapitalMap = make(map[string]string) /* map插入key - value对,各个国家对应的首都 */ - countryCapitalMap [ "France" ] = "巴黎" - countryCapitalMap [ "Italy" ] = "罗马" - countryCapitalMap [ "Japan" ] = "东京" - countryCapitalMap [ "India " ] = "新德里" + countryCapitalMap["France"] = "巴黎" + countryCapitalMap["Italy"] = "罗马" + countryCapitalMap["Japan"] = "东京" + countryCapitalMap["India "] = "新德里" gtest.AssertEQ(gconv.Float32(countryCapitalMap), float32(0)) gtest.AssertEQ(gconv.Float32("1"), float32(1)) @@ -501,10 +501,10 @@ func Test_Float64_All(t *testing.T) { var countryCapitalMap = make(map[string]string) /* map插入key - value对,各个国家对应的首都 */ - countryCapitalMap [ "France" ] = "巴黎" - countryCapitalMap [ "Italy" ] = "罗马" - countryCapitalMap [ "Japan" ] = "东京" - countryCapitalMap [ "India " ] = "新德里" + countryCapitalMap["France"] = "巴黎" + countryCapitalMap["Italy"] = "罗马" + countryCapitalMap["Japan"] = "东京" + countryCapitalMap["India "] = "新德里" gtest.AssertEQ(gconv.Float64(countryCapitalMap), float64(0)) gtest.AssertEQ(gconv.Float64("1"), float64(1)) @@ -553,10 +553,10 @@ func Test_String_All(t *testing.T) { var countryCapitalMap = make(map[string]string) /* map插入key - value对,各个国家对应的首都 */ - countryCapitalMap [ "France" ] = "巴黎" - countryCapitalMap [ "Italy" ] = "罗马" - countryCapitalMap [ "Japan" ] = "东京" - countryCapitalMap [ "India " ] = "新德里" + countryCapitalMap["France"] = "巴黎" + countryCapitalMap["Italy"] = "罗马" + countryCapitalMap["Japan"] = "东京" + countryCapitalMap["India "] = "新德里" gtest.AssertEQ(gconv.String(countryCapitalMap), `{"France":"巴黎","India ":"新德里","Italy":"罗马","Japan":"东京"}`) gtest.AssertEQ(gconv.String(int64(1)), "1") gtest.AssertEQ(gconv.String(123.456), "123.456") @@ -731,7 +731,7 @@ func Test_Slice_All(t *testing.T) { gtest.AssertEQ(gconv.Interfaces(slices), []interface{}{1}) gtest.AssertEQ(gconv.Maps(nil), nil) - gtest.AssertEQ(gconv.Maps([]map[string]interface{}{{"a": "1"},}), []map[string]interface{}{{"a": "1"},}) + gtest.AssertEQ(gconv.Maps([]map[string]interface{}{{"a": "1"}}), []map[string]interface{}{{"a": "1"}}) gtest.AssertEQ(gconv.Maps(1223), []map[string]interface{}{{}}) gtest.AssertEQ(gconv.Maps([]int{}), nil) }) @@ -965,8 +965,8 @@ func Test_Struct_Basic1_All(t *testing.T) { "PASS1": "123", "PASS2": "456", "As": g.Map{"Name": 1, "Result": "22222"}, - "Ass": &Score{11,"11"}, - "Assb": []string{"wwww"}, + "Ass": &Score{11, "11"}, + "Assb": []string{"wwww"}, } _ = gconv.Struct(nil, user) _ = gconv.Struct(params1, nil) diff --git a/g/util/gutil/gutil_comparator_z_unit_test.go b/g/util/gutil/gutil_comparator_z_unit_test.go new file mode 100755 index 000000000..82b556a68 --- /dev/null +++ b/g/util/gutil/gutil_comparator_z_unit_test.go @@ -0,0 +1,156 @@ +package gutil_test + +import ( + "testing" + + "github.com/gogf/gf/g/test/gtest" + "github.com/gogf/gf/g/util/gutil" +) + +func Test_ComparatorString(t *testing.T) { + + gtest.Case(t, func() { + gtest.Assert(gutil.ComparatorString(1, 1), 0) + gtest.Assert(gutil.ComparatorString(1, 2), -1) + gtest.Assert(gutil.ComparatorString(2, 1), 1) + }) +} + +func Test_ComparatorInt(t *testing.T) { + + gtest.Case(t, func() { + gtest.Assert(gutil.ComparatorInt(1, 1), 0) + gtest.Assert(gutil.ComparatorInt(1, 2), -1) + gtest.Assert(gutil.ComparatorInt(2, 1), 1) + }) +} + +func Test_ComparatorInt8(t *testing.T) { + + gtest.Case(t, func() { + gtest.Assert(gutil.ComparatorInt8(1, 1), 0) + gtest.Assert(gutil.ComparatorInt8(1, 2), -1) + gtest.Assert(gutil.ComparatorInt8(2, 1), 1) + }) +} + +func Test_ComparatorInt16(t *testing.T) { + + gtest.Case(t, func() { + gtest.Assert(gutil.ComparatorInt16(1, 1), 0) + gtest.Assert(gutil.ComparatorInt16(1, 2), -1) + gtest.Assert(gutil.ComparatorInt16(2, 1), 1) + }) +} + +func Test_ComparatorInt32(t *testing.T) { + + gtest.Case(t, func() { + gtest.Assert(gutil.ComparatorInt32(1, 1), 0) + gtest.Assert(gutil.ComparatorInt32(1, 2), -1) + gtest.Assert(gutil.ComparatorInt32(2, 1), 1) + }) +} + +func Test_ComparatorInt64(t *testing.T) { + + gtest.Case(t, func() { + gtest.Assert(gutil.ComparatorInt64(1, 1), 0) + gtest.Assert(gutil.ComparatorInt64(1, 2), -1) + gtest.Assert(gutil.ComparatorInt64(2, 1), 1) + }) +} + +func Test_ComparatorUint(t *testing.T) { + + gtest.Case(t, func() { + gtest.Assert(gutil.ComparatorUint(1, 1), 0) + gtest.Assert(gutil.ComparatorUint(1, 2), -1) + gtest.Assert(gutil.ComparatorUint(2, 1), 1) + }) +} + +func Test_ComparatorUint8(t *testing.T) { + + gtest.Case(t, func() { + gtest.Assert(gutil.ComparatorUint8(1, 1), 0) + gtest.Assert(gutil.ComparatorUint8(2, 6), 252) + gtest.Assert(gutil.ComparatorUint8(2, 1), 1) + }) +} + +func Test_ComparatorUint16(t *testing.T) { + + gtest.Case(t, func() { + gtest.Assert(gutil.ComparatorUint16(1, 1), 0) + gtest.Assert(gutil.ComparatorUint16(1, 2), 65535) + gtest.Assert(gutil.ComparatorUint16(2, 1), 1) + }) +} + +func Test_ComparatorUint32(t *testing.T) { + + gtest.Case(t, func() { + gtest.Assert(gutil.ComparatorUint32(1, 1), 0) + gtest.Assert(gutil.ComparatorUint32(-1000, 2147483640), 2147482656) + gtest.Assert(gutil.ComparatorUint32(2, 1), 1) + }) +} + +func Test_ComparatorUint64(t *testing.T) { + + gtest.Case(t, func() { + gtest.Assert(gutil.ComparatorUint64(1, 1), 0) + gtest.Assert(gutil.ComparatorUint64(1, 2), -1) + gtest.Assert(gutil.ComparatorUint64(2, 1), 1) + }) +} + +func Test_ComparatorFloat32(t *testing.T) { + + gtest.Case(t, func() { + gtest.Assert(gutil.ComparatorFloat32(1, 1), 0) + gtest.Assert(gutil.ComparatorFloat32(1, 2), -1) + gtest.Assert(gutil.ComparatorFloat32(2, 1), 1) + }) +} + +func Test_ComparatorFloat64(t *testing.T) { + + gtest.Case(t, func() { + gtest.Assert(gutil.ComparatorFloat64(1, 1), 0) + gtest.Assert(gutil.ComparatorFloat64(1, 2), -1) + gtest.Assert(gutil.ComparatorFloat64(2, 1), 1) + }) +} + +func Test_ComparatorByte(t *testing.T) { + + gtest.Case(t, func() { + gtest.Assert(gutil.ComparatorByte(1, 1), 0) + gtest.Assert(gutil.ComparatorByte(1, 2), 255) + gtest.Assert(gutil.ComparatorByte(2, 1), 1) + }) +} + +func Test_ComparatorRune(t *testing.T) { + + gtest.Case(t, func() { + gtest.Assert(gutil.ComparatorRune(1, 1), 0) + gtest.Assert(gutil.ComparatorRune(1, 2), -1) + gtest.Assert(gutil.ComparatorRune(2, 1), 1) + }) +} + +func Test_ComparatorTime(t *testing.T) { + gtest.Case(t, func() { + j := gutil.ComparatorTime("2019-06-14", "2019-06-14") + gtest.Assert(j, 0) + + k := gutil.ComparatorTime("2019-06-15", "2019-06-14") + gtest.Assert(k, 1) + + l := gutil.ComparatorTime("2019-06-13", "2019-06-14") + gtest.Assert(l, -1) + }) +} diff --git a/g/util/gutil/gutil_z_unit_test.go b/g/util/gutil/gutil_z_unit_test.go new file mode 100755 index 000000000..cf52edc2a --- /dev/null +++ b/g/util/gutil/gutil_z_unit_test.go @@ -0,0 +1,66 @@ +package gutil_test + +import ( + "testing" + + "github.com/gogf/gf/g/test/gtest" + "github.com/gogf/gf/g/util/gutil" +) + +func Test_Dump(t *testing.T) { + gtest.Case(t, func() { + gutil.Dump(map[int]int{ + 100: 100, + }) + }) + + gtest.Case(t, func() { + gutil.Dump(map[string]interface{}{"": func() {}}) + }) + + gtest.Case(t, func() { + gutil.Dump([]byte("gutil Dump test")) + }) +} + +func Test_PrintBacktrace(t *testing.T) { + gtest.Case(t, func() { + gutil.PrintBacktrace() + }) +} + +func Test_TryCatch(t *testing.T) { + + gtest.Case(t, func() { + gutil.TryCatch(func() { + panic("gutil TryCatch test") + }) + }) + + gtest.Case(t, func() { + gutil.TryCatch(func() { + panic("gutil TryCatch test") + + }, func(err interface{}) { + gtest.Assert(err, "gutil TryCatch test") + }) + }) +} + +func Test_IsEmpty(t *testing.T) { + + gtest.Case(t, func() { + gtest.Assert(gutil.IsEmpty(1), false) + }) +} + +func Test_Throw(t *testing.T) { + + gtest.Case(t, func() { + defer func() { + gtest.Assert(recover(), "gutil Throw test") + }() + + gutil.Throw("gutil Throw test") + }) +} diff --git a/geg/net/scanner.go b/geg/net/scanner.go deleted file mode 100644 index 259da0bfe..000000000 --- a/geg/net/scanner.go +++ /dev/null @@ -1,23 +0,0 @@ -package main - -import ( - "fmt" - "github.com/gogf/gf/g/net/gscanner" - "net" - "time" -) - -func main() { - //gscanner.New().SetTimeout(3*time.Second).ScanIp("192.168.2.1", "192.168.2.255", 80, func(conn net.Conn){ - // fmt.Println(conn.RemoteAddr()) - //}) - - gscanner.New().SetTimeout(3*time.Second).ScanIp("120.76.249.1", "120.76.249.255", 80, func(conn net.Conn) { - fmt.Println(conn.RemoteAddr()) - }) - - //gscanner.New().SetTimeout(6*time.Second).ScanPort("120.76.249.2", func(conn net.Conn){ - // //fmt.Println("yes") - // fmt.Println(conn.RemoteAddr()) - //}) -}