From e38c455252a402da7a96337758eba99af7c8ee9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E9=AA=9E?= Date: Mon, 11 Jul 2022 19:34:40 +0800 Subject: [PATCH] Improve the code coverage of the gpool, gqueue, gring module (#1987) --- container/gpool/gpool_z_unit_test.go | 24 ++++++++++++++++ container/gqueue/gqueue_z_unit_test.go | 8 ++++++ container/gring/gring_z_unit_test.go | 40 ++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/container/gpool/gpool_z_unit_test.go b/container/gpool/gpool_z_unit_test.go index 759bc9dce..c7dca3388 100644 --- a/container/gpool/gpool_z_unit_test.go +++ b/container/gpool/gpool_z_unit_test.go @@ -85,6 +85,7 @@ func Test_Gpool(t *testing.T) { assertIndex = 0 p2.Close() time.Sleep(3 * time.Second) + t.AssertNE(p2.Put(1), nil) }) gtest.C(t, func(t *gtest.T) { @@ -95,4 +96,27 @@ func Test_Gpool(t *testing.T) { t.Assert(err3, errors.New("pool is empty")) t.Assert(v3, nil) }) + + gtest.C(t, func(t *gtest.T) { + p := gpool.New(time.Millisecond*200, nil, func(i interface{}) {}) + p.Put(1) + time.Sleep(time.Millisecond * 100) + p.Put(2) + time.Sleep(time.Millisecond * 200) + }) + + gtest.C(t, func(t *gtest.T) { + s := make([]int, 0) + p := gpool.New(time.Millisecond*200, nil, func(i interface{}) { + s = append(s, i.(int)) + }) + for i := 0; i < 5; i++ { + p.Put(i) + time.Sleep(time.Millisecond * 50) + } + val, err := p.Get() + t.Assert(val, 2) + t.AssertNil(err) + t.Assert(p.Size(), 2) + }) } diff --git a/container/gqueue/gqueue_z_unit_test.go b/container/gqueue/gqueue_z_unit_test.go index f9741ff4c..fb171d4cc 100644 --- a/container/gqueue/gqueue_z_unit_test.go +++ b/container/gqueue/gqueue_z_unit_test.go @@ -62,4 +62,12 @@ func TestQueue_Close(t *testing.T) { t.Assert(q1.Len(), 2) q1.Close() }) + gtest.C(t, func(t *gtest.T) { + q1 := gqueue.New(2) + q1.Push(1) + q1.Push(2) + time.Sleep(time.Millisecond) + t.Assert(q1.Len(), 2) + q1.Close() + }) } diff --git a/container/gring/gring_z_unit_test.go b/container/gring/gring_z_unit_test.go index 55007ce26..6d972de63 100644 --- a/container/gring/gring_z_unit_test.go +++ b/container/gring/gring_z_unit_test.go @@ -183,3 +183,43 @@ func Test_Issue1394(t *testing.T) { }) } + +func TestRing_RLockIteratorNext(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + r := gring.New(10) + for i := 0; i < 10; i++ { + r.Set(i).Next() + } + + iterVal := 0 + r.RLockIteratorNext(func(value interface{}) bool { + if value.(int) == 0 { + iterVal = value.(int) + return false + } + return true + }) + + t.Assert(iterVal, 0) + }) +} + +func TestRing_RLockIteratorPrev(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + r := gring.New(10) + for i := 0; i < 10; i++ { + r.Set(i).Next() + } + + iterVal := 0 + r.RLockIteratorPrev(func(value interface{}) bool { + if value.(int) == 0 { + iterVal = value.(int) + return false + } + return true + }) + + t.Assert(iterVal, 0) + }) +}