-
Notifications
You must be signed in to change notification settings - Fork 49
/
pool_test.go
191 lines (137 loc) · 4.33 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
179
180
181
182
183
184
185
186
187
188
189
190
191
package gremgo
import (
"testing"
"time"
)
func TestPurge(t *testing.T) {
n := time.Now()
// invalid has timedout and should be cleaned up
invalid := &idleConnection{t: n.Add(-30 * time.Second), pc: &PooledConnection{Client: &Client{}}}
// valid has not yet timed out and should remain in the idle pool
valid := &idleConnection{t: n.Add(30 * time.Second), pc: &PooledConnection{Client: &Client{}}}
// Pool has a 30 second timeout and an idle connection slice containing both
// the invalid and valid idle connections
p := &Pool{IdleTimeout: time.Second * 30, idle: []*idleConnection{invalid, valid}}
if len(p.idle) != 2 {
t.Errorf("Expected 2 idle connections, got %d", len(p.idle))
}
p.purge()
if len(p.idle) != 1 {
t.Errorf("Expected 1 idle connection after purge, got %d", len(p.idle))
}
if p.idle[0].t != valid.t {
t.Error("Expected the valid connection to remain in idle pool")
}
}
func TestPurgeErrorClosedConnection(t *testing.T) {
n := time.Now()
p := &Pool{IdleTimeout: time.Second * 30}
valid := &idleConnection{t: n.Add(30 * time.Second), pc: &PooledConnection{Client: &Client{}}}
client := &Client{}
closed := &idleConnection{t: n.Add(30 * time.Second), pc: &PooledConnection{Pool: p, Client: client}}
idle := []*idleConnection{valid, closed}
p.idle = idle
// Simulate error
closed.pc.Client.Errored = true
if len(p.idle) != 2 {
t.Errorf("Expected 2 idle connections, got %d", len(p.idle))
}
p.purge()
if len(p.idle) != 1 {
t.Errorf("Expected 1 idle connection after purge, got %d", len(p.idle))
}
if p.idle[0] != valid {
t.Error("Expected valid connection to remain in pool")
}
}
func TestPooledConnectionClose(t *testing.T) {
pool := &Pool{}
pc := &PooledConnection{Pool: pool}
if len(pool.idle) != 0 {
t.Errorf("Expected 0 idle connection, got %d", len(pool.idle))
}
pc.Close()
if len(pool.idle) != 1 {
t.Errorf("Expected 1 idle connection, got %d", len(pool.idle))
}
idled := pool.idle[0]
if idled == nil {
t.Error("Expected to get connection")
}
if idled.t.IsZero() {
t.Error("Expected an idled time")
}
}
func TestFirst(t *testing.T) {
n := time.Now()
pool := &Pool{MaxActive: 1, IdleTimeout: 30 * time.Second}
idled := []*idleConnection{
&idleConnection{t: n.Add(-45 * time.Second), pc: &PooledConnection{Pool: pool, Client: &Client{}}}, // expired
&idleConnection{t: n.Add(-45 * time.Second), pc: &PooledConnection{Pool: pool, Client: &Client{}}}, // expired
&idleConnection{pc: &PooledConnection{Pool: pool, Client: &Client{}}}, // valid
}
pool.idle = idled
if len(pool.idle) != 3 {
t.Errorf("Expected 3 idle connection, got %d", len(pool.idle))
}
// Get should return the last idle connection and purge the others
c := pool.first()
if c != pool.idle[0] {
t.Error("Expected to get first connection in idle slice")
}
// Empty pool should return nil
emptyPool := &Pool{}
c = emptyPool.first()
if c != nil {
t.Errorf("Expected nil, got %T", c)
}
}
func TestGetAndDial(t *testing.T) {
n := time.Now()
pool := &Pool{IdleTimeout: time.Second * 30}
invalid := &idleConnection{t: n.Add(-30 * time.Second), pc: &PooledConnection{Pool: pool, Client: &Client{}}}
idle := []*idleConnection{invalid}
pool.idle = idle
client := &Client{}
pool.Dial = func() (*Client, error) {
return client, nil
}
if len(pool.idle) != 1 {
t.Error("Expected 1 idle connection")
}
if pool.idle[0] != invalid {
t.Error("Expected invalid connection")
}
conn, err := pool.Get()
if err != nil {
t.Error(err)
}
if len(pool.idle) != 0 {
t.Errorf("Expected 0 idle connections, got %d", len(pool.idle))
}
if conn.Client != client {
t.Error("Expected correct client to be returned")
}
if pool.active != 1 {
t.Errorf("Expected 1 active connection, got %d", pool.active)
}
// Close the connection and ensure it was returned to the idle pool
conn.Close()
if len(pool.idle) != 1 {
t.Error("Expected connection to be returned to idle pool")
}
if pool.active != 0 {
t.Errorf("Expected 0 active connections, got %d", pool.active)
}
// Get a new connection and ensure that it is the now idling connection
conn, err = pool.Get()
if err != nil {
t.Error(err)
}
if conn.Client != client {
t.Error("Expected the same connection to be reused")
}
if pool.active != 1 {
t.Errorf("Expected 1 active connection, got %d", pool.active)
}
}