MCPcopy
hub / github.com/jackc/pgx / TestPoolAcquireChecksIdleConns

Function TestPoolAcquireChecksIdleConns

pgxpool/pool_test.go:154–206  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

152}
153
154func TestPoolAcquireChecksIdleConns(t *testing.T) {
155 t.Parallel()
156
157 ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
158 defer cancel()
159
160 controllerConn, err := pgx.Connect(ctx, os.Getenv("PGX_TEST_DATABASE"))
161 require.NoError(t, err)
162 defer controllerConn.Close(ctx)
163 pgxtest.SkipCockroachDB(t, controllerConn, "Server does not support pg_terminate_backend() (https://github.com/cockroachdb/cockroach/issues/35897)")
164
165 pool, err := pgxpool.New(ctx, os.Getenv("PGX_TEST_DATABASE"))
166 require.NoError(t, err)
167 defer pool.Close()
168
169 var conns []*pgxpool.Conn
170 for range 3 {
171 c, err := pool.Acquire(ctx)
172 require.NoError(t, err)
173 conns = append(conns, c)
174 }
175
176 require.EqualValues(t, 3, pool.Stat().TotalConns())
177
178 var pids []uint32
179 for _, c := range conns {
180 pids = append(pids, c.Conn().PgConn().PID())
181 c.Release()
182 }
183
184 _, err = controllerConn.Exec(ctx, `select pg_terminate_backend(n) from unnest($1::int[]) n`, pids)
185 require.NoError(t, err)
186
187 // All conns are dead they don't know it and neither does the pool.
188 require.EqualValues(t, 3, pool.Stat().TotalConns())
189
190 // Wait long enough so the pool will realize it needs to check the connections.
191 time.Sleep(time.Second)
192
193 // Pool should try all existing connections and find them dead, then create a new connection which should successfully ping.
194 err = pool.Ping(ctx)
195 require.NoError(t, err)
196
197 // The original 3 conns should have been terminated and the a new conn established for the ping.
198 require.EqualValues(t, 1, pool.Stat().TotalConns())
199 c, err := pool.Acquire(ctx)
200 require.NoError(t, err)
201
202 cPID := c.Conn().PgConn().PID()
203 c.Release()
204
205 require.NotContains(t, pids, cPID)
206}
207
208func TestPoolAcquireChecksIdleConnsWithShouldPing(t *testing.T) {
209 t.Parallel()

Callers

nothing calls this directly

Calls 13

SkipCockroachDBFunction · 0.92
NewFunction · 0.92
AcquireMethod · 0.80
TotalConnsMethod · 0.80
StatMethod · 0.80
PIDMethod · 0.80
PgConnMethod · 0.80
ReleaseMethod · 0.80
CloseMethod · 0.65
ConnMethod · 0.65
ExecMethod · 0.65
ConnectMethod · 0.45

Tested by

no test coverage detected