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

Function TestPoolAcquirePingTimeout

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

Source from the content-addressed store, hash-verified

1325}
1326
1327func TestPoolAcquirePingTimeout(t *testing.T) {
1328 t.Parallel()
1329
1330 ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
1331 defer cancel()
1332
1333 config, err := pgxpool.ParseConfig(os.Getenv("PGX_TEST_DATABASE"))
1334 require.NoError(t, err)
1335
1336 config.PingTimeout = 200 * time.Millisecond
1337 config.ConnConfig.DialFunc = newDelayProxyDialFunc(500 * time.Millisecond)
1338
1339 var conID *uint32
1340 // Only ping the connection with the original PID to force creation of a new connection
1341 config.ShouldPing = func(_ context.Context, params pgxpool.ShouldPingParams) bool {
1342 if conID != nil && params.Conn.PgConn().PID() == *conID {
1343 return true
1344 }
1345 return false
1346 }
1347
1348 // Limit to a single connection to ensure the same connection is reused
1349 config.MinConns = 1
1350 config.MaxConns = 1
1351
1352 pool, err := pgxpool.NewWithConfig(ctx, config)
1353 require.NoError(t, err)
1354 defer pool.Close()
1355
1356 c, err := pool.Acquire(ctx)
1357 require.NoError(t, err)
1358 require.EqualValues(t, 1, pool.Stat().TotalConns())
1359 originalPID := c.Conn().PgConn().PID()
1360 conID = &originalPID
1361
1362 c.Release()
1363 require.EqualValues(t, 1, pool.Stat().TotalConns())
1364
1365 c, err = pool.Acquire(ctx)
1366 require.NoError(t, err)
1367 require.EqualValues(t, 1, pool.Stat().TotalConns())
1368 newPID := c.Conn().PgConn().PID()
1369
1370 c.Release()
1371
1372 require.EqualValues(t, 1, pool.Stat().TotalConns())
1373 assert.Nil(t, ctx.Err())
1374 assert.NotEqualValues(t, originalPID, newPID,
1375 "Expected new connection due to ping timeout, but got same connection")
1376}

Callers

nothing calls this directly

Calls 12

ParseConfigFunction · 0.92
NewWithConfigFunction · 0.92
newDelayProxyDialFuncFunction · 0.85
PIDMethod · 0.80
PgConnMethod · 0.80
AcquireMethod · 0.80
TotalConnsMethod · 0.80
StatMethod · 0.80
ReleaseMethod · 0.80
CloseMethod · 0.65
ConnMethod · 0.65
ErrMethod · 0.65

Tested by

no test coverage detected