(t *testing.T)
| 350 | } |
| 351 | |
| 352 | func TestPoolBeforeAcquire(t *testing.T) { |
| 353 | t.Parallel() |
| 354 | |
| 355 | ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second) |
| 356 | defer cancel() |
| 357 | |
| 358 | config, err := pgxpool.ParseConfig(os.Getenv("PGX_TEST_DATABASE")) |
| 359 | require.NoError(t, err) |
| 360 | |
| 361 | acquireAttempts := 0 |
| 362 | |
| 363 | config.BeforeAcquire = func(ctx context.Context, c *pgx.Conn) bool { |
| 364 | acquireAttempts++ |
| 365 | return acquireAttempts%2 == 0 |
| 366 | } |
| 367 | |
| 368 | db, err := pgxpool.NewWithConfig(ctx, config) |
| 369 | require.NoError(t, err) |
| 370 | defer db.Close() |
| 371 | |
| 372 | conns := make([]*pgxpool.Conn, 4) |
| 373 | for i := range conns { |
| 374 | conns[i], err = db.Acquire(ctx) |
| 375 | assert.NoError(t, err) |
| 376 | } |
| 377 | |
| 378 | for _, c := range conns { |
| 379 | c.Release() |
| 380 | } |
| 381 | waitForReleaseToComplete() |
| 382 | |
| 383 | assert.EqualValues(t, 8, acquireAttempts) |
| 384 | |
| 385 | conns = db.AcquireAllIdle(ctx) |
| 386 | assert.Len(t, conns, 2) |
| 387 | |
| 388 | for _, c := range conns { |
| 389 | c.Release() |
| 390 | } |
| 391 | waitForReleaseToComplete() |
| 392 | |
| 393 | assert.EqualValues(t, 12, acquireAttempts) |
| 394 | } |
| 395 | |
| 396 | func TestPoolPrepareConn(t *testing.T) { |
| 397 | t.Parallel() |
nothing calls this directly
no test coverage detected