gf/container/gpool/gpool_z_unit_test.go

124 lines
2.6 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2019-07-09 15:41:50 +08:00
//
// 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.
2019-06-13 11:42:51 +08:00
package gpool_test
import (
"errors"
"testing"
"time"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/container/gpool"
2021-11-13 23:23:55 +08:00
"github.com/gogf/gf/v2/frame/g"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/test/gtest"
2019-06-13 11:42:51 +08:00
)
var nf gpool.NewFunc = func() (i interface{}, e error) {
return "hello", nil
}
var assertIndex int = 0
2019-06-13 11:42:51 +08:00
var ef gpool.ExpireFunc = func(i interface{}) {
assertIndex++
2020-03-20 08:49:40 +08:00
gtest.Assert(i, assertIndex)
2019-06-13 11:42:51 +08:00
}
func Test_Gpool(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-13 11:42:51 +08:00
//
2021-11-13 23:23:55 +08:00
// expire = 0
2019-06-13 11:42:51 +08:00
p1 := gpool.New(0, nf)
p1.Put(1)
p1.Put(2)
time.Sleep(1 * time.Second)
2021-11-13 23:23:55 +08:00
// test won't be timeout
2019-06-13 11:42:51 +08:00
v1, err1 := p1.Get()
2020-03-19 22:56:12 +08:00
t.Assert(err1, nil)
t.AssertIN(v1, g.Slice{1, 2})
2021-11-13 23:23:55 +08:00
// test clear
2019-06-13 11:42:51 +08:00
p1.Clear()
2020-03-19 22:56:12 +08:00
t.Assert(p1.Size(), 0)
2021-11-13 23:23:55 +08:00
// test newFunc
2019-06-13 11:42:51 +08:00
v1, err1 = p1.Get()
2020-03-19 22:56:12 +08:00
t.Assert(err1, nil)
t.Assert(v1, "hello")
2021-11-13 23:23:55 +08:00
// put data again
2019-06-13 11:42:51 +08:00
p1.Put(3)
p1.Put(4)
v1, err1 = p1.Get()
2020-03-19 22:56:12 +08:00
t.Assert(err1, nil)
t.AssertIN(v1, g.Slice{3, 4})
2021-11-13 23:23:55 +08:00
// test close
2019-06-13 11:42:51 +08:00
p1.Close()
v1, err1 = p1.Get()
2020-03-19 22:56:12 +08:00
t.Assert(err1, nil)
t.Assert(v1, "hello")
2019-06-13 11:42:51 +08:00
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-13 11:42:51 +08:00
//
2021-11-13 23:23:55 +08:00
// expire > 0
p2 := gpool.New(2*time.Second, nil, ef)
2019-06-13 11:42:51 +08:00
for index := 0; index < 10; index++ {
p2.Put(index)
}
2020-03-19 22:56:12 +08:00
t.Assert(p2.Size(), 10)
2019-06-13 11:42:51 +08:00
v2, err2 := p2.Get()
2020-03-19 22:56:12 +08:00
t.Assert(err2, nil)
t.Assert(v2, 0)
2021-11-13 23:23:55 +08:00
// test timeout expireFunc
2019-06-13 11:42:51 +08:00
time.Sleep(3 * time.Second)
v2, err2 = p2.Get()
2020-03-19 22:56:12 +08:00
t.Assert(err2, errors.New("pool is empty"))
t.Assert(v2, nil)
2021-11-13 23:23:55 +08:00
// test close expireFunc
2019-06-13 11:42:51 +08:00
for index := 0; index < 10; index++ {
p2.Put(index)
}
2020-03-19 22:56:12 +08:00
t.Assert(p2.Size(), 10)
2019-06-13 11:42:51 +08:00
v2, err2 = p2.Get()
2020-03-19 22:56:12 +08:00
t.Assert(err2, nil)
t.Assert(v2, 0)
2019-06-13 11:42:51 +08:00
assertIndex = 0
p2.Close()
time.Sleep(3 * time.Second)
t.AssertNE(p2.Put(1), nil)
2019-06-13 11:42:51 +08:00
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-13 11:42:51 +08:00
//
2021-11-13 23:23:55 +08:00
// expire < 0
2019-06-13 11:42:51 +08:00
p3 := gpool.New(-1, nil)
v3, err3 := p3.Get()
2020-03-19 22:56:12 +08:00
t.Assert(err3, errors.New("pool is empty"))
t.Assert(v3, nil)
2019-06-13 11:42:51 +08:00
})
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)
})
2019-06-13 11:42:51 +08:00
}