forked from schwartzmx/gremgo-neptune
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool_test.go
198 lines (149 loc) · 4.53 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
192
193
194
195
196
197
198
package gremgo
import (
"fmt"
"testing"
"time"
)
var dummyDialFunc func() (*Client, error)
func TestConnectionCleaner(t *testing.T) {
n := time.Now()
// invalid has timedout and should be cleaned up
invalid := &conn{Client: &Client{}, t: n.Add(-1030 * time.Millisecond)}
// valid has not yet timed out and should remain in the freeConns pool
valid := &conn{Client: &Client{}, t: n.Add(1030 * time.Millisecond)}
// Pool has a 30 second timeout and an freeConns connection slice containing both
// the invalid and valid freeConns connections
p := NewPool(dummyDialFunc)
defer p.Close()
p.MaxLifetime = time.Second * 1
p.freeConns = []*conn{invalid, valid}
if len(p.freeConns) != 2 {
t.Errorf("Expected 2 freeConns connections, got %d", len(p.freeConns))
}
p.mu.Lock()
p.open = len(p.freeConns)
p.startCleanerLocked()
p.mu.Unlock()
time.Sleep(1010 * time.Millisecond)
if len(p.freeConns) != 1 {
t.Errorf("Expected 1 freeConns connection after clean, got %d", len(p.freeConns))
for _, pc := range p.freeConns {
fmt.Println(pc.t)
}
}
if p.freeConns[0].t != valid.t {
t.Error("Expected the valid connection to remain in freeConns pool")
}
}
func TestPurgeErrorClosedConnection(t *testing.T) {
n := time.Now()
p := NewPool(dummyDialFunc)
defer p.Close()
p.MaxLifetime = time.Second * 1
valid := &conn{Client: &Client{}, t: n.Add(1030 * time.Millisecond)}
client := &Client{}
closed := &conn{Pool: p, Client: client, t: n.Add(1030 * time.Millisecond)}
freeConns := []*conn{valid, closed}
p.freeConns = freeConns
// Simulate error
closed.Client.Errored = true
if len(p.freeConns) != 2 {
t.Errorf("Expected 2 idle connections, got %d", len(p.freeConns))
}
p.mu.Lock()
p.open = len(p.freeConns)
p.startCleanerLocked()
p.mu.Unlock()
time.Sleep(1010 * time.Millisecond)
if len(p.freeConns) != 1 {
t.Errorf("Expected 1 freeConns connection after clean, got %d", len(p.freeConns))
}
if p.freeConns[0] != valid {
t.Error("Expected valid connection to remain in pool")
}
}
func TestPooledConnectionClose(t *testing.T) {
pool := NewPool(dummyDialFunc)
defer pool.Close()
pc := &conn{Pool: pool}
if len(pool.freeConns) != 0 {
t.Errorf("Expected 0 freeConns connection, got %d", len(pool.freeConns))
}
pool.putConn(pc, nil)
if len(pool.freeConns) != 1 {
t.Errorf("Expected 1 freeConns connection, got %d", len(pool.freeConns))
}
freeConnsd := pool.freeConns[0]
if freeConnsd == nil {
t.Error("Expected to get connection")
}
}
func TestFirst(t *testing.T) {
n := time.Now()
pool := NewPool(dummyDialFunc)
defer pool.Close()
pool.MaxOpen = 1
pool.MaxLifetime = 30 * time.Millisecond
freeConnsd := []*conn{
&conn{Pool: pool, Client: &Client{}, t: n.Add(-45 * time.Millisecond)}, // expired
&conn{Pool: pool, Client: &Client{}, t: n.Add(-45 * time.Millisecond)}, // expired
&conn{Pool: pool, Client: &Client{}}, // valid
}
pool.freeConns = freeConnsd
if len(pool.freeConns) != 3 {
t.Errorf("Expected 3 freeConns connection, got %d", len(pool.freeConns))
}
// Empty pool should return nil
emptyPool := &Pool{}
if len(emptyPool.freeConns) != 0 {
t.Errorf("Expected nil, got %d", len(emptyPool.freeConns))
}
}
func TestGetAndDial(t *testing.T) {
n := time.Now()
client := &Client{}
pool := NewPool(func() (*Client, error) {
return client, nil
})
defer pool.Close()
pool.MaxLifetime = time.Millisecond * 30
invalid := &conn{Pool: pool, Client: &Client{}, t: n.Add(-30 * time.Millisecond)}
freeConns := []*conn{invalid}
pool.freeConns = freeConns
if len(pool.freeConns) != 1 {
t.Error("Expected 1 freeConns connection")
}
if pool.freeConns[0] != invalid {
t.Error("Expected invalid connection")
}
pool.mu.Lock()
pool.startCleanerLocked()
pool.mu.Unlock()
time.Sleep(1010 * time.Millisecond)
conn, err := pool.conn()
if err != nil {
t.Error(err)
}
if len(pool.freeConns) != 0 {
t.Errorf("Expected 0 freeConns connections, got %d", len(pool.freeConns))
}
if conn.Client != client {
t.Error("Expected correct client to be returned")
}
if pool.open != 0 {
t.Errorf("Expected 0 opened connection, got %d", pool.open)
}
// Close the connection and ensure it was returned to the freeConns pool
pool.putConn(conn, nil)
if len(pool.freeConns) != 1 {
t.Error("Expected connection to be returned to freeConns pool")
}
// Get a new connection and ensure that it is the now idling connection
conn, err = pool.conn()
if err != nil {
t.Error(err)
}
if conn.Client != client {
t.Error("Expected the same connection to be reused")
}
}