123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- package pool
- import (
- "context"
- "io"
- "testing"
- "time"
- xtime "go-common/library/time"
- "github.com/stretchr/testify/assert"
- )
- func TestListGetPut(t *testing.T) {
- // new pool
- config := &Config{
- Active: 1,
- Idle: 1,
- IdleTimeout: xtime.Duration(90 * time.Second),
- WaitTimeout: xtime.Duration(10 * time.Millisecond),
- Wait: false,
- }
- pool := NewList(config)
- pool.New = func(ctx context.Context) (io.Closer, error) {
- return &closer{}, nil
- }
- // test Get Put
- conn, err := pool.Get(context.TODO())
- assert.Nil(t, err)
- c1 := connection{pool: pool, c: conn}
- c1.HandleNormal()
- c1.Close()
- }
- func TestListPut(t *testing.T) {
- var id = 0
- type connID struct {
- io.Closer
- id int
- }
- config := &Config{
- Active: 1,
- Idle: 1,
- IdleTimeout: xtime.Duration(1 * time.Second),
- // WaitTimeout: xtime.Duration(10 * time.Millisecond),
- Wait: false,
- }
- pool := NewList(config)
- pool.New = func(ctx context.Context) (io.Closer, error) {
- id = id + 1
- return &connID{id: id, Closer: &closer{}}, nil
- }
- // test Put(ctx, conn, true)
- conn, err := pool.Get(context.TODO())
- assert.Nil(t, err)
- conn1 := conn.(*connID)
- // Put(ctx, conn, true) drop the connection.
- pool.Put(context.TODO(), conn, true)
- conn, err = pool.Get(context.TODO())
- assert.Nil(t, err)
- conn2 := conn.(*connID)
- assert.NotEqual(t, conn1.id, conn2.id)
- }
- func TestListIdleTimeout(t *testing.T) {
- var id = 0
- type connID struct {
- io.Closer
- id int
- }
- config := &Config{
- Active: 1,
- Idle: 1,
- // conn timeout
- IdleTimeout: xtime.Duration(1 * time.Millisecond),
- }
- pool := NewList(config)
- pool.New = func(ctx context.Context) (io.Closer, error) {
- id = id + 1
- return &connID{id: id, Closer: &closer{}}, nil
- }
- // test Put(ctx, conn, true)
- conn, err := pool.Get(context.TODO())
- assert.Nil(t, err)
- conn1 := conn.(*connID)
- // Put(ctx, conn, true) drop the connection.
- pool.Put(context.TODO(), conn, false)
- time.Sleep(5 * time.Millisecond)
- // idletimeout and get new conn
- conn, err = pool.Get(context.TODO())
- assert.Nil(t, err)
- conn2 := conn.(*connID)
- assert.NotEqual(t, conn1.id, conn2.id)
- }
- func TestListContextTimeout(t *testing.T) {
- // new pool
- config := &Config{
- Active: 1,
- Idle: 1,
- IdleTimeout: xtime.Duration(90 * time.Second),
- WaitTimeout: xtime.Duration(10 * time.Millisecond),
- Wait: false,
- }
- pool := NewList(config)
- pool.New = func(ctx context.Context) (io.Closer, error) {
- return &closer{}, nil
- }
- // test context timeout
- ctx, cancel := context.WithTimeout(context.TODO(), 100*time.Millisecond)
- defer cancel()
- conn, err := pool.Get(ctx)
- assert.Nil(t, err)
- _, err = pool.Get(ctx)
- // context timeout error
- assert.NotNil(t, err)
- pool.Put(context.TODO(), conn, false)
- _, err = pool.Get(ctx)
- assert.Nil(t, err)
- }
- func TestListPoolExhausted(t *testing.T) {
- // test pool exhausted
- config := &Config{
- Active: 1,
- Idle: 1,
- IdleTimeout: xtime.Duration(90 * time.Second),
- // WaitTimeout: xtime.Duration(10 * time.Millisecond),
- Wait: false,
- }
- pool := NewList(config)
- pool.New = func(ctx context.Context) (io.Closer, error) {
- return &closer{}, nil
- }
- ctx, cancel := context.WithTimeout(context.TODO(), 100*time.Millisecond)
- defer cancel()
- conn, err := pool.Get(context.TODO())
- assert.Nil(t, err)
- _, err = pool.Get(ctx)
- // config active == 1, so no avaliable conns make connection exhausted.
- assert.NotNil(t, err)
- pool.Put(context.TODO(), conn, false)
- _, err = pool.Get(ctx)
- assert.Nil(t, err)
- }
- func TestListStaleClean(t *testing.T) {
- var id = 0
- type connID struct {
- io.Closer
- id int
- }
- config := &Config{
- Active: 1,
- Idle: 1,
- IdleTimeout: xtime.Duration(1 * time.Second),
- // WaitTimeout: xtime.Duration(10 * time.Millisecond),
- Wait: false,
- }
- pool := NewList(config)
- pool.New = func(ctx context.Context) (io.Closer, error) {
- id = id + 1
- return &connID{id: id, Closer: &closer{}}, nil
- }
- conn, err := pool.Get(context.TODO())
- assert.Nil(t, err)
- conn1 := conn.(*connID)
- pool.Put(context.TODO(), conn, false)
- conn, err = pool.Get(context.TODO())
- assert.Nil(t, err)
- conn2 := conn.(*connID)
- assert.Equal(t, conn1.id, conn2.id)
- pool.Put(context.TODO(), conn, false)
- // sleep more than idleTimeout
- time.Sleep(2 * time.Second)
- conn, err = pool.Get(context.TODO())
- assert.Nil(t, err)
- conn3 := conn.(*connID)
- assert.NotEqual(t, conn1.id, conn3.id)
- }
- func BenchmarkList1(b *testing.B) {
- config := &Config{
- Active: 30,
- Idle: 30,
- IdleTimeout: xtime.Duration(90 * time.Second),
- WaitTimeout: xtime.Duration(10 * time.Millisecond),
- Wait: false,
- }
- pool := NewList(config)
- pool.New = func(ctx context.Context) (io.Closer, error) {
- return &closer{}, nil
- }
- b.ResetTimer()
- b.RunParallel(func(pb *testing.PB) {
- for pb.Next() {
- conn, err := pool.Get(context.TODO())
- if err != nil {
- b.Error(err)
- continue
- }
- c1 := connection{pool: pool, c: conn}
- c1.HandleQuick()
- c1.Close()
- }
- })
- }
- func BenchmarkList2(b *testing.B) {
- config := &Config{
- Active: 30,
- Idle: 30,
- IdleTimeout: xtime.Duration(90 * time.Second),
- WaitTimeout: xtime.Duration(10 * time.Millisecond),
- Wait: false,
- }
- pool := NewList(config)
- pool.New = func(ctx context.Context) (io.Closer, error) {
- return &closer{}, nil
- }
- b.ResetTimer()
- b.RunParallel(func(pb *testing.PB) {
- for pb.Next() {
- conn, err := pool.Get(context.TODO())
- if err != nil {
- b.Error(err)
- continue
- }
- c1 := connection{pool: pool, c: conn}
- c1.HandleNormal()
- c1.Close()
- }
- })
- }
- func BenchmarkPool3(b *testing.B) {
- config := &Config{
- Active: 30,
- Idle: 30,
- IdleTimeout: xtime.Duration(90 * time.Second),
- WaitTimeout: xtime.Duration(10 * time.Millisecond),
- Wait: false,
- }
- pool := NewList(config)
- pool.New = func(ctx context.Context) (io.Closer, error) {
- return &closer{}, nil
- }
- b.ResetTimer()
- b.RunParallel(func(pb *testing.PB) {
- for pb.Next() {
- conn, err := pool.Get(context.TODO())
- if err != nil {
- b.Error(err)
- continue
- }
- c1 := connection{pool: pool, c: conn}
- c1.HandleSlow()
- c1.Close()
- }
- })
- }
- func BenchmarkList4(b *testing.B) {
- config := &Config{
- Active: 30,
- Idle: 30,
- IdleTimeout: xtime.Duration(90 * time.Second),
- // WaitTimeout: xtime.Duration(10 * time.Millisecond),
- Wait: false,
- }
- pool := NewList(config)
- pool.New = func(ctx context.Context) (io.Closer, error) {
- return &closer{}, nil
- }
- b.ResetTimer()
- b.RunParallel(func(pb *testing.PB) {
- for pb.Next() {
- conn, err := pool.Get(context.TODO())
- if err != nil {
- b.Error(err)
- continue
- }
- c1 := connection{pool: pool, c: conn}
- c1.HandleSlow()
- c1.Close()
- }
- })
- }
- func BenchmarkList5(b *testing.B) {
- config := &Config{
- Active: 30,
- Idle: 30,
- IdleTimeout: xtime.Duration(90 * time.Second),
- // WaitTimeout: xtime.Duration(10 * time.Millisecond),
- Wait: true,
- }
- pool := NewList(config)
- pool.New = func(ctx context.Context) (io.Closer, error) {
- return &closer{}, nil
- }
- b.ResetTimer()
- b.RunParallel(func(pb *testing.PB) {
- for pb.Next() {
- conn, err := pool.Get(context.TODO())
- if err != nil {
- b.Error(err)
- continue
- }
- c1 := connection{pool: pool, c: conn}
- c1.HandleSlow()
- c1.Close()
- }
- })
- }
|