forked from processout/grpc-go-pool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool_test.go
448 lines (381 loc) · 12.1 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
package grpcpool
import (
"context"
"sync"
"testing"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials/insecure"
)
func TestNew(t *testing.T) {
p, err := New(func() (*grpc.ClientConn, error) {
return grpc.Dial("example.com", grpc.WithTransportCredentials(insecure.NewCredentials()))
}, 1, 3, 0)
if err != nil {
t.Errorf("The pool returned an error: %s", err.Error())
}
if a := p.Available(); a != 3 {
t.Errorf("The pool available was %d but should be 3", a)
}
if a := p.Capacity(); a != 3 {
t.Errorf("The pool capacity was %d but should be 3", a)
}
// Get a client
client, err := p.Get(context.Background())
if err != nil {
t.Errorf("Get returned an error: %s", err.Error())
}
if client == nil {
t.Error("client was nil")
}
if a := p.Available(); a != 2 {
t.Errorf("The pool available was %d but should be 2", a)
}
if a := p.Capacity(); a != 3 {
t.Errorf("The pool capacity was %d but should be 3", a)
}
// Return the client
err = client.Close()
if err != nil {
t.Errorf("Close returned an error: %s", err.Error())
}
if a := p.Available(); a != 3 {
t.Errorf("The pool available was %d but should be 3", a)
}
if a := p.Capacity(); a != 3 {
t.Errorf("The pool capacity was %d but should be 3", a)
}
// Attempt to return the client again
err = client.Close()
if err != ErrAlreadyClosed {
t.Errorf("Expected error \"%s\" but got \"%s\"",
ErrAlreadyClosed.Error(), err.Error())
}
// Take 3 clients
cl1, err1 := p.Get(context.Background())
cl2, err2 := p.Get(context.Background())
cl3, err3 := p.Get(context.Background())
if err1 != nil {
t.Errorf("Err1 was not nil: %s", err1.Error())
}
if err2 != nil {
t.Errorf("Err2 was not nil: %s", err2.Error())
}
if err3 != nil {
t.Errorf("Err3 was not nil: %s", err3.Error())
}
if a := p.Available(); a != 0 {
t.Errorf("The pool available was %d but should be 0", a)
}
if a := p.Capacity(); a != 3 {
t.Errorf("The pool capacity was %d but should be 3", a)
}
// Returning all of them
err1 = cl1.Close()
if err1 != nil {
t.Errorf("Close returned an error: %s", err1.Error())
}
err2 = cl2.Close()
if err2 != nil {
t.Errorf("Close returned an error: %s", err2.Error())
}
err3 = cl3.Close()
if err3 != nil {
t.Errorf("Close returned an error: %s", err3.Error())
}
}
func TestCapacity(t *testing.T) {
// Assign negative capacity, test that it is changed to 1
p, err := New(func() (*grpc.ClientConn, error) {
return grpc.Dial("example.com", grpc.WithTransportCredentials(insecure.NewCredentials()))
}, -1, -2, 0)
if err != nil {
t.Errorf("The pool returned an error: %s", err.Error())
}
if a := p.Available(); a != 1 {
t.Errorf("The pool available was %d but should be 1", a)
}
if a := p.Capacity(); a != 1 {
t.Errorf("The pool capacity was %d but should be 1", a)
}
// Assing more initial connections than capacity, test that it is changed to cap limit
p, err = New(func() (*grpc.ClientConn, error) {
return grpc.Dial("example.com", grpc.WithTransportCredentials(insecure.NewCredentials()))
}, 100, 2, 0)
if err != nil {
t.Errorf("The pool returned an error: %s", err.Error())
}
if a := p.Available(); a != 2 {
t.Errorf("The pool available was %d but should be 2", a)
}
if a := p.Capacity(); a != 2 {
t.Errorf("The pool capacity was %d but should be 2", a)
}
}
func TestTimeout(t *testing.T) {
p, err := New(func() (*grpc.ClientConn, error) {
return grpc.Dial("example.com", grpc.WithTransportCredentials(insecure.NewCredentials()))
}, 1, 1, 0)
if err != nil {
t.Errorf("The pool returned an error: %s", err.Error())
}
_, err = p.Get(context.Background())
if err != nil {
t.Errorf("Get returned an error: %s", err.Error())
}
if a := p.Available(); a != 0 {
t.Errorf("The pool available was %d but expected 0", a)
}
// We want to fetch a second one, with a timeout. If the timeout was
// ommitted, the pool would wait indefinitely as it'd wait for another
// client to get back into the queue
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(10*time.Millisecond))
defer cancel()
_, err2 := p.Get(ctx)
if err2 != ErrTimeout {
t.Errorf("Expected error \"%s\" but got \"%s\"", ErrTimeout, err2.Error())
}
}
func TestMaxLifeDuration(t *testing.T) {
p, err := New(func() (*grpc.ClientConn, error) {
return grpc.Dial("example.com", grpc.WithTransportCredentials(insecure.NewCredentials()))
}, 1, 1, 0, 1)
if err != nil {
t.Errorf("The pool returned an error: %s", err.Error())
}
c, err := p.Get(context.Background())
if err != nil {
t.Errorf("Get returned an error: %s", err.Error())
}
// The max life of the connection was very low (1ns), so when we close
// the connection it should get marked as unhealthy
if err := c.Close(); err != nil {
t.Errorf("Close returned an error: %s", err.Error())
}
if !c.unhealthy {
t.Errorf("the connection should've been marked as unhealthy")
}
// Let's also make sure we don't prematurely close the connection
count := 0
p, err = New(func() (*grpc.ClientConn, error) {
count++
return grpc.Dial("example.com", grpc.WithTransportCredentials(insecure.NewCredentials()))
}, 1, 1, 0, time.Minute)
if err != nil {
t.Errorf("The pool returned an error: %s", err.Error())
}
for i := 0; i < 3; i++ {
c, err = p.Get(context.Background())
if err != nil {
t.Errorf("Get returned an error: %s", err.Error())
}
// The max life of the connection is high, so when we close
// the connection it shouldn't be marked as unhealthy
if err := c.Close(); err != nil {
t.Errorf("Close returned an error: %s", err.Error())
}
if c.unhealthy {
t.Errorf("the connection shouldn't have been marked as unhealthy")
}
}
// Count should have been 1 as dial function should only have been called once
if count > 1 {
t.Errorf("Dial function has been called multiple times")
}
}
func TestIdleTimeout(t *testing.T) {
// Let's test idle timeout for connection laying in the pool
// Call Get 3 times. 2 of them will happen before idle timeout, one after. Connection should be created 2 times total
count := 0
p, err := New(func() (*grpc.ClientConn, error) {
count++
return grpc.Dial("example.com", grpc.WithTransportCredentials(insecure.NewCredentials()))
}, 1, 1, time.Millisecond)
if err != nil {
t.Errorf("The pool returned an error: %s", err.Error())
}
c, _ := p.Get(context.Background())
c.Close()
c, _ = p.Get(context.Background())
c.Close()
time.Sleep(10 * time.Millisecond)
c, _ = p.Get(context.Background())
c.Close()
if count != 2 {
t.Errorf("Dial function has been called only %v times, expected 2", count)
}
}
func TestPoolClose(t *testing.T) {
p, err := New(func() (*grpc.ClientConn, error) {
return grpc.Dial("example.com", grpc.WithTransportCredentials(insecure.NewCredentials()))
}, 1, 1, 0)
if err != nil {
t.Errorf("The pool returned an error: %s", err.Error())
}
c, err := p.Get(context.Background())
if err != nil {
t.Errorf("Get returned an error: %s", err.Error())
}
cc := c.ClientConn
if err := c.Close(); err != nil {
t.Errorf("Close returned an error: %s", err.Error())
}
// Close pool should close all underlying gRPC client connections
p.Close()
if cc.GetState() != connectivity.Shutdown {
t.Errorf("Returned connection was not closed, underlying connection is not in shutdown state")
}
if p.Capacity() != 0 {
t.Errorf("Closed pool capacity is not zero")
}
if p.Available() != 0 {
t.Errorf("Closed pool availability is not zero")
}
_, err = p.Get(context.Background())
if err != ErrClosed {
t.Errorf("Closed pool returned unexpected error")
}
}
func TestContextCancelation(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := NewWithContext(ctx, func(ctx context.Context) (*grpc.ClientConn, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
return grpc.Dial("example.com", grpc.WithTransportCredentials(insecure.NewCredentials()))
}
}, 1, 1, 0)
if err != context.Canceled {
t.Errorf("Returned error was not context.Canceled, but the context did cancel before the invocation")
}
}
func TestContextTimeout(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Microsecond)
defer cancel()
_, err := NewWithContext(ctx, func(ctx context.Context) (*grpc.ClientConn, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
// wait for the deadline to pass
case <-time.After(time.Millisecond):
return grpc.Dial("example.com", grpc.WithTransportCredentials(insecure.NewCredentials()))
}
}, 1, 1, 0)
if err != context.DeadlineExceeded {
t.Errorf("Returned error was not context.DeadlineExceeded, but the context was timed out before the initialization")
}
}
func TestGetContextTimeout(t *testing.T) {
p, err := New(func() (*grpc.ClientConn, error) {
return grpc.Dial("example.com", grpc.WithTransportCredentials(insecure.NewCredentials()))
}, 1, 1, 0)
if err != nil {
t.Errorf("The pool returned an error: %s", err.Error())
}
// keep busy the available conn
_, _ = p.Get(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), time.Microsecond)
defer cancel()
// wait for the deadline to pass
time.Sleep(time.Millisecond)
_, err = p.Get(ctx)
if err != ErrTimeout { // it should be context.DeadlineExceeded
t.Errorf("Returned error was not ErrTimeout, but the context was timed out before the Get invocation")
}
}
func TestGetContextFactoryTimeout(t *testing.T) {
p, err := NewWithContext(context.Background(), func(ctx context.Context) (*grpc.ClientConn, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
// wait for the deadline to pass
case <-time.After(100 * time.Millisecond):
return grpc.Dial("example.com", grpc.WithTransportCredentials(insecure.NewCredentials()))
}
}, 1, 1, 0)
if err != nil {
t.Errorf("The pool returned an error: %s", err.Error())
}
// mark as unhealty the available conn
c, err := p.Get(context.Background())
if err != nil {
t.Errorf("Get returned an error: %s", err.Error())
}
c.Unhealthy()
c.Close()
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
defer cancel()
_, err = p.Get(ctx)
if err != context.DeadlineExceeded {
t.Errorf("Returned error was not context.DeadlineExceeded, but the context was timed out before the Get invocation")
}
}
func TestNilPtr(t *testing.T) {
p, err := New(func() (*grpc.ClientConn, error) {
return grpc.Dial("example.com", grpc.WithTransportCredentials(insecure.NewCredentials()))
}, 1, 1, 0)
if err != nil {
t.Errorf("The pool returned an error: %s", err.Error())
}
c, _ := p.Get(context.Background())
c.Unhealthy() // suppress linter warning before assigning to nil
// after setting pointers to nil functions should return, no crash should be triggered
p = nil
p.Get(context.Background())
p.getClients()
p.put(nil)
p.IsClosed()
p.Available()
p.Capacity()
p.Close()
c = nil
c.Unhealthy()
c.Close()
}
// tun Racing tests with `-race` flag
func TestConnRacing(t *testing.T) {
p, err := New(func() (*grpc.ClientConn, error) {
return grpc.Dial("example.com", grpc.WithTransportCredentials(insecure.NewCredentials()))
}, 1, 1, 0)
if err != nil {
t.Errorf("The pool returned an error: %s", err.Error())
}
var wg sync.WaitGroup
const iterations = 10
// Attempt to close same connection in different threads
conn, _ := p.Get(context.Background())
for i := 0; i < iterations; i++ {
wg.Add(1)
go conn.Close()
wg.Done()
}
wg.Wait()
// Check racing condintion with Pool and Connection close
conn, _ = p.Get(context.Background())
go p.Close()
go conn.Close()
}
func TestPoolRacing(t *testing.T) {
var wg sync.WaitGroup
const iterations = 1000
for i := 0; i < iterations; i++ {
wg.Add(1)
p, err := New(func() (*grpc.ClientConn, error) {
return grpc.Dial("example.com", grpc.WithTransportCredentials(insecure.NewCredentials()))
}, 1, 1, 0)
if err != nil {
t.Errorf("The pool returned an error: %s", err.Error())
}
// deliberately call multiple Close commands to force race
conn, _ := p.Get(context.Background())
go p.Close()
go p.Close()
go conn.Close()
go conn.Close()
wg.Done()
}
wg.Wait()
}