From e4e58791a6645fd6aa7a572c16d96de3b0020b1a Mon Sep 17 00:00:00 2001 From: "zcool321@sina.com" <1234qwer> Date: Thu, 13 Jun 2019 23:50:12 +0800 Subject: [PATCH 1/7] add empty test --- g/internal/empty/empty_test.go | 90 ++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 g/internal/empty/empty_test.go diff --git a/g/internal/empty/empty_test.go b/g/internal/empty/empty_test.go new file mode 100644 index 000000000..0241a8b2c --- /dev/null +++ b/g/internal/empty/empty_test.go @@ -0,0 +1,90 @@ +// 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 empty_test + +import ( + "github.com/gogf/gf/g" + "github.com/gogf/gf/g/internal/empty" + "github.com/gogf/gf/g/test/gtest" + "github.com/gogf/gf/g/util/gconv" + "testing" +) + +type TestPerson interface { + Say() string +} +type TestWoman struct { +} + +func (woman TestWoman) Say() string { + return "nice" +} + +func TestIsEmpty(t *testing.T) { + gtest.Case(t, func() { + tmpT1 := "0" + tmpT2 := func() {} + tmpT2 = nil + tmpT3 := make(chan int, 0) + var tmpT4 TestPerson = nil + var tmpT5 *TestPerson = nil + tmpF1 := "1" + tmpF2 := func(a string) string { return "1" } + tmpF3 := make(chan int, 1) + tmpF3 <- 1 + var tmpF4 TestPerson = TestWoman{} + tmpF5 := &tmpF4 + // true + gtest.Assert(empty.IsEmpty(nil), true) + gtest.Assert(empty.IsEmpty(gconv.Int(tmpT1)), true) + gtest.Assert(empty.IsEmpty(gconv.Int8(tmpT1)), true) + gtest.Assert(empty.IsEmpty(gconv.Int16(tmpT1)), true) + gtest.Assert(empty.IsEmpty(gconv.Int32(tmpT1)), true) + gtest.Assert(empty.IsEmpty(gconv.Int64(tmpT1)), true) + gtest.Assert(empty.IsEmpty(gconv.Uint64(tmpT1)), true) + gtest.Assert(empty.IsEmpty(gconv.Uint(tmpT1)), true) + gtest.Assert(empty.IsEmpty(gconv.Uint16(tmpT1)), true) + gtest.Assert(empty.IsEmpty(gconv.Uint32(tmpT1)), true) + gtest.Assert(empty.IsEmpty(gconv.Uint64(tmpT1)), true) + gtest.Assert(empty.IsEmpty(gconv.Float32(tmpT1)), true) + gtest.Assert(empty.IsEmpty(gconv.Float64(tmpT1)), true) + gtest.Assert(empty.IsEmpty(false), true) + gtest.Assert(empty.IsEmpty([]byte("")), true) + gtest.Assert(empty.IsEmpty(""), true) + gtest.Assert(empty.IsEmpty(g.Map{}), true) + gtest.Assert(empty.IsEmpty(g.Slice{}), true) + gtest.Assert(empty.IsEmpty(g.Array{}), true) + gtest.Assert(empty.IsEmpty(tmpT2), true) + gtest.Assert(empty.IsEmpty(tmpT3), true) + gtest.Assert(empty.IsEmpty(tmpT3), true) + gtest.Assert(empty.IsEmpty(tmpT4), true) + gtest.Assert(empty.IsEmpty(tmpT5), true) + // false + gtest.Assert(empty.IsEmpty(gconv.Int(tmpF1)), false) + gtest.Assert(empty.IsEmpty(gconv.Int8(tmpF1)), false) + gtest.Assert(empty.IsEmpty(gconv.Int16(tmpF1)), false) + gtest.Assert(empty.IsEmpty(gconv.Int32(tmpF1)), false) + gtest.Assert(empty.IsEmpty(gconv.Int64(tmpF1)), false) + gtest.Assert(empty.IsEmpty(gconv.Uint(tmpF1)), false) + gtest.Assert(empty.IsEmpty(gconv.Uint8(tmpF1)), false) + gtest.Assert(empty.IsEmpty(gconv.Uint16(tmpF1)), false) + gtest.Assert(empty.IsEmpty(gconv.Uint32(tmpF1)), false) + gtest.Assert(empty.IsEmpty(gconv.Uint64(tmpF1)), false) + gtest.Assert(empty.IsEmpty(gconv.Float32(tmpF1)), false) + gtest.Assert(empty.IsEmpty(gconv.Float64(tmpF1)), false) + gtest.Assert(empty.IsEmpty(true), false) + gtest.Assert(empty.IsEmpty(tmpT1), false) + gtest.Assert(empty.IsEmpty([]byte("1")), false) + gtest.Assert(empty.IsEmpty(g.Map{"a": 1}), false) + gtest.Assert(empty.IsEmpty(g.Slice{"1"}), false) + gtest.Assert(empty.IsEmpty(g.Array{"1"}), false) + gtest.Assert(empty.IsEmpty(tmpF2), false) + gtest.Assert(empty.IsEmpty(tmpF3), false) + gtest.Assert(empty.IsEmpty(tmpF4), false) + gtest.Assert(empty.IsEmpty(tmpF5), false) + }) +} From 48f610a216ce31182e8f7c7db13c37c20d94f406 Mon Sep 17 00:00:00 2001 From: "zcool321@sina.com" <1234qwer> Date: Fri, 14 Jun 2019 00:03:13 +0800 Subject: [PATCH 2/7] add mutex and rwmutex test --- g/internal/mutex/mutex_z_unit_test.go | 32 +++++++++++++++++++ g/internal/rwmutex/rwmutex_z_unit_test.go | 38 +++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 g/internal/mutex/mutex_z_unit_test.go create mode 100644 g/internal/rwmutex/rwmutex_z_unit_test.go diff --git a/g/internal/mutex/mutex_z_unit_test.go b/g/internal/mutex/mutex_z_unit_test.go new file mode 100644 index 000000000..905d6c0d2 --- /dev/null +++ b/g/internal/mutex/mutex_z_unit_test.go @@ -0,0 +1,32 @@ +// Copyright 2018 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 mutex_test + +import ( + "github.com/gogf/gf/g/internal/mutex" + "github.com/gogf/gf/g/test/gtest" + "testing" +) + +func TestMutex(t *testing.T) { + gtest.Case(t, func() { + lock := mutex.New() + lock.Lock() + lock.Unlock() + gtest.Assert(lock.IsSafe(), true) + + safeLock1 := mutex.New(false) + safeLock1.Lock() + safeLock1.Unlock() + gtest.Assert(safeLock1.IsSafe(), true) + + unsafeLock1 := mutex.New(true) + unsafeLock1.Lock() + unsafeLock1.Unlock() + gtest.Assert(unsafeLock1.IsSafe(), false) + }) +} diff --git a/g/internal/rwmutex/rwmutex_z_unit_test.go b/g/internal/rwmutex/rwmutex_z_unit_test.go new file mode 100644 index 000000000..d6ada7ada --- /dev/null +++ b/g/internal/rwmutex/rwmutex_z_unit_test.go @@ -0,0 +1,38 @@ +// Copyright 2018 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 rwmutex_test + +import ( + "github.com/gogf/gf/g/internal/rwmutex" + "github.com/gogf/gf/g/test/gtest" + "testing" +) + +func TestRwmutex(t *testing.T) { + gtest.Case(t, func() { + lock := rwmutex.New() + lock.Lock() + lock.Unlock() + lock.RLock() + lock.RUnlock() + gtest.Assert(lock.IsSafe(), true) + + safeLock1 := rwmutex.New(false) + safeLock1.Lock() + safeLock1.Unlock() + safeLock1.RLock() + safeLock1.RUnlock() + gtest.Assert(safeLock1.IsSafe(), true) + + unsafeLock1 := rwmutex.New(true) + unsafeLock1.Lock() + unsafeLock1.Unlock() + unsafeLock1.RLock() + unsafeLock1.RUnlock() + gtest.Assert(unsafeLock1.IsSafe(), false) + }) +} From 5baa82da8faa59cd796d8e987f324800ec4aed8d Mon Sep 17 00:00:00 2001 From: "zcool321@sina.com" <1234qwer> Date: Fri, 14 Jun 2019 00:35:18 +0800 Subject: [PATCH 3/7] add gcmd test --- g/os/gcmd/gcmd_z_unit_test.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/g/os/gcmd/gcmd_z_unit_test.go b/g/os/gcmd/gcmd_z_unit_test.go index bd9e05a84..dda04293e 100644 --- a/g/os/gcmd/gcmd_z_unit_test.go +++ b/g/os/gcmd/gcmd_z_unit_test.go @@ -14,7 +14,6 @@ import ( "testing" ) - func Test_ValueAndOption(t *testing.T) { os.Args = []string{"v1", "v2", "--o1=111", "-o2=222"} doInit() @@ -23,6 +22,28 @@ func Test_ValueAndOption(t *testing.T) { gtest.Assert(Value.Get(0), "v1") gtest.Assert(Value.Get(1), "v2") gtest.Assert(Value.Get(2), "") + gtest.Assert(Value.Get(2, "1"), "1") + gtest.Assert(Value.GetVar(1, "1").String(), "v2") + gtest.Assert(Value.GetVar(2, "1").String(), "1") + + gtest.Assert(Option.GetAll(), map[string]string{"o1": "111", "o2": "222"}) + gtest.Assert(Option.Get("o1"), "111") + gtest.Assert(Option.Get("o2"), "222") + gtest.Assert(Option.Get("o3", "1"), "1") + gtest.Assert(Option.GetVar("o2", "1").String(), "222") + gtest.Assert(Option.GetVar("o3", "1").String(), "1") + }) } +func Test_Handle(t *testing.T) { + os.Args = []string{"gf", "gf"} + doInit() + gtest.Case(t, func() { + BindHandle("gf", func() { + print("gf test") + }) + RunHandle("gf") + AutoRun() + }) +} From 1f19ed71aae328e4d70e128bd39a3abb811ac000 Mon Sep 17 00:00:00 2001 From: zhangbiao Date: Fri, 14 Jun 2019 10:20:15 +0800 Subject: [PATCH 4/7] update mutex test --- g/internal/mutex/mutex_z_unit_test.go | 87 ++++++++++++++++++++++++--- 1 file changed, 77 insertions(+), 10 deletions(-) diff --git a/g/internal/mutex/mutex_z_unit_test.go b/g/internal/mutex/mutex_z_unit_test.go index 905d6c0d2..cb2567fab 100644 --- a/g/internal/mutex/mutex_z_unit_test.go +++ b/g/internal/mutex/mutex_z_unit_test.go @@ -7,26 +7,93 @@ package mutex_test import ( + "github.com/gogf/gf/g/container/garray" "github.com/gogf/gf/g/internal/mutex" "github.com/gogf/gf/g/test/gtest" "testing" + "time" ) func TestMutex(t *testing.T) { gtest.Case(t, func() { lock := mutex.New() - lock.Lock() - lock.Unlock() gtest.Assert(lock.IsSafe(), true) - safeLock1 := mutex.New(false) - safeLock1.Lock() - safeLock1.Unlock() - gtest.Assert(safeLock1.IsSafe(), true) + lock = mutex.New(false) + gtest.Assert(lock.IsSafe(), true) - unsafeLock1 := mutex.New(true) - unsafeLock1.Lock() - unsafeLock1.Unlock() - gtest.Assert(unsafeLock1.IsSafe(), false) + lock = mutex.New(false, false) + gtest.Assert(lock.IsSafe(), true) + + lock = mutex.New(true, false) + gtest.Assert(lock.IsSafe(), false) + + lock = mutex.New(true, true) + gtest.Assert(lock.IsSafe(), false) + + lock = mutex.New(true) + gtest.Assert(lock.IsSafe(), false) + }) +} + +func TestSafeMutex(t *testing.T) { + gtest.Case(t, func() { + safeLock := mutex.New(false) + array := garray.New() + + go func() { + safeLock.Lock() + array.Append(1) + time.Sleep(100 * time.Millisecond) + array.Append(1) + safeLock.Unlock() + }() + go func() { + time.Sleep(10 * time.Millisecond) + safeLock.Lock() + array.Append(1) + time.Sleep(200 * time.Millisecond) + array.Append(1) + safeLock.Unlock() + }() + time.Sleep(50 * time.Millisecond) + gtest.Assert(array.Len(), 1) + time.Sleep(80 * time.Millisecond) + gtest.Assert(array.Len(), 3) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 3) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 4) + }) +} + +func TestUnsafeMutex(t *testing.T) { + gtest.Case(t, func() { + unsafeLock := mutex.New(true) + array := garray.New() + + go func() { + unsafeLock.Lock() + array.Append(1) + time.Sleep(100 * time.Millisecond) + array.Append(1) + unsafeLock.Unlock() + }() + go func() { + time.Sleep(10 * time.Millisecond) + unsafeLock.Lock() + array.Append(1) + time.Sleep(200 * time.Millisecond) + array.Append(1) + unsafeLock.Unlock() + }() + time.Sleep(50 * time.Millisecond) + gtest.Assert(array.Len(), 2) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 3) + time.Sleep(50 * time.Millisecond) + gtest.Assert(array.Len(), 3) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 4) }) } From 3e2d5e0bdbc7f051aa88ed6037b15012841d9540 Mon Sep 17 00:00:00 2001 From: zhangbiao Date: Fri, 14 Jun 2019 10:28:24 +0800 Subject: [PATCH 5/7] update mutex test2 --- g/internal/mutex/mutex_z_unit_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/g/internal/mutex/mutex_z_unit_test.go b/g/internal/mutex/mutex_z_unit_test.go index cb2567fab..ca7f0c368 100644 --- a/g/internal/mutex/mutex_z_unit_test.go +++ b/g/internal/mutex/mutex_z_unit_test.go @@ -14,7 +14,7 @@ import ( "time" ) -func TestMutex(t *testing.T) { +func TestMutexIsSafe(t *testing.T) { gtest.Case(t, func() { lock := mutex.New() gtest.Assert(lock.IsSafe(), true) From cd30efaaa1fa7338cc51f78bd38a687e4d8d397b Mon Sep 17 00:00:00 2001 From: "zcool321@sina.com" <1234qwer> Date: Sat, 15 Jun 2019 22:11:08 +0800 Subject: [PATCH 6/7] update gcmd test --- g/os/gcmd/gcmd_z_unit_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/g/os/gcmd/gcmd_z_unit_test.go b/g/os/gcmd/gcmd_z_unit_test.go index dda04293e..be548c46a 100644 --- a/g/os/gcmd/gcmd_z_unit_test.go +++ b/g/os/gcmd/gcmd_z_unit_test.go @@ -40,10 +40,13 @@ func Test_Handle(t *testing.T) { os.Args = []string{"gf", "gf"} doInit() gtest.Case(t, func() { + num := 1 BindHandle("gf", func() { - print("gf test") + num += 1 }) RunHandle("gf") + gtest.AssertEQ(num, 2) AutoRun() + gtest.AssertEQ(num, 3) }) } From cb4e36f59190ae4e09da9ec2b07f51c626e53d75 Mon Sep 17 00:00:00 2001 From: "zcool321@sina.com" <1234qwer> Date: Sat, 15 Jun 2019 22:19:37 +0800 Subject: [PATCH 7/7] update rwmutex test2 --- g/internal/rwmutex/rwmutex_z_unit_test.go | 134 +++++++++++++++++++--- 1 file changed, 117 insertions(+), 17 deletions(-) diff --git a/g/internal/rwmutex/rwmutex_z_unit_test.go b/g/internal/rwmutex/rwmutex_z_unit_test.go index d6ada7ada..2c5cccbc1 100644 --- a/g/internal/rwmutex/rwmutex_z_unit_test.go +++ b/g/internal/rwmutex/rwmutex_z_unit_test.go @@ -7,32 +7,132 @@ package rwmutex_test import ( + "github.com/gogf/gf/g/container/garray" "github.com/gogf/gf/g/internal/rwmutex" "github.com/gogf/gf/g/test/gtest" "testing" + "time" ) -func TestRwmutex(t *testing.T) { +func TestRwmutexIsSafe(t *testing.T) { gtest.Case(t, func() { lock := rwmutex.New() - lock.Lock() - lock.Unlock() - lock.RLock() - lock.RUnlock() gtest.Assert(lock.IsSafe(), true) - safeLock1 := rwmutex.New(false) - safeLock1.Lock() - safeLock1.Unlock() - safeLock1.RLock() - safeLock1.RUnlock() - gtest.Assert(safeLock1.IsSafe(), true) + lock = rwmutex.New(false) + gtest.Assert(lock.IsSafe(), true) - unsafeLock1 := rwmutex.New(true) - unsafeLock1.Lock() - unsafeLock1.Unlock() - unsafeLock1.RLock() - unsafeLock1.RUnlock() - gtest.Assert(unsafeLock1.IsSafe(), false) + lock = rwmutex.New(false, false) + gtest.Assert(lock.IsSafe(), true) + + lock = rwmutex.New(true, false) + gtest.Assert(lock.IsSafe(), false) + + lock = rwmutex.New(true, true) + gtest.Assert(lock.IsSafe(), false) + + lock = rwmutex.New(true) + gtest.Assert(lock.IsSafe(), false) + }) +} + +func TestSafeRwmutex(t *testing.T) { + gtest.Case(t, func() { + safeLock := rwmutex.New() + array := garray.New() + + go func() { + safeLock.Lock() + array.Append(1) + time.Sleep(100 * time.Millisecond) + array.Append(1) + safeLock.Unlock() + }() + go func() { + time.Sleep(10 * time.Millisecond) + safeLock.Lock() + array.Append(1) + time.Sleep(200 * time.Millisecond) + array.Append(1) + safeLock.Unlock() + }() + time.Sleep(50 * time.Millisecond) + gtest.Assert(array.Len(), 1) + time.Sleep(80 * time.Millisecond) + gtest.Assert(array.Len(), 3) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 3) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 4) + }) +} + +func TestSafeReaderRwmutex(t *testing.T) { + gtest.Case(t, func() { + safeLock := rwmutex.New() + array := garray.New() + + go func() { + safeLock.RLock() + array.Append(1) + time.Sleep(100 * time.Millisecond) + array.Append(1) + safeLock.RUnlock() + }() + go func() { + time.Sleep(10 * time.Millisecond) + safeLock.RLock() + array.Append(1) + time.Sleep(200 * time.Millisecond) + array.Append(1) + time.Sleep(100 * time.Millisecond) + array.Append(1) + safeLock.RUnlock() + }() + go func() { + time.Sleep(50 * time.Millisecond) + safeLock.Lock() + array.Append(1) + safeLock.Unlock() + }() + time.Sleep(50 * time.Millisecond) + gtest.Assert(array.Len(), 2) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 3) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 4) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 6) + }) +} + +func TestUnsafeRwmutex(t *testing.T) { + gtest.Case(t, func() { + unsafeLock := rwmutex.New(true) + array := garray.New() + + go func() { + unsafeLock.Lock() + array.Append(1) + time.Sleep(100 * time.Millisecond) + array.Append(1) + unsafeLock.Unlock() + }() + go func() { + time.Sleep(10 * time.Millisecond) + unsafeLock.Lock() + array.Append(1) + time.Sleep(200 * time.Millisecond) + array.Append(1) + unsafeLock.Unlock() + }() + time.Sleep(50 * time.Millisecond) + gtest.Assert(array.Len(), 2) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 3) + time.Sleep(50 * time.Millisecond) + gtest.Assert(array.Len(), 3) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 4) }) }