-
Notifications
You must be signed in to change notification settings - Fork 639
Expand file tree
/
Copy pathconn_pool_sync_test.go
More file actions
69 lines (54 loc) · 1.84 KB
/
conn_pool_sync_test.go
File metadata and controls
69 lines (54 loc) · 1.84 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
//go:build go1.25
package clickhouse
import (
"context"
"testing"
"testing/synctest"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConnPool_ExpiredConnectionsAreDrained(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
pool := newConnPool(50*time.Millisecond, 5)
defer pool.Close()
firstConn := &mockTransport{
connectedAt: time.Now(),
id: 1,
}
pool.Put(firstConn)
secondConn := &mockTransport{
connectedAt: time.Now().Add(50 * time.Millisecond),
id: 2,
}
pool.Put(secondConn)
// Wait for the old connection to expire
time.Sleep(50 * time.Millisecond)
synctest.Wait()
// Ensure the first connection is already closed before we attempt
// to Get one from the pool. This is to ensure that it was closed by
// the background drail pool and not on the call to Get.
assert.True(t, firstConn.closed, "first connection should be closed")
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")
assert.False(t, secondConn.closed, "second connection should not be closed")
// put the second connection back so that it can be expired by background routine
pool.Put(secondConn)
// Wait for the second connection to expire
time.Sleep(50 * time.Millisecond)
synctest.Wait()
// Again ensure the connection has been closed due to the drain
// pool goroutine and not the next call to Get.
assert.True(t, secondConn.closed, "second connection should be closed")
// Pool should be empty now
retrieved, err = pool.Get(ctx)
require.ErrorIs(t, err, errQueueEmpty)
assert.Nil(t, retrieved)
assert.Nil(t, pool.Close())
synctest.Wait()
})
}