-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathpool_test.go
75 lines (61 loc) · 1.71 KB
/
pool_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package client
import (
"context"
"fmt"
"net"
"strings"
"testing"
"time"
"github.com/go-mysql-org/go-mysql/test_util"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type poolTestSuite struct {
suite.Suite
port string
}
func TestPoolSuite(t *testing.T) {
segs := strings.Split(*test_util.MysqlPort, ",")
for _, seg := range segs {
suite.Run(t, &poolTestSuite{port: seg})
}
}
func (s *poolTestSuite) TestPool_Close() {
addr := fmt.Sprintf("%s:%s", *test_util.MysqlHost, s.port)
pool, err := NewPoolWithOptions(addr, *testUser, *testPassword, "",
WithPoolLimits(5, 10, 5),
)
require.NoError(s.T(), err)
conn, err := pool.GetConn(context.Background())
require.NoError(s.T(), err)
err = conn.Ping()
require.NoError(s.T(), err)
pool.PutConn(conn)
pool.Close()
var poolStats ConnectionStats
pool.GetStats(&poolStats)
require.Equal(s.T(), 0, poolStats.IdleCount)
require.Len(s.T(), pool.readyConnection, 0)
_, err = pool.GetConn(context.Background())
require.Error(s.T(), err)
}
func (s *poolTestSuite) TestPool_WrongPassword() {
addr := fmt.Sprintf("%s:%s", *test_util.MysqlHost, s.port)
_, err := NewPoolWithOptions(addr, *testUser, "wrong-password", "",
WithPoolLimits(5, 10, 5),
WithNewPoolPingTimeout(time.Second),
)
require.ErrorContains(s.T(), err, "ERROR 1045 (28000): Access denied for user")
}
func (s *poolTestSuite) TestPool_WrongAddr() {
l, err := net.Listen("tcp4", "127.0.0.1:0")
require.NoError(s.T(), err)
laddr, ok := l.Addr().(*net.TCPAddr)
require.True(s.T(), ok)
_ = l.Close()
_, err = NewPoolWithOptions(laddr.String(), *testUser, *testPassword, "",
WithPoolLimits(5, 10, 5),
WithNewPoolPingTimeout(time.Second),
)
require.Error(s.T(), err)
}