Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions tests/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -434,26 +435,37 @@ func TestConnectionExpiresIdleConnection(t *testing.T) {
opts.ConnMaxLifetime = time.Second / 10
conn, err := clickhouse.Open(&opts)
require.NoError(t, err)
t.Cleanup(func() {
conn.Close()
})

// run 1000 queries in parallel
var wg sync.WaitGroup
var queryErrors atomic.Int64
const selectToRunAtOnce = 1000
for i := 0; i < selectToRunAtOnce; i++ {
wg.Add(1)
go func() {
defer wg.Done()
r, err := conn.Query(ctx, "SELECT 1")
require.NoError(t, err)
require.NoError(t, r.Close())
require.NoError(t, r.Err())
if err != nil {
queryErrors.Add(1)
return
}
if err := r.Close(); err != nil {
queryErrors.Add(1)
return
}
}()
}
wg.Wait()
require.Equal(t, int64(0), queryErrors.Load(), "expected no query errors")

// then we expect that all connections will be closed when they are idle
// retrying for 10 seconds to make sure that the connections are closed
// use <= to tolerate minor fluctuations in server-wide connection count
assert.Eventuallyf(t, func() bool {
return getActiveConnections(t, baseConn) == expectedConnections
return getActiveConnections(t, baseConn) <= expectedConnections
}, time.Second*10, opts.ConnMaxLifetime, "expected connections to be reset back to %d", expectedConnections)
}

Expand Down
Loading