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

Function TestPoolPrepareConn

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

Source from the content-addressed store, hash-verified

394}
395
396func TestPoolPrepareConn(t *testing.T) {
397 t.Parallel()
398
399 ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
400 defer cancel()
401
402 config, err := pgxpool.ParseConfig(os.Getenv("PGX_TEST_DATABASE"))
403 require.NoError(t, err)
404
405 acquireAttempts := 0
406
407 config.PrepareConn = func(context.Context, *pgx.Conn) (bool, error) {
408 acquireAttempts++
409 var err error
410 if acquireAttempts%3 == 0 {
411 err = errors.New("PrepareConn error")
412 }
413 return acquireAttempts%2 == 0, err
414 }
415
416 db, err := pgxpool.NewWithConfig(ctx, config)
417 require.NoError(t, err)
418 t.Cleanup(db.Close)
419
420 var errorCount int
421 conns := make([]*pgxpool.Conn, 0, 4)
422 for {
423 conn, err := db.Acquire(ctx)
424 if err != nil {
425 errorCount++
426 continue
427 }
428 conns = append(conns, conn)
429 if len(conns) == 4 {
430 break
431 }
432 }
433 const wantErrorCount = 3
434 assert.Equal(t, wantErrorCount, errorCount, "Acquire() should have failed %d times", wantErrorCount)
435
436 for _, c := range conns {
437 c.Release()
438 }
439 waitForReleaseToComplete()
440
441 assert.EqualValues(t, len(conns)*2+wantErrorCount-1, acquireAttempts)
442
443 conns = db.AcquireAllIdle(ctx)
444 assert.Len(t, conns, 1)
445
446 for _, c := range conns {
447 c.Release()
448 }
449 waitForReleaseToComplete()
450
451 assert.EqualValues(t, 14, acquireAttempts)
452}
453

Callers

nothing calls this directly

Calls 7

ParseConfigFunction · 0.92
NewWithConfigFunction · 0.92
waitForReleaseToCompleteFunction · 0.85
AcquireMethod · 0.80
ReleaseMethod · 0.80
AcquireAllIdleMethod · 0.80
LenMethod · 0.65

Tested by

no test coverage detected