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

Function TestPoolBeforeClose

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

Source from the content-addressed store, hash-verified

491}
492
493func TestPoolBeforeClose(t *testing.T) {
494 t.Parallel()
495
496 ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
497 defer cancel()
498
499 func() {
500 pool, err := pgxpool.New(ctx, os.Getenv("PGX_TEST_DATABASE"))
501 require.NoError(t, err)
502 defer pool.Close()
503 }()
504
505 config, err := pgxpool.ParseConfig(os.Getenv("PGX_TEST_DATABASE"))
506 require.NoError(t, err)
507
508 connPIDs := make(chan uint32, 5)
509 config.BeforeClose = func(c *pgx.Conn) {
510 connPIDs <- c.PgConn().PID()
511 }
512
513 db, err := pgxpool.NewWithConfig(ctx, config)
514 require.NoError(t, err)
515 defer db.Close()
516
517 acquiredPIDs := make([]uint32, 0, 5)
518 closedPIDs := make([]uint32, 0, 5)
519 for range 5 {
520 conn, err := db.Acquire(ctx)
521 assert.NoError(t, err)
522 acquiredPIDs = append(acquiredPIDs, conn.Conn().PgConn().PID())
523 conn.Release()
524 db.Reset()
525 closedPIDs = append(closedPIDs, <-connPIDs)
526 }
527
528 assert.ElementsMatch(t, acquiredPIDs, closedPIDs)
529}
530
531func TestPoolAcquireAllIdle(t *testing.T) {
532 t.Parallel()

Callers

nothing calls this directly

Calls 10

NewFunction · 0.92
ParseConfigFunction · 0.92
NewWithConfigFunction · 0.92
PIDMethod · 0.80
PgConnMethod · 0.80
AcquireMethod · 0.80
ReleaseMethod · 0.80
ResetMethod · 0.80
CloseMethod · 0.65
ConnMethod · 0.65

Tested by

no test coverage detected