mihomo/component/pool/pool_test.go

74 lines
1.1 KiB
Go
Raw Permalink Normal View History

package pool
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
2022-04-24 02:07:57 +08:00
func lg() Factory[int] {
initial := -1
2022-04-24 02:07:57 +08:00
return func(context.Context) (int, error) {
initial++
return initial, nil
}
}
func TestPool_Basic(t *testing.T) {
g := lg()
2022-04-24 02:07:57 +08:00
pool := New[int](g)
elm, _ := pool.Get()
2022-04-24 02:07:57 +08:00
assert.Equal(t, 0, elm)
pool.Put(elm)
elm, _ = pool.Get()
2022-04-24 02:07:57 +08:00
assert.Equal(t, 0, elm)
elm, _ = pool.Get()
2022-04-24 02:07:57 +08:00
assert.Equal(t, 1, elm)
}
func TestPool_MaxSize(t *testing.T) {
g := lg()
size := 5
2022-04-24 02:07:57 +08:00
pool := New[int](g, WithSize[int](size))
2022-04-24 02:07:57 +08:00
var items []int
for i := 0; i < size; i++ {
item, _ := pool.Get()
items = append(items, item)
}
extra, _ := pool.Get()
2022-04-24 02:07:57 +08:00
assert.Equal(t, size, extra)
for _, item := range items {
pool.Put(item)
}
pool.Put(extra)
for _, item := range items {
elm, _ := pool.Get()
2022-04-24 02:07:57 +08:00
assert.Equal(t, item, elm)
}
}
func TestPool_MaxAge(t *testing.T) {
g := lg()
2022-04-24 02:07:57 +08:00
pool := New[int](g, WithAge[int](20))
elm, _ := pool.Get()
pool.Put(elm)
elm, _ = pool.Get()
2022-04-24 02:07:57 +08:00
assert.Equal(t, 0, elm)
pool.Put(elm)
time.Sleep(time.Millisecond * 22)
elm, _ = pool.Get()
2022-04-24 02:07:57 +08:00
assert.Equal(t, 1, elm)
}