From a5ab2ba332e54ea07687775acc457d16566aa93e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E6=BD=AE?= <2892931976@qq.com> Date: Sat, 15 Jun 2019 14:23:28 +0800 Subject: [PATCH 01/20] gutil test 2019 06 15 14:20 --- g/util/gutil/gutil_comparator_z_unit_test.go | 92 ++++++++++++++++++++ g/util/gutil/gutil_z_unit_test.go | 68 +++++++++++++++ 2 files changed, 160 insertions(+) create mode 100755 g/util/gutil/gutil_comparator_z_unit_test.go create mode 100755 g/util/gutil/gutil_z_unit_test.go 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..5d8ed7a2e --- /dev/null +++ b/g/util/gutil/gutil_comparator_z_unit_test.go @@ -0,0 +1,92 @@ +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) { + j := gutil.ComparatorString(1, 1) + gtest.Assert(j, 0) +} + +func Test_ComparatorInt(t *testing.T) { + j := gutil.ComparatorInt(1, 1) + gtest.Assert(j, 0) +} + +func Test_ComparatorInt8(t *testing.T) { + j := gutil.ComparatorInt8(1, 1) + gtest.Assert(j, 0) +} + +func Test_ComparatorInt16(t *testing.T) { + j := gutil.ComparatorInt16(1, 1) + gtest.Assert(j, 0) +} + +func Test_ComparatorInt32(t *testing.T) { + j := gutil.ComparatorInt32(1, 1) + gtest.Assert(j, 0) +} + +func Test_ComparatorInt64(t *testing.T) { + j := gutil.ComparatorInt64(1, 1) + gtest.Assert(j, 0) +} + +func Test_ComparatorUint(t *testing.T) { + j := gutil.ComparatorUint(1, 1) + gtest.Assert(j, 0) +} + +func Test_ComparatorUint8(t *testing.T) { + j := gutil.ComparatorUint8(1, 1) + gtest.Assert(j, 0) +} + +func Test_ComparatorUint16(t *testing.T) { + j := gutil.ComparatorUint16(1, 1) + gtest.Assert(j, 0) +} + +func Test_ComparatorUint32(t *testing.T) { + j := gutil.ComparatorUint32(1, 1) + gtest.Assert(j, 0) +} + +func Test_ComparatorUint64(t *testing.T) { + j := gutil.ComparatorUint64(1, 1) + gtest.Assert(j, 0) +} + +func Test_ComparatorFloat32(t *testing.T) { + j := gutil.ComparatorFloat32(1, 1) + gtest.Assert(j, 0) +} + +func Test_ComparatorFloat64(t *testing.T) { + j := gutil.ComparatorFloat64(1, 1) + gtest.Assert(j, 0) +} + +func Test_ComparatorByte(t *testing.T) { + j := gutil.ComparatorByte(1, 1) + gtest.Assert(j, 0) +} + +func Test_ComparatorRune(t *testing.T) { + j := gutil.ComparatorRune(1, 1) + gtest.Assert(j, 0) +} + +func Test_ComparatorTime(t *testing.T) { + 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..20b87fd8b --- /dev/null +++ b/g/util/gutil/gutil_z_unit_test.go @@ -0,0 +1,68 @@ +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.Assert("", "") + }) + + gtest.Case(t, func() { + gutil.Dump(map[string]interface{}{"": func() {}}) + gtest.Assert("", "") + }) + + gtest.Case(t, func() { + gutil.Dump([]byte("gutil Dump test")) + gtest.Assert("", "") + }) +} + +func Test_PrintBacktrace(t *testing.T) { + gtest.Case(t, func() { + gutil.PrintBacktrace() + gtest.Assert("", "") + }) +} + +func Test_TryCatch(t *testing.T) { + + gutil.TryCatch(func() { + }, func(err interface{}) { + }) + gtest.Assert("", "") + + gutil.TryCatch(func() { + }) + gtest.Assert("", "") + + gutil.TryCatch(func() { + panic("gutil TryCatch test") + }, func(err interface{}) { + }) + gtest.Assert("", "") +} + +func Test_IsEmpty(t *testing.T) { + gtest.Assert(gutil.IsEmpty(1), false) +} + +func Test_Throw(t *testing.T) { + gtest.Case(t, func() { + defer func() { + if e := recover(); e != nil { + gtest.Assert(e, "gutil Throw test") + } + }() + + gutil.Throw("gutil Throw test") + }) +} From 674590e2476bdb64f62c1d6b3bb4be39f378df36 Mon Sep 17 00:00:00 2001 From: John Date: Tue, 18 Jun 2019 22:04:17 +0800 Subject: [PATCH 02/20] improve ghttp.RouterGroup --- g/net/ghttp/ghttp_server_router_group.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/g/net/ghttp/ghttp_server_router_group.go b/g/net/ghttp/ghttp_server_router_group.go index 075645b4f..17fdc9a83 100644 --- a/g/net/ghttp/ghttp_server_router_group.go +++ b/g/net/ghttp/ghttp_server_router_group.go @@ -55,10 +55,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]) } } } From abe14e049a7afa9ea17dc8580c9051e7bd699661 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E6=BD=AE?= <2892931976@qq.com> Date: Sun, 23 Jun 2019 16:45:30 +0800 Subject: [PATCH 03/20] gutil test 20190623 16:42 --- g/util/gutil/gutil_comparator_z_unit_test.go | 89 ++++++++++++++------ g/util/gutil/gutil_z_unit_test.go | 28 +++--- 2 files changed, 72 insertions(+), 45 deletions(-) diff --git a/g/util/gutil/gutil_comparator_z_unit_test.go b/g/util/gutil/gutil_comparator_z_unit_test.go index 5d8ed7a2e..fcdeba8b9 100755 --- a/g/util/gutil/gutil_comparator_z_unit_test.go +++ b/g/util/gutil/gutil_comparator_z_unit_test.go @@ -8,81 +8,114 @@ import ( ) func Test_ComparatorString(t *testing.T) { - j := gutil.ComparatorString(1, 1) - gtest.Assert(j, 0) + + 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) { - j := gutil.ComparatorInt(1, 1) - gtest.Assert(j, 0) + + 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) { - j := gutil.ComparatorInt8(1, 1) - gtest.Assert(j, 0) + + 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) { - j := gutil.ComparatorInt16(1, 1) - gtest.Assert(j, 0) + + 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) { - j := gutil.ComparatorInt32(1, 1) - gtest.Assert(j, 0) + + 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) { - j := gutil.ComparatorInt64(1, 1) - gtest.Assert(j, 0) + + 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) { j := gutil.ComparatorUint(1, 1) gtest.Assert(j, 0) + + 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) { - j := gutil.ComparatorUint8(1, 1) - gtest.Assert(j, 0) + + gtest.Assert(gutil.ComparatorUint8(1, 1), 0) + gtest.Assert(gutil.ComparatorUint8(1, 2), 255) + gtest.Assert(gutil.ComparatorUint8(2, 1), 1) } func Test_ComparatorUint16(t *testing.T) { - j := gutil.ComparatorUint16(1, 1) - gtest.Assert(j, 0) + + 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) { - j := gutil.ComparatorUint32(1, 1) - gtest.Assert(j, 0) + + gtest.Assert(gutil.ComparatorUint32(1, 1), 0) + gtest.Assert(gutil.ComparatorUint32(1, 2), 4294967295) + gtest.Assert(gutil.ComparatorUint32(2, 1), 1) } func Test_ComparatorUint64(t *testing.T) { - j := gutil.ComparatorUint64(1, 1) - gtest.Assert(j, 0) + + 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) { - j := gutil.ComparatorFloat32(1, 1) - gtest.Assert(j, 0) + + 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) { - j := gutil.ComparatorFloat64(1, 1) - gtest.Assert(j, 0) + + 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) { - j := gutil.ComparatorByte(1, 1) - gtest.Assert(j, 0) + + 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) { - j := gutil.ComparatorRune(1, 1) - gtest.Assert(j, 0) + + 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) { + j := gutil.ComparatorTime("2019-06-14", "2019-06-14") gtest.Assert(j, 0) k := gutil.ComparatorTime("2019-06-15", "2019-06-14") diff --git a/g/util/gutil/gutil_z_unit_test.go b/g/util/gutil/gutil_z_unit_test.go index 20b87fd8b..511605d18 100755 --- a/g/util/gutil/gutil_z_unit_test.go +++ b/g/util/gutil/gutil_z_unit_test.go @@ -12,43 +12,39 @@ func Test_Dump(t *testing.T) { gutil.Dump(map[int]int{ 100: 100, }) - gtest.Assert("", "") }) gtest.Case(t, func() { gutil.Dump(map[string]interface{}{"": func() {}}) - gtest.Assert("", "") }) gtest.Case(t, func() { gutil.Dump([]byte("gutil Dump test")) - gtest.Assert("", "") }) } func Test_PrintBacktrace(t *testing.T) { gtest.Case(t, func() { gutil.PrintBacktrace() - gtest.Assert("", "") }) } func Test_TryCatch(t *testing.T) { - gutil.TryCatch(func() { - }, func(err interface{}) { + gtest.Case(t, func() { + gutil.TryCatch(func() { + panic("gutil TryCatch test") + }) }) - gtest.Assert("", "") - gutil.TryCatch(func() { - }) - gtest.Assert("", "") + gtest.Case(t, func() { + gutil.TryCatch(func() { + panic("gutil TryCatch test") - gutil.TryCatch(func() { - panic("gutil TryCatch test") - }, func(err interface{}) { + }, func(err interface{}) { + gtest.Assert(err, "gutil TryCatch test") + }) }) - gtest.Assert("", "") } func Test_IsEmpty(t *testing.T) { @@ -58,9 +54,7 @@ func Test_IsEmpty(t *testing.T) { func Test_Throw(t *testing.T) { gtest.Case(t, func() { defer func() { - if e := recover(); e != nil { - gtest.Assert(e, "gutil Throw test") - } + gtest.Assert(recover(), "gutil Throw test") }() gutil.Throw("gutil Throw test") From 327e33b827e79b47be9ef1004495f745c970f396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E6=BD=AE?= <2892931976@qq.com> Date: Sun, 23 Jun 2019 20:44:03 +0800 Subject: [PATCH 04/20] gutil test 20190623 20:41 --- g/util/gutil/gutil_comparator_z_unit_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/g/util/gutil/gutil_comparator_z_unit_test.go b/g/util/gutil/gutil_comparator_z_unit_test.go index fcdeba8b9..6ec468eff 100755 --- a/g/util/gutil/gutil_comparator_z_unit_test.go +++ b/g/util/gutil/gutil_comparator_z_unit_test.go @@ -50,8 +50,6 @@ func Test_ComparatorInt64(t *testing.T) { } func Test_ComparatorUint(t *testing.T) { - j := gutil.ComparatorUint(1, 1) - gtest.Assert(j, 0) gtest.Assert(gutil.ComparatorUint(1, 1), 0) gtest.Assert(gutil.ComparatorUint(1, 2), -1) From 14348009828b948660f16b68ff4e54f25538ec3a Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Mon, 24 Jun 2019 09:27:31 +0800 Subject: [PATCH 05/20] Improve gmlock unit testing. --- g/os/gmlock/gmlock_unit_lock_test.go | 66 +++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/g/os/gmlock/gmlock_unit_lock_test.go b/g/os/gmlock/gmlock_unit_lock_test.go index fcd730dd7..27ddbdb1d 100644 --- a/g/os/gmlock/gmlock_unit_lock_test.go +++ b/g/os/gmlock/gmlock_unit_lock_test.go @@ -16,7 +16,6 @@ import ( ) func Test_Locker_Lock(t *testing.T) { - //no expire gtest.Case(t, func() { key := "testLock" array := garray.New() @@ -43,6 +42,71 @@ func Test_Locker_Lock(t *testing.T) { gtest.Assert(array.Len(), 3) time.Sleep(50 * time.Millisecond) gtest.Assert(array.Len(), 4) + 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(50 * time.Millisecond) + array.Append(1) + lock.Unlock(key) + }() + go func() { + time.Sleep(10 * time.Millisecond) + lock.Lock(key) + array.Append(1) + time.Sleep(100 * time.Millisecond) + array.Append(1) + lock.Unlock(key) + }() + time.Sleep(10 * 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) + 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(200 * time.Millisecond) + gmlock.Unlock(key) + }() + go func() { + time.Sleep(10 * time.Millisecond) + if gmlock.TryLock(key) { + array.Append(1) + gmlock.Unlock(key) + } + }() + go func() { + time.Sleep(300 * time.Millisecond) + if gmlock.TryLock(key) { + array.Append(1) + gmlock.Unlock(key) + } + }() + time.Sleep(50 * time.Millisecond) + gtest.Assert(array.Len(), 1) + time.Sleep(50 * time.Millisecond) + gtest.Assert(array.Len(), 1) + time.Sleep(300 * time.Millisecond) + gtest.Assert(array.Len(), 2) }) } From 2b02f7e21081acd32f907467f618288bd1ef24d8 Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Mon, 24 Jun 2019 12:50:00 +0800 Subject: [PATCH 06/20] Improve gmutex unit testing. --- g/os/gmutex/gmutex_uinit_test.go | 50 ++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/g/os/gmutex/gmutex_uinit_test.go b/g/os/gmutex/gmutex_uinit_test.go index c3389c507..e679c65ec 100644 --- a/g/os/gmutex/gmutex_uinit_test.go +++ b/g/os/gmutex/gmutex_uinit_test.go @@ -38,6 +38,24 @@ func Test_Mutex_RUnlock(t *testing.T) { 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) { @@ -227,32 +245,28 @@ func Test_Mutex_TryRLockFunc(t *testing.T) { go func() { mu.LockFunc(func() { array.Append(1) - time.Sleep(500 * time.Millisecond) + time.Sleep(200 * 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(300 * time.Millisecond) + mu.TryRLockFunc(func() { + array.Append(1) + }) + }() + } + time.Sleep(10 * time.Millisecond) + gtest.Assert(array.Len(), 1) time.Sleep(100 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(500 * 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) }) } From 38ad5d457be7b9fa5fec08c7c9b8013d10172aa9 Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Mon, 24 Jun 2019 15:01:12 +0800 Subject: [PATCH 07/20] Improve grpool unit testing. --- g/os/grpool/grpool_unit_test.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/g/os/grpool/grpool_unit_test.go b/g/os/grpool/grpool_unit_test.go index 372ea03a8..b5c900b2d 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(50 * 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) + }) } From 9985378062d46f33dd0a313ba8b31eb3c72c0f28 Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Mon, 24 Jun 2019 15:04:33 +0800 Subject: [PATCH 08/20] rename gmutex unit test file. --- g/os/gmutex/{gmutex_uinit_test.go => gmutex_unit_test.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename g/os/gmutex/{gmutex_uinit_test.go => gmutex_unit_test.go} (100%) diff --git a/g/os/gmutex/gmutex_uinit_test.go b/g/os/gmutex/gmutex_unit_test.go similarity index 100% rename from g/os/gmutex/gmutex_uinit_test.go rename to g/os/gmutex/gmutex_unit_test.go From 8eb10a58ad7401241aa29db0da8aede7a1b41cf2 Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Mon, 24 Jun 2019 17:15:30 +0800 Subject: [PATCH 09/20] Improve gflock. --- g/os/gflock/gflock.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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() } From 1bf53d8b89ef6b398adcb8245257e37b1b143bba Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Mon, 24 Jun 2019 17:15:46 +0800 Subject: [PATCH 10/20] Improve gflock unit testing. --- g/os/gflock/gflock_unit_test.go | 180 ++++++++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 g/os/gflock/gflock_unit_test.go diff --git a/g/os/gflock/gflock_unit_test.go b/g/os/gflock/gflock_unit_test.go new file mode 100644 index 000000000..493071bf4 --- /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(200 * time.Millisecond) + lock.Unlock() + }() + + go func() { + time.Sleep(50 * time.Millisecond) + lock2.Lock() + array.Append(1) + lock2.Unlock() + }() + + time.Sleep(50 * time.Millisecond) + gtest.Assert(array.Len(), 1) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 1) + time.Sleep(100 * 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(200 * time.Millisecond) + lock.RUnlock() + }() + + go func() { + time.Sleep(50 * time.Millisecond) + lock2.RLock() + array.Append(1) + lock2.RUnlock() + }() + + time.Sleep(50 * time.Millisecond) + gtest.Assert(array.Len(), 1) + time.Sleep(100 * 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) + }) +} From 0f14002b059b743ff90975507611eacb81aa836e Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Mon, 24 Jun 2019 17:17:24 +0800 Subject: [PATCH 11/20] Improve gflock unit testing. --- g/os/gflock/gflock_unit_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/g/os/gflock/gflock_unit_test.go b/g/os/gflock/gflock_unit_test.go index 493071bf4..4a07c4649 100644 --- a/g/os/gflock/gflock_unit_test.go +++ b/g/os/gflock/gflock_unit_test.go @@ -10,7 +10,6 @@ import ( "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" From 3806f9db07453afad3868efb29a02947b39ac295 Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Mon, 24 Jun 2019 17:33:56 +0800 Subject: [PATCH 12/20] Improve gflock unit testing. --- g/os/gflock/gflock_unit_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/g/os/gflock/gflock_unit_test.go b/g/os/gflock/gflock_unit_test.go index 4a07c4649..d3f1e6c3e 100644 --- a/g/os/gflock/gflock_unit_test.go +++ b/g/os/gflock/gflock_unit_test.go @@ -79,20 +79,20 @@ func Test_GFlock_RLock(t *testing.T) { go func() { lock.RLock() array.Append(1) - time.Sleep(200 * time.Millisecond) + time.Sleep(400 * time.Millisecond) lock.RUnlock() }() go func() { - time.Sleep(50 * time.Millisecond) + time.Sleep(200 * time.Millisecond) lock2.RLock() array.Append(1) lock2.RUnlock() }() - time.Sleep(50 * 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) }) } From dcf7772589e5ae000f41fce0ad32df96b85e69e6 Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Mon, 24 Jun 2019 18:04:35 +0800 Subject: [PATCH 13/20] Improve gmlock, gmutex unit testing sleep time. --- g/os/gmlock/gmlock_unit_rlock_test.go | 68 ++++++++++++--------------- g/os/gmutex/gmutex_unit_test.go | 47 +++++++++--------- 2 files changed, 54 insertions(+), 61 deletions(-) diff --git a/g/os/gmlock/gmlock_unit_rlock_test.go b/g/os/gmlock/gmlock_unit_rlock_test.go index ca596040f..465435402 100644 --- a/g/os/gmlock/gmlock_unit_rlock_test.go +++ b/g/os/gmlock/gmlock_unit_rlock_test.go @@ -23,7 +23,7 @@ func Test_Locker_RLock(t *testing.T) { go func() { gmlock.RLock(key) array.Append(1) - time.Sleep(50 * time.Millisecond) + time.Sleep(100 * time.Millisecond) array.Append(1) gmlock.RUnlock(key) }() @@ -35,7 +35,7 @@ func Test_Locker_RLock(t *testing.T) { }() time.Sleep(20 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(80 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gtest.Assert(array.Len(), 3) }) @@ -46,7 +46,7 @@ func Test_Locker_RLock(t *testing.T) { go func() { gmlock.Lock(key) array.Append(1) - time.Sleep(50 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gmlock.Unlock(key) }() go func() { @@ -57,7 +57,7 @@ func Test_Locker_RLock(t *testing.T) { }() time.Sleep(20 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(50 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gtest.Assert(array.Len(), 2) }) @@ -68,26 +68,26 @@ func Test_Locker_RLock(t *testing.T) { go func() { gmlock.Lock(key) array.Append(1) - time.Sleep(50 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gmlock.Unlock(key) }() go func() { time.Sleep(10 * 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) gmlock.RLock(key) array.Append(1) - time.Sleep(70 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gmlock.RUnlock(key) }() time.Sleep(20 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(120 * time.Millisecond) + time.Sleep(500 * time.Millisecond) gtest.Assert(array.Len(), 3) }) } @@ -100,7 +100,7 @@ func Test_Locker_TryRLock(t *testing.T) { go func() { gmlock.Lock(key) array.Append(1) - time.Sleep(50 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gmlock.Unlock(key) }() go func() { @@ -112,7 +112,7 @@ func Test_Locker_TryRLock(t *testing.T) { }() time.Sleep(20 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(50 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gtest.Assert(array.Len(), 1) }) @@ -123,7 +123,7 @@ func Test_Locker_TryRLock(t *testing.T) { go func() { gmlock.Lock(key) array.Append(1) - time.Sleep(50 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gmlock.Unlock(key) }() go func() { @@ -134,7 +134,7 @@ func Test_Locker_TryRLock(t *testing.T) { } }() go func() { - time.Sleep(70 * time.Millisecond) + time.Sleep(150 * time.Millisecond) if gmlock.TryRLock(key) { array.Append(1) gmlock.RUnlock(key) @@ -142,12 +142,12 @@ func Test_Locker_TryRLock(t *testing.T) { }() time.Sleep(20 * 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,7 +155,7 @@ func Test_Locker_RLockFunc1(t *testing.T) { go func() { gmlock.RLockFunc(key, func() { array.Append(1) - time.Sleep(500 * time.Millisecond) + time.Sleep(100 * time.Millisecond) array.Append(1) }) }() @@ -165,9 +165,9 @@ func Test_Locker_RLockFunc1(t *testing.T) { array.Append(1) gmlock.Unlock(key) }() - time.Sleep(200 * time.Millisecond) + time.Sleep(20 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(800 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gtest.Assert(array.Len(), 3) }) @@ -178,7 +178,7 @@ func Test_Locker_RLockFunc1(t *testing.T) { go func() { gmlock.Lock(key) array.Append(1) - time.Sleep(50 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gmlock.Unlock(key) }() go func() { @@ -189,13 +189,10 @@ func Test_Locker_RLockFunc1(t *testing.T) { }() time.Sleep(20 * 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" @@ -203,29 +200,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(100 * time.Millisecond) gmlock.Unlock(key) }() go func() { - time.Sleep(100 * time.Millisecond) + time.Sleep(10 * 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) + time.Sleep(10 * 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(20 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(700 * time.Millisecond) + time.Sleep(400 * time.Millisecond) gtest.Assert(array.Len(), 3) }) } @@ -238,7 +232,7 @@ func Test_Locker_TryRLockFunc(t *testing.T) { go func() { gmlock.Lock(key) array.Append(1) - time.Sleep(50 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gmlock.Unlock(key) }() go func() { @@ -249,7 +243,7 @@ func Test_Locker_TryRLockFunc(t *testing.T) { }() time.Sleep(20 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(50 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gtest.Assert(array.Len(), 1) }) @@ -260,7 +254,7 @@ func Test_Locker_TryRLockFunc(t *testing.T) { go func() { gmlock.Lock(key) array.Append(1) - time.Sleep(50 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gmlock.Unlock(key) }() go func() { @@ -270,14 +264,14 @@ func Test_Locker_TryRLockFunc(t *testing.T) { }) }() go func() { - time.Sleep(70 * time.Millisecond) + time.Sleep(150 * time.Millisecond) gmlock.TryRLockFunc(key, func() { array.Append(1) }) }() time.Sleep(20 * 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_unit_test.go b/g/os/gmutex/gmutex_unit_test.go index e679c65ec..bad656643 100644 --- a/g/os/gmutex/gmutex_unit_test.go +++ b/g/os/gmutex/gmutex_unit_test.go @@ -95,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(200 * 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(100 * time.Millisecond) + mu.Unlock() mu.Unlock() mu.Unlock() mu.Unlock() }() - time.Sleep(20 * time.Millisecond) + time.Sleep(50 * 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) }) } @@ -134,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) }) } @@ -159,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) }) } @@ -190,7 +189,7 @@ func Test_Mutex_RLockFunc(t *testing.T) { go func() { mu.LockFunc(func() { array.Append(1) - time.Sleep(100 * time.Millisecond) + time.Sleep(200 * time.Millisecond) }) }() go func() { @@ -200,11 +199,11 @@ func Test_Mutex_RLockFunc(t *testing.T) { time.Sleep(100 * time.Millisecond) }) }() - time.Sleep(20 * time.Millisecond) - gtest.Assert(array.Len(), 1) time.Sleep(50 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(50 * time.Millisecond) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 1) + time.Sleep(300 * time.Millisecond) gtest.Assert(array.Len(), 2) }) @@ -233,7 +232,7 @@ func Test_Mutex_RLockFunc(t *testing.T) { }) }() gtest.Assert(array.Len(), 0) - time.Sleep(80 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gtest.Assert(array.Len(), 3) }) } From f25571a0a9e908362561deee18505b17bcd9fad9 Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Mon, 24 Jun 2019 19:34:53 +0800 Subject: [PATCH 14/20] Improve gmlock, gmutex, grpool, gmutex unit testing sleep time. --- g/os/gflock/gflock_unit_test.go | 8 +-- g/os/gmlock/gmlock_unit_lock_test.go | 68 ++++++++++------------- g/os/gmlock/gmlock_unit_rlock_test.go | 78 +++++++++++++-------------- g/os/gmutex/gmutex_unit_test.go | 42 +++++++-------- g/os/grpool/grpool_unit_test.go | 2 +- 5 files changed, 93 insertions(+), 105 deletions(-) diff --git a/g/os/gflock/gflock_unit_test.go b/g/os/gflock/gflock_unit_test.go index d3f1e6c3e..e24bbdee7 100644 --- a/g/os/gflock/gflock_unit_test.go +++ b/g/os/gflock/gflock_unit_test.go @@ -49,22 +49,22 @@ func Test_GFlock_Lock(t *testing.T) { go func() { lock.Lock() array.Append(1) - time.Sleep(200 * time.Millisecond) + time.Sleep(300 * time.Millisecond) lock.Unlock() }() go func() { - time.Sleep(50 * time.Millisecond) + time.Sleep(100 * time.Millisecond) lock2.Lock() array.Append(1) lock2.Unlock() }() - time.Sleep(50 * time.Millisecond) - gtest.Assert(array.Len(), 1) 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) }) } diff --git a/g/os/gmlock/gmlock_unit_lock_test.go b/g/os/gmlock/gmlock_unit_lock_test.go index 27ddbdb1d..da1132d4b 100644 --- a/g/os/gmlock/gmlock_unit_lock_test.go +++ b/g/os/gmlock/gmlock_unit_lock_test.go @@ -22,26 +22,21 @@ func Test_Locker_Lock(t *testing.T) { 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) }) @@ -52,26 +47,21 @@ func Test_Locker_Lock(t *testing.T) { go func() { lock.Lock(key) array.Append(1) - time.Sleep(50 * time.Millisecond) - array.Append(1) + time.Sleep(300 * time.Millisecond) lock.Unlock(key) }() go func() { - time.Sleep(10 * time.Millisecond) - lock.Lock(key) - array.Append(1) time.Sleep(100 * time.Millisecond) + lock.Lock(key) array.Append(1) lock.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) lock.Clear() }) @@ -84,26 +74,26 @@ func Test_Locker_TryLock(t *testing.T) { go func() { gmlock.Lock(key) array.Append(1) - time.Sleep(200 * time.Millisecond) + time.Sleep(300 * time.Millisecond) gmlock.Unlock(key) }() go func() { - time.Sleep(10 * time.Millisecond) + time.Sleep(150 * time.Millisecond) if gmlock.TryLock(key) { array.Append(1) gmlock.Unlock(key) } }() go func() { - time.Sleep(300 * time.Millisecond) + time.Sleep(400 * time.Millisecond) if gmlock.TryLock(key) { array.Append(1) gmlock.Unlock(key) } }() - time.Sleep(50 * 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(300 * time.Millisecond) gtest.Assert(array.Len(), 2) @@ -119,20 +109,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) }) } @@ -144,24 +134,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) }) } diff --git a/g/os/gmlock/gmlock_unit_rlock_test.go b/g/os/gmlock/gmlock_unit_rlock_test.go index 465435402..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(100 * 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(200 * time.Millisecond) - gtest.Assert(array.Len(), 3) + gtest.Assert(array.Len(), 2) }) //Lock before RLock @@ -46,16 +45,16 @@ func Test_Locker_RLock(t *testing.T) { go func() { gmlock.Lock(key) array.Append(1) - time.Sleep(100 * 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(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(100 * 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(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(200 * time.Millisecond) gmlock.RUnlock(key) }() - time.Sleep(20 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(500 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gtest.Assert(array.Len(), 3) }) } @@ -100,17 +99,17 @@ func Test_Locker_TryRLock(t *testing.T) { go func() { gmlock.Lock(key) array.Append(1) - time.Sleep(100 * 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(200 * time.Millisecond) gtest.Assert(array.Len(), 1) @@ -123,24 +122,24 @@ func Test_Locker_TryRLock(t *testing.T) { go func() { gmlock.Lock(key) array.Append(1) - time.Sleep(100 * 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(150 * 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(200 * time.Millisecond) gtest.Assert(array.Len(), 2) @@ -155,20 +154,19 @@ func Test_Locker_RLockFunc(t *testing.T) { go func() { gmlock.RLockFunc(key, func() { array.Append(1) - time.Sleep(100 * 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(20 * time.Millisecond) + time.Sleep(150 * time.Millisecond) gtest.Assert(array.Len(), 1) time.Sleep(200 * time.Millisecond) - gtest.Assert(array.Len(), 3) + gtest.Assert(array.Len(), 2) }) //Lock before RLockFunc @@ -178,16 +176,16 @@ func Test_Locker_RLockFunc(t *testing.T) { go func() { gmlock.Lock(key) array.Append(1) - time.Sleep(100 * 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(200 * time.Millisecond) gtest.Assert(array.Len(), 2) @@ -200,26 +198,26 @@ func Test_Locker_RLockFunc(t *testing.T) { go func() { gmlock.Lock(key) array.Append(1) - time.Sleep(100 * 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(200 * time.Millisecond) }) }() go func() { - time.Sleep(10 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gmlock.RLockFunc(key, func() { array.Append(1) time.Sleep(200 * time.Millisecond) }) }() - time.Sleep(20 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(400 * time.Millisecond) + time.Sleep(200 * time.Millisecond) gtest.Assert(array.Len(), 3) }) } @@ -232,16 +230,16 @@ func Test_Locker_TryRLockFunc(t *testing.T) { go func() { gmlock.Lock(key) array.Append(1) - time.Sleep(100 * 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(200 * time.Millisecond) gtest.Assert(array.Len(), 1) @@ -254,22 +252,22 @@ func Test_Locker_TryRLockFunc(t *testing.T) { go func() { gmlock.Lock(key) array.Append(1) - time.Sleep(100 * 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(150 * 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(300 * time.Millisecond) gtest.Assert(array.Len(), 2) diff --git a/g/os/gmutex/gmutex_unit_test.go b/g/os/gmutex/gmutex_unit_test.go index bad656643..74d42edb1 100644 --- a/g/os/gmutex/gmutex_unit_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,7 +34,7 @@ func Test_Mutex_RUnlock(t *testing.T) { mu.RUnlock() }() } - time.Sleep(150 * time.Millisecond) + time.Sleep(300 * time.Millisecond) gtest.Assert(mu.IsRLocked(), false) }) @@ -63,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) }) } @@ -95,7 +95,7 @@ func Test_Mutex_Unlock(t *testing.T) { go func() { mu.LockFunc(func() { array.Append(1) - time.Sleep(200 * time.Millisecond) + time.Sleep(300 * time.Millisecond) }) }() go func() { @@ -112,14 +112,14 @@ func Test_Mutex_Unlock(t *testing.T) { }() go func() { - time.Sleep(100 * time.Millisecond) + time.Sleep(200 * time.Millisecond) mu.Unlock() mu.Unlock() mu.Unlock() mu.Unlock() }() - time.Sleep(50 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gtest.Assert(array.Len(), 1) time.Sleep(400 * time.Millisecond) gtest.Assert(array.Len(), 3) @@ -189,17 +189,17 @@ func Test_Mutex_RLockFunc(t *testing.T) { go func() { mu.LockFunc(func() { array.Append(1) - time.Sleep(200 * 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(50 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gtest.Assert(array.Len(), 1) time.Sleep(100 * time.Millisecond) gtest.Assert(array.Len(), 1) @@ -211,21 +211,21 @@ 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) @@ -244,7 +244,7 @@ func Test_Mutex_TryRLockFunc(t *testing.T) { go func() { mu.LockFunc(func() { array.Append(1) - time.Sleep(200 * time.Millisecond) + time.Sleep(300 * time.Millisecond) }) }() go func() { @@ -255,13 +255,13 @@ func Test_Mutex_TryRLockFunc(t *testing.T) { }() for index := 0; index < 1000; index++ { go func() { - time.Sleep(300 * time.Millisecond) + time.Sleep(400 * time.Millisecond) mu.TryRLockFunc(func() { array.Append(1) }) }() } - time.Sleep(10 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gtest.Assert(array.Len(), 1) time.Sleep(100 * time.Millisecond) gtest.Assert(array.Len(), 1) diff --git a/g/os/grpool/grpool_unit_test.go b/g/os/grpool/grpool_unit_test.go index b5c900b2d..90ce424bd 100644 --- a/g/os/grpool/grpool_unit_test.go +++ b/g/os/grpool/grpool_unit_test.go @@ -31,7 +31,7 @@ func Test_Basic(t *testing.T) { }) } wg.Wait() - time.Sleep(50 * time.Millisecond) + time.Sleep(100 * time.Millisecond) gtest.Assert(array.Len(), size) gtest.Assert(grpool.Jobs(), 0) gtest.Assert(grpool.Size(), 0) From 51cf2326911b44b1588c00ab781a7a476cdc075e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E6=BD=AE?= <2892931976@qq.com> Date: Tue, 25 Jun 2019 15:50:06 +0800 Subject: [PATCH 15/20] gutil test 20190615 15:47 --- g/util/gutil/gutil_comparator_z_unit_test.go | 135 ++++++++++++------- g/util/gutil/gutil_z_unit_test.go | 6 +- 2 files changed, 89 insertions(+), 52 deletions(-) diff --git a/g/util/gutil/gutil_comparator_z_unit_test.go b/g/util/gutil/gutil_comparator_z_unit_test.go index 6ec468eff..99c672190 100755 --- a/g/util/gutil/gutil_comparator_z_unit_test.go +++ b/g/util/gutil/gutil_comparator_z_unit_test.go @@ -9,115 +9,148 @@ import ( func Test_ComparatorString(t *testing.T) { - gtest.Assert(gutil.ComparatorString(1, 1), 0) - gtest.Assert(gutil.ComparatorString(1, 2), -1) - gtest.Assert(gutil.ComparatorString(2, 1), 1) + 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.Assert(gutil.ComparatorInt(1, 1), 0) - gtest.Assert(gutil.ComparatorInt(1, 2), -1) - gtest.Assert(gutil.ComparatorInt(2, 1), 1) + 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.Assert(gutil.ComparatorInt8(1, 1), 0) - gtest.Assert(gutil.ComparatorInt8(1, 2), -1) - gtest.Assert(gutil.ComparatorInt8(2, 1), 1) + 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.Assert(gutil.ComparatorInt16(1, 1), 0) - gtest.Assert(gutil.ComparatorInt16(1, 2), -1) - gtest.Assert(gutil.ComparatorInt16(2, 1), 1) + 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.Assert(gutil.ComparatorInt32(1, 1), 0) - gtest.Assert(gutil.ComparatorInt32(1, 2), -1) - gtest.Assert(gutil.ComparatorInt32(2, 1), 1) + 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.Assert(gutil.ComparatorInt64(1, 1), 0) - gtest.Assert(gutil.ComparatorInt64(1, 2), -1) - gtest.Assert(gutil.ComparatorInt64(2, 1), 1) + 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.Assert(gutil.ComparatorUint(1, 1), 0) - gtest.Assert(gutil.ComparatorUint(1, 2), -1) - gtest.Assert(gutil.ComparatorUint(2, 1), 1) + 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.Assert(gutil.ComparatorUint8(1, 1), 0) - gtest.Assert(gutil.ComparatorUint8(1, 2), 255) - gtest.Assert(gutil.ComparatorUint8(2, 1), 1) + 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.Assert(gutil.ComparatorUint16(1, 1), 0) - gtest.Assert(gutil.ComparatorUint16(1, 2), 65535) - gtest.Assert(gutil.ComparatorUint16(2, 1), 1) + 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.Assert(gutil.ComparatorUint32(1, 1), 0) - gtest.Assert(gutil.ComparatorUint32(1, 2), 4294967295) - gtest.Assert(gutil.ComparatorUint32(2, 1), 1) + 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.Assert(gutil.ComparatorUint64(1, 1), 0) - gtest.Assert(gutil.ComparatorUint64(1, 2), -1) - gtest.Assert(gutil.ComparatorUint64(2, 1), 1) + 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.Assert(gutil.ComparatorFloat32(1, 1), 0) - gtest.Assert(gutil.ComparatorFloat32(1, 2), -1) - gtest.Assert(gutil.ComparatorFloat32(2, 1), 1) + 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.Assert(gutil.ComparatorFloat64(1, 1), 0) - gtest.Assert(gutil.ComparatorFloat64(1, 2), -1) - gtest.Assert(gutil.ComparatorFloat64(2, 1), 1) + 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.Assert(gutil.ComparatorByte(1, 1), 0) - gtest.Assert(gutil.ComparatorByte(1, 2), 255) - gtest.Assert(gutil.ComparatorByte(2, 1), 1) + 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.Assert(gutil.ComparatorRune(1, 1), 0) - gtest.Assert(gutil.ComparatorRune(1, 2), -1) - gtest.Assert(gutil.ComparatorRune(2, 1), 1) + 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) - 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) + 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 index 511605d18..2c25edb47 100755 --- a/g/util/gutil/gutil_z_unit_test.go +++ b/g/util/gutil/gutil_z_unit_test.go @@ -48,10 +48,14 @@ func Test_TryCatch(t *testing.T) { } func Test_IsEmpty(t *testing.T) { - gtest.Assert(gutil.IsEmpty(1), false) + + 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") From c0aebe023c26249e3bf16f8a22bec665b0d875bb Mon Sep 17 00:00:00 2001 From: John Date: Tue, 25 Jun 2019 18:40:55 +0800 Subject: [PATCH 16/20] remove example codes for gcanner --- geg/net/scanner.go | 23 ----------------------- 1 file changed, 23 deletions(-) delete mode 100644 geg/net/scanner.go 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()) - //}) -} From 56a62bb4c2a74729b6c1d561fb885f0944dc9f6c Mon Sep 17 00:00:00 2001 From: John Date: Tue, 25 Jun 2019 19:01:27 +0800 Subject: [PATCH 17/20] gofmt --- g/util/gconv/gconv_z_all_test.go | 118 +++---- g/util/gutil/gutil_comparator_z_unit_test.go | 312 +++++++++---------- g/util/gutil/gutil_z_unit_test.go | 132 ++++---- 3 files changed, 281 insertions(+), 281 deletions(-) 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 index 99c672190..82b556a68 100755 --- a/g/util/gutil/gutil_comparator_z_unit_test.go +++ b/g/util/gutil/gutil_comparator_z_unit_test.go @@ -1,156 +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) - }) -} +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 index 2c25edb47..cf52edc2a 100755 --- a/g/util/gutil/gutil_z_unit_test.go +++ b/g/util/gutil/gutil_z_unit_test.go @@ -1,66 +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") - }) -} +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") + }) +} From 591158adc58a310b410263e26151a2da0be34a78 Mon Sep 17 00:00:00 2001 From: John Date: Tue, 25 Jun 2019 23:03:29 +0800 Subject: [PATCH 18/20] improve router registry for ghttp --- g/net/ghttp/ghttp_server_router_group.go | 11 ++++++----- g/net/ghttp/ghttp_server_router_serve.go | 6 +++++- g/net/ghttp/ghttp_server_service_controller.go | 15 +++++++++++++-- g/net/ghttp/ghttp_server_service_object.go | 15 +++++++++++++-- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/g/net/ghttp/ghttp_server_router_group.go b/g/net/ghttp/ghttp_server_router_group.go index ee65be8cf..9be28c77a 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" ) // 分组路由对象 @@ -126,10 +127,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) From 5fa0f8dd7e1f2c5d3fccd5ad6d43d21271edc5db Mon Sep 17 00:00:00 2001 From: John Date: Tue, 25 Jun 2019 23:06:12 +0800 Subject: [PATCH 19/20] up --- g/os/gflock/gflock_unit_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/g/os/gflock/gflock_unit_test.go b/g/os/gflock/gflock_unit_test.go index e24bbdee7..689f0a921 100644 --- a/g/os/gflock/gflock_unit_test.go +++ b/g/os/gflock/gflock_unit_test.go @@ -3,6 +3,7 @@ // 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 ( From 133531b6a031ae2e87c8681db53ba5155f9a3d8f Mon Sep 17 00:00:00 2001 From: John Date: Tue, 25 Jun 2019 23:17:14 +0800 Subject: [PATCH 20/20] README updates --- README.MD | 66 ++++++++++++++++++++++++++----------------------------- 1 file changed, 31 insertions(+), 35 deletions(-) diff --git a/README.MD b/README.MD index 26310d175..ab18f553f 100644 --- a/README.MD +++ b/README.MD @@ -11,48 +11,35 @@ -`GF(Go Frame)`是一款模块化、高性能、生产级Go应用开发框架。提供了常用的核心开发组件,如:缓存、日志、文件、时间、队列、数组、集合、字符串、定时器、命令行、文件锁、内存锁、对象池、连接池、数据校验、数据编码、文件监控、定时任务、数据库ORM、TCP/UDP组件、进程管理/通信、 -并发安全容器等等。并提供了Web服务开发的系列核心组件,如:Router、Cookie、Session、服务注册、配置管理、模板引擎等等,支持热重启、热更新、多域名、多端口、多服务、HTTPS、Rewrite等特性。 +`GF(GoFrame)` is a modular, full-featured and production-ready application development framework of golang. Providing a series of core components and dozens of practical modules, such as: memcache, configure, validator, logging, array/queue/set/map containers, timer/timing tasks, file/memory lock, object pool, database ORM, etc. Supporting web server integrated with router, cookie, session, logger, template, https, hooks, rewrites and many more features. -# 特点 -* 模块化、松耦合设计; -* 模块丰富,开箱即用; -* 详尽的开发文档及示例; -* 完善的本地中文化支持; -* 致力于项目的通用方案; -* 更适合企业及团队使用; -* 更多请查阅文档及源码; - -# 安装 -```html +# Installation +``` go get -u github.com/gogf/gf ``` -或者 -`go.mod`: +or use `go.mod`: ``` require github.com/gogf/gf latest ``` -# 限制 -```shell -golang版本 >= 1.10 +# Limitation +``` +golang version >= 1.10 ``` -# 架构 +# Documentation + +* [APIDoc](https://godoc.org/github.com/gogf/gf) +* [中文文档](https://goframe.org) + +# Architecture
+# Quick Start - -# 文档 - -开发文档:[https://goframe.org](https://goframe.org) - -接口文档:[https://godoc.org/github.com/gogf/gf](https://godoc.org/github.com/gogf/gf) - -# 使用 ```go package main @@ -70,17 +57,26 @@ func main() { } ``` -[更多..](https://goframe.org/start/index) +[View More..](https://goframe.org/start/index) -# 协议 +# License + +`GF` is licensed under the [MIT License](LICENSE), 100% free and open-source, forever. + +# Donators + +We currently accept donation by Alipay/WechatPay, please note your github/gitee account in your payment bill. If you like `GF`, why not [buy developer a cup of coffee](DONATOR.MD)? + +# Thanks +JetBrains + + + -`GF` 使用非常友好的 [MIT](LICENSE) 开源协议进行发布,永久`100%`开源免费。 -# 捐赠 -如果您喜欢`GF`,要不[给开发者来杯咖啡吧](DONATOR.MD)! -请在捐赠时备注您的`github`/`gitee`账号名称。 -# 感谢 -JetBrains \ No newline at end of file