diff --git a/tests/conn_test.go b/tests/conn_test.go index 4afb8ee363..920f09f15e 100644 --- a/tests/conn_test.go +++ b/tests/conn_test.go @@ -9,6 +9,7 @@ import ( "strconv" "strings" "sync" + "sync/atomic" "testing" "time" @@ -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) }