-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool_test.go
178 lines (135 loc) · 3.88 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package pg_test
import (
"testing"
"time"
. "gopkg.in/check.v1"
"gopkg.in/pg.v4"
)
func TestStatementTimeout(t *testing.T) {
opt := pgOptions()
opt.Params = map[string]interface{}{
"statement_timeout": 1000,
}
db := pg.Connect(opt)
defer db.Close()
_, err := db.Exec("SELECT pg_sleep(60)")
if err == nil {
t.Fatalf("err is nil")
}
if err.Error() != "ERROR #57014 canceling statement due to statement timeout: " {
t.Fatalf("got %q", err.Error())
}
if db.Pool().Len() != 1 || db.Pool().FreeLen() != 1 {
t.Fatalf("pool is empty")
}
}
var _ = Suite(&PoolTest{})
type PoolTest struct {
db *pg.DB
}
func (t *PoolTest) SetUpTest(c *C) {
opt := pgOptions()
opt.IdleTimeout = time.Second
t.db = pg.Connect(opt)
}
func (t *PoolTest) TearDownTest(c *C) {
_ = t.db.Close()
}
func (t *PoolTest) TestPoolReusesConnection(c *C) {
for i := 0; i < 100; i++ {
_, err := t.db.Exec("SELECT 'test_pool_reuses_connection'")
c.Assert(err, IsNil)
}
c.Assert(t.db.Pool().Len(), Equals, 1)
c.Assert(t.db.Pool().FreeLen(), Equals, 1)
}
func (t *PoolTest) TestPoolMaxSize(c *C) {
N := 1000
perform(N, func(int) {
_, err := t.db.Exec("SELECT 'test_pool_max_size'")
c.Assert(err, IsNil)
})
c.Assert(t.db.Pool().Len(), Equals, 10)
c.Assert(t.db.Pool().FreeLen(), Equals, 10)
}
func (t *PoolTest) TestCloseClosesAllConnections(c *C) {
ln, err := t.db.Listen("test_channel")
c.Assert(err, IsNil)
wait := make(chan struct{}, 2)
go func() {
wait <- struct{}{}
_, _, err := ln.Receive()
c.Assert(err, ErrorMatches, `^(.*use of closed network connection|EOF)$`)
wait <- struct{}{}
}()
select {
case <-wait:
// ok
case <-time.After(3 * time.Second):
c.Fatal("timeout")
}
c.Assert(t.db.Close(), IsNil)
select {
case <-wait:
// ok
case <-time.After(3 * time.Second):
c.Fatal("timeout")
}
c.Assert(t.db.Pool().Len(), Equals, 0)
c.Assert(t.db.Pool().FreeLen(), Equals, 0)
}
func (t *PoolTest) TestClosedDB(c *C) {
c.Assert(t.db.Close(), IsNil)
c.Assert(t.db.Pool().Len(), Equals, 0)
c.Assert(t.db.Pool().FreeLen(), Equals, 0)
err := t.db.Close()
c.Assert(err, Not(IsNil))
c.Assert(err.Error(), Equals, "pg: database is closed")
_, err = t.db.Exec("SELECT 'test_closed_db'")
c.Assert(err, Not(IsNil))
c.Assert(err.Error(), Equals, "pg: database is closed")
}
func (t *PoolTest) TestClosedListener(c *C) {
ln, err := t.db.Listen("test_channel")
c.Assert(err, IsNil)
c.Assert(t.db.Pool().Len(), Equals, 1)
c.Assert(t.db.Pool().FreeLen(), Equals, 0)
c.Assert(ln.Close(), IsNil)
c.Assert(t.db.Pool().Len(), Equals, 0)
c.Assert(t.db.Pool().FreeLen(), Equals, 0)
err = ln.Close()
c.Assert(err, Not(IsNil))
c.Assert(err.Error(), Equals, "pg: listener is closed")
_, _, err = ln.ReceiveTimeout(time.Second)
c.Assert(err, Not(IsNil))
c.Assert(err.Error(), Equals, "pg: listener is closed")
}
func (t *PoolTest) TestClosedTx(c *C) {
tx, err := t.db.Begin()
c.Assert(err, IsNil)
c.Assert(t.db.Pool().Len(), Equals, 1)
c.Assert(t.db.Pool().FreeLen(), Equals, 0)
c.Assert(tx.Rollback(), IsNil)
c.Assert(t.db.Pool().Len(), Equals, 1)
c.Assert(t.db.Pool().FreeLen(), Equals, 1)
err = tx.Rollback()
c.Assert(err, Not(IsNil))
c.Assert(err.Error(), Equals, "pg: transaction has already been committed or rolled back")
_, err = tx.Exec("SELECT 'test_closed_tx'")
c.Assert(err, Not(IsNil))
c.Assert(err.Error(), Equals, "pg: transaction has already been committed or rolled back")
}
func (t *PoolTest) TestClosedStmt(c *C) {
stmt, err := t.db.Prepare("SELECT $1::int")
c.Assert(err, IsNil)
c.Assert(t.db.Pool().Len(), Equals, 1)
c.Assert(t.db.Pool().FreeLen(), Equals, 0)
c.Assert(stmt.Close(), IsNil)
c.Assert(t.db.Pool().Len(), Equals, 1)
c.Assert(t.db.Pool().FreeLen(), Equals, 1)
err = stmt.Close()
c.Assert(err, Not(IsNil))
c.Assert(err.Error(), Equals, "pg: statement is closed")
_, err = stmt.Exec(1)
c.Assert(err.Error(), Equals, "pg: statement is closed")
}