forked from ClickHouse/clickhouse-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconn_pool_test.go
More file actions
576 lines (466 loc) · 14 KB
/
conn_pool_test.go
File metadata and controls
576 lines (466 loc) · 14 KB
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
package clickhouse
import (
"context"
"log/slog"
"sync"
"testing"
"time"
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConnPool_Cap(t *testing.T) {
tests := []struct {
name string
capacity int
}{
{
name: "capacity 1",
capacity: 1,
},
{
name: "capacity 5",
capacity: 5,
},
{
name: "capacity 10",
capacity: 10,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pool := newConnPool(time.Hour, tt.capacity)
defer pool.Close()
assert.Equal(t, tt.capacity, pool.Cap())
})
}
}
func TestConnPool_Len(t *testing.T) {
pool := newConnPool(time.Hour, 5)
defer pool.Close()
assert.Equal(t, 0, pool.Len(), "new pool should have length 0")
// Add connections
conn1 := &mockTransport{connectedAt: time.Now(), id: 1}
pool.Put(conn1)
assert.Equal(t, 1, pool.Len())
conn2 := &mockTransport{connectedAt: time.Now(), id: 2}
pool.Put(conn2)
assert.Equal(t, 2, pool.Len())
// Remove connection
ctx := context.Background()
conn, err := pool.Get(ctx)
require.NoError(t, err)
require.NotNil(t, conn)
assert.Equal(t, 1, pool.Len())
conn, err = pool.Get(ctx)
require.NoError(t, err)
require.NotNil(t, conn)
assert.Equal(t, 0, pool.Len())
}
func TestConnPool_GetEmpty(t *testing.T) {
pool := newConnPool(time.Hour, 5)
defer pool.Close()
ctx := context.Background()
conn, err := pool.Get(ctx)
require.ErrorIs(t, err, errQueueEmpty)
assert.Nil(t, conn, "getting from empty pool should return nil")
}
func TestConnPool_PutAndGet(t *testing.T) {
pool := newConnPool(time.Hour, 5)
defer pool.Close()
now := time.Now()
conn1 := &mockTransport{connectedAt: now, id: 1}
conn2 := &mockTransport{connectedAt: now.Add(time.Second), id: 2}
conn3 := &mockTransport{connectedAt: now.Add(2 * time.Second), id: 3}
// Put connections
pool.Put(conn1)
pool.Put(conn2)
pool.Put(conn3)
assert.Equal(t, 3, pool.Len())
ctx := context.Background()
// Get should return oldest connection first (heap min)
retrieved, err := pool.Get(ctx)
require.NoError(t, err)
require.NotNil(t, retrieved)
assert.Equal(t, 1, retrieved.connID(), "should get oldest connection first")
retrieved, err = pool.Get(ctx)
require.NoError(t, err)
require.NotNil(t, retrieved)
assert.Equal(t, 2, retrieved.connID())
retrieved, err = pool.Get(ctx)
require.NoError(t, err)
require.NotNil(t, retrieved)
assert.Equal(t, 3, retrieved.connID())
// Pool should be empty now
retrieved, err = pool.Get(ctx)
require.ErrorIs(t, err, errQueueEmpty)
assert.Nil(t, retrieved)
}
func TestConnPool_CapacityLimit(t *testing.T) {
capacity := 3
pool := newConnPool(time.Hour, capacity)
defer pool.Close()
now := time.Now()
// Add more connections than capacity
allConns := make([]*mockTransport, 5)
for i := 0; i < 5; i++ {
conn := &mockTransport{
connectedAt: now.Add(time.Duration(i) * time.Second),
id: i + 1,
}
allConns[i] = conn
pool.Put(conn)
}
// Pool should not exceed capacity
assert.Equal(t, capacity, pool.Len())
// Connections 4 and 5 should have been closed (rejected when pool at capacity)
assert.True(t, allConns[3].closed, "connection 4 should be closed when pool is full")
assert.True(t, allConns[4].closed, "connection 5 should be closed when pool is full")
assert.False(t, allConns[0].closed, "connection 1 should not be closed")
assert.False(t, allConns[1].closed, "connection 2 should not be closed")
assert.False(t, allConns[2].closed, "connection 3 should not be closed")
ctx := context.Background()
// With FIFO insertion order, the first 3 connections are kept [1, 2, 3]
// connections (4) and (5) are rejected when pool is at capacity
for i := 0; i < 3; i++ {
retrieved, err := pool.Get(ctx)
require.NoError(t, err)
require.NotNil(t, retrieved)
assert.Equal(t, i+1, retrieved.connID(), "should get connections in FIFO order")
}
}
func TestConnPool_ExpiredConnectionNotReturned(t *testing.T) {
// Pool with very short lifetime
lifetime := 100 * time.Millisecond
pool := newConnPool(lifetime, 5)
defer pool.Close()
// Add connection that is not yet expired (but close to expiration)
oldConn := &mockTransport{
connectedAt: time.Now().Add(-50 * time.Millisecond),
id: 1,
}
pool.Put(oldConn)
// Add fresh connection
freshConn := &mockTransport{
connectedAt: time.Now(),
id: 2,
}
pool.Put(freshConn)
// Wait for the old connection to expire
time.Sleep(60 * time.Millisecond)
ctx := context.Background()
// Get should skip expired connection and return fresh one
retrieved, err := pool.Get(ctx)
require.NoError(t, err)
require.NotNil(t, retrieved)
assert.Equal(t, 2, retrieved.connID(), "should skip expired connection")
// The expired connection should have been closed during Get()
assert.True(t, oldConn.closed, "expired connection should be closed")
assert.False(t, freshConn.closed, "fresh connection should not be closed")
// Pool should be empty now
retrieved, err = pool.Get(ctx)
require.ErrorIs(t, err, errQueueEmpty)
assert.Nil(t, retrieved)
}
func TestConnPool_PutExpiredConnection(t *testing.T) {
lifetime := 100 * time.Millisecond
pool := newConnPool(lifetime, 5)
defer pool.Close()
// Try to put already expired connection
expiredConn := &mockTransport{
connectedAt: time.Now().Add(-200 * time.Millisecond),
id: 1,
}
pool.Put(expiredConn)
// Pool should not accept expired connection
assert.Equal(t, 0, pool.Len())
}
func TestConnPool_PutOlderThanMinimumWithCapacity(t *testing.T) {
pool := newConnPool(time.Hour, 5)
defer pool.Close()
now := time.Now()
// Add a connection
conn1 := &mockTransport{connectedAt: now, id: 1}
pool.Put(conn1)
// Add an older connection (but inserted second)
olderConn := &mockTransport{connectedAt: now.Add(-time.Minute), id: 2}
pool.Put(olderConn)
// Both connections should be in the pool
assert.Equal(t, 2, pool.Len())
ctx := context.Background()
retrieved, err := pool.Get(ctx)
require.NoError(t, err)
require.NotNil(t, retrieved)
// With FIFO insertion order, we get the first inserted connection
assert.Equal(t, 1, retrieved.connID(), "should retrieve the first inserted connection")
}
func TestConnPool_GetWithCancelledContext(t *testing.T) {
pool := newConnPool(time.Hour, 5)
defer pool.Close()
// Add a connection
conn := &mockTransport{connectedAt: time.Now(), id: 1}
pool.Put(conn)
// Create cancelled context
ctx, cancel := context.WithCancel(context.Background())
cancel()
// Get with cancelled context should return context error
retrieved, err := pool.Get(ctx)
assert.Error(t, err)
assert.Equal(t, context.Canceled, err)
assert.Nil(t, retrieved)
// Connection should still be in pool
assert.Equal(t, 1, pool.Len())
}
func TestConnPool_Close(t *testing.T) {
pool := newConnPool(time.Hour, 5)
// Add connections
for i := 0; i < 3; i++ {
conn := &mockTransport{
connectedAt: time.Now(),
id: i + 1,
}
pool.Put(conn)
}
assert.Equal(t, 3, pool.Len())
// Close the pool
err := pool.Close()
assert.NoError(t, err)
// Verify pool is closed
assert.True(t, pool.closed())
// Get should return ErrConnectionClosed from closed pool
ctx := context.Background()
conn, err := pool.Get(ctx)
assert.Error(t, err)
assert.Equal(t, ErrConnectionClosed, err)
assert.Nil(t, conn)
// Put should be ignored on closed pool
initialLen := pool.Len()
newConn := &mockTransport{connectedAt: time.Now(), id: 99}
pool.Put(newConn)
assert.Equal(t, initialLen, pool.Len(), "closed pool should not accept new connections")
// Closing again should be safe
err = pool.Close()
assert.NoError(t, err)
}
func TestConnPool_CloseWithDrain(t *testing.T) {
pool := newConnPool(time.Hour, 5)
// Add connections
allConns := make([]*mockTransport, 3)
for i := 0; i < 3; i++ {
mock := &mockTransport{
connectedAt: time.Now(),
id: i + 1,
}
allConns[i] = mock
pool.Put(mock)
}
assert.Equal(t, 3, pool.Len(), "pool should have 3 connections before close")
// Close the pool
err := pool.Close()
assert.NoError(t, err)
// Verify pool is closed
assert.True(t, pool.closed())
// Verify all connections are drained from the pool
assert.Equal(t, 0, pool.Len(), "pool should be empty after close (all connections drained)")
// Verify all connections were closed
for i, conn := range allConns {
assert.True(t, conn.closed, "connection %d should be closed after pool close", i+1)
}
// Verify no connections can be retrieved
ctx := context.Background()
conn, err := pool.Get(ctx)
assert.Error(t, err)
assert.Equal(t, ErrConnectionClosed, err)
assert.Nil(t, conn, "get should return nil after pool is closed and drained")
}
func TestConnPool_DrainExpiredConnections(t *testing.T) {
lifetime := 100 * time.Millisecond
pool := newConnPool(lifetime, 5)
defer pool.Close()
// Add connections that are already old (so they will definitely expire)
oldTime := time.Now().Add(-50 * time.Millisecond)
expiredConns := make([]*mockTransport, 3)
for i := 0; i < 3; i++ {
conn := &mockTransport{
connectedAt: oldTime.Add(time.Duration(i) * time.Millisecond),
id: i + 1,
}
expiredConns[i] = conn
pool.Put(conn)
}
assert.Equal(t, 3, pool.Len())
// Wait for connections to expire and drain cycle to run
// The connections will be 50ms + 100ms (sleep) = 150ms old, exceeding the 100ms lifetime
time.Sleep(lifetime + 50*time.Millisecond)
// At this point the drain should have run and removed the expired connections
assert.Equal(t, 0, pool.Len(), "all expired connections should be drained")
// Verify all expired connections were closed
for i, conn := range expiredConns {
assert.True(t, conn.closed, "expired connection %d should be closed", i+1)
}
// Add a fresh connection to verify pool still works after drain
freshConn := &mockTransport{
connectedAt: time.Now(),
id: 99,
}
pool.Put(freshConn)
// Fresh connection should be in the pool
assert.Equal(t, 1, pool.Len(), "fresh connection should be added after drain")
assert.False(t, freshConn.closed, "fresh connection should not be closed")
}
func TestConnPool_ConcurrentAccess(t *testing.T) {
pool := newConnPool(time.Hour, 10)
defer pool.Close()
ctx := context.Background()
done := make(chan struct{})
// Concurrent puts
go func() {
for i := 0; i < 20; i++ {
conn := &mockTransport{
connectedAt: time.Now(),
id: i,
}
pool.Put(conn)
time.Sleep(time.Millisecond)
}
close(done)
}()
// Concurrent gets
go func() {
for i := 0; i < 20; i++ {
_, _ = pool.Get(ctx)
time.Sleep(time.Millisecond)
}
}()
// Wait for puts to complete
<-done
// Give gets time to complete
time.Sleep(50 * time.Millisecond)
// Pool should not exceed capacity
assert.LessOrEqual(t, pool.Len(), pool.Cap())
}
func TestConnPool_FIFOOrdering(t *testing.T) {
pool := newConnPool(time.Hour, 10)
defer pool.Close()
now := time.Now()
// Add connections with varying timestamps in non-chronological order
// to test that FIFO is based on insertion order, not connection age
connections := []struct {
time time.Time
id int
}{
{now.Add(1 * time.Second), 1},
{now.Add(5 * time.Second), 5},
{now.Add(2 * time.Second), 2},
{now.Add(4 * time.Second), 4},
{now.Add(3 * time.Second), 3},
}
// Add all connections
for _, c := range connections {
conn := &mockTransport{
connectedAt: c.time,
id: c.id,
}
pool.Put(conn)
}
expectedCount := 5
assert.Equal(t, expectedCount, pool.Len())
ctx := context.Background()
// Get all connections and verify they come out in insertion order (FIFO)
for _, id := range []int{1, 5, 2, 4, 3} {
conn, err := pool.Get(ctx)
require.NoError(t, err)
require.NotNil(t, conn, "should get connection %d", id)
assert.Equal(t, id, conn.connID(),
"connections should be returned in FIFO insertion order")
}
}
// mockTransport implements nativeTransport for testing
type mockTransport struct {
connectedAt time.Time
id int
released bool
closed bool
bad bool
bufferFreed bool
debugMessages []string
mu sync.Mutex
}
func (m *mockTransport) serverVersion() (*ServerVersion, error) {
return nil, nil
}
func (m *mockTransport) query(ctx context.Context, release nativeTransportRelease, query string, args ...any) (*rows, error) {
return nil, nil
}
func (m *mockTransport) queryRow(ctx context.Context, release nativeTransportRelease, query string, args ...any) *row {
return nil
}
func (m *mockTransport) prepareBatch(ctx context.Context, release nativeTransportRelease, acquire nativeTransportAcquire, query string, opts driver.PrepareBatchOptions) (driver.Batch, error) {
return nil, nil
}
func (m *mockTransport) exec(ctx context.Context, query string, args ...any) error {
return nil
}
func (m *mockTransport) asyncInsert(ctx context.Context, query string, wait bool, args ...any) error {
return nil
}
func (m *mockTransport) ping(ctx context.Context) error {
return nil
}
func (m *mockTransport) isBad() bool {
m.mu.Lock()
defer m.mu.Unlock()
return m.bad
}
func (m *mockTransport) connID() int {
return m.id
}
func (m *mockTransport) connectedAtTime() time.Time {
return m.connectedAt
}
func (m *mockTransport) isReleased() bool {
m.mu.Lock()
defer m.mu.Unlock()
return m.released
}
func (m *mockTransport) setReleased(released bool) {
m.mu.Lock()
defer m.mu.Unlock()
m.released = released
}
func (m *mockTransport) getLogger() *slog.Logger {
return newNoopLogger()
}
func (m *mockTransport) freeBuffer() {
m.mu.Lock()
defer m.mu.Unlock()
m.bufferFreed = true
}
func (m *mockTransport) close() error {
m.mu.Lock()
defer m.mu.Unlock()
m.closed = true
return nil
}
// Helper methods for testing
func newMockTransport(id int) *mockTransport {
return &mockTransport{
connectedAt: time.Now(),
id: id,
}
}
func (m *mockTransport) setBad(bad bool) {
m.mu.Lock()
defer m.mu.Unlock()
m.bad = bad
}
func (m *mockTransport) isClosed() bool {
m.mu.Lock()
defer m.mu.Unlock()
return m.closed
}
func (m *mockTransport) wasBufferFreed() bool {
m.mu.Lock()
defer m.mu.Unlock()
return m.bufferFreed
}