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

Function TestDeadlineContextWatcherHandler

pgconn/pgconn_test.go:4707–4751  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

4705}
4706
4707func TestDeadlineContextWatcherHandler(t *testing.T) {
4708 t.Parallel()
4709
4710 t.Run("DeadlineExceeded with zero DeadlineDelay", func(t *testing.T) {
4711 config, err := pgconn.ParseConfig(os.Getenv("PGX_TEST_DATABASE"))
4712 require.NoError(t, err)
4713 config.BuildContextWatcherHandler = func(conn *pgconn.PgConn) ctxwatch.Handler {
4714 return &pgconn.DeadlineContextWatcherHandler{Conn: conn.Conn()}
4715 }
4716 config.ConnectTimeout = 5 * time.Second
4717
4718 pgConn, err := pgconn.ConnectConfig(context.Background(), config)
4719 require.NoError(t, err)
4720 defer closeConn(t, pgConn)
4721
4722 ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
4723 defer cancel()
4724
4725 _, err = pgConn.Exec(ctx, "select 1, pg_sleep(1)").ReadAll()
4726 require.Error(t, err)
4727 require.ErrorIs(t, err, context.DeadlineExceeded)
4728 require.True(t, pgConn.IsClosed())
4729 })
4730
4731 t.Run("DeadlineExceeded with DeadlineDelay", func(t *testing.T) {
4732 config, err := pgconn.ParseConfig(os.Getenv("PGX_TEST_DATABASE"))
4733 require.NoError(t, err)
4734 config.BuildContextWatcherHandler = func(conn *pgconn.PgConn) ctxwatch.Handler {
4735 return &pgconn.DeadlineContextWatcherHandler{Conn: conn.Conn(), DeadlineDelay: 500 * time.Millisecond}
4736 }
4737 config.ConnectTimeout = 5 * time.Second
4738
4739 pgConn, err := pgconn.ConnectConfig(context.Background(), config)
4740 require.NoError(t, err)
4741 defer closeConn(t, pgConn)
4742
4743 ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
4744 defer cancel()
4745
4746 _, err = pgConn.Exec(ctx, "select 1, pg_sleep(0.250)").ReadAll()
4747 require.NoError(t, err)
4748
4749 ensureConnValid(t, pgConn)
4750 })
4751}
4752
4753func TestCancelRequestContextWatcherHandler(t *testing.T) {
4754 t.Parallel()

Callers

nothing calls this directly

Calls 10

ParseConfigFunction · 0.92
ConnectConfigFunction · 0.92
ReadAllMethod · 0.80
closeConnFunction · 0.70
ensureConnValidFunction · 0.70
ConnMethod · 0.65
ExecMethod · 0.65
RunMethod · 0.45
ErrorMethod · 0.45
IsClosedMethod · 0.45

Tested by

no test coverage detected