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

Function TestCancelRequestContextWatcherHandler

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

Source from the content-addressed store, hash-verified

4751}
4752
4753func TestCancelRequestContextWatcherHandler(t *testing.T) {
4754 t.Parallel()
4755
4756 t.Run("DeadlineExceeded cancels request after CancelRequestDelay", func(t *testing.T) {
4757 config, err := pgconn.ParseConfig(os.Getenv("PGX_TEST_DATABASE"))
4758 require.NoError(t, err)
4759 config.BuildContextWatcherHandler = func(conn *pgconn.PgConn) ctxwatch.Handler {
4760 return &pgconn.CancelRequestContextWatcherHandler{
4761 Conn: conn,
4762 CancelRequestDelay: 250 * time.Millisecond,
4763 DeadlineDelay: 5000 * time.Millisecond,
4764 }
4765 }
4766 config.ConnectTimeout = 5 * time.Second
4767
4768 pgConn, err := pgconn.ConnectConfig(context.Background(), config)
4769 require.NoError(t, err)
4770 defer closeConn(t, pgConn)
4771
4772 ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
4773 defer cancel()
4774
4775 _, err = pgConn.Exec(ctx, "select 1, pg_sleep(3)").ReadAll()
4776 require.Error(t, err)
4777 var pgErr *pgconn.PgError
4778 require.ErrorAs(t, err, &pgErr)
4779
4780 ensureConnValid(t, pgConn)
4781 })
4782
4783 t.Run("DeadlineExceeded - do not send cancel request when query finishes in grace period", func(t *testing.T) {
4784 config, err := pgconn.ParseConfig(os.Getenv("PGX_TEST_DATABASE"))
4785 require.NoError(t, err)
4786 config.BuildContextWatcherHandler = func(conn *pgconn.PgConn) ctxwatch.Handler {
4787 return &pgconn.CancelRequestContextWatcherHandler{
4788 Conn: conn,
4789 CancelRequestDelay: 1000 * time.Millisecond,
4790 DeadlineDelay: 5000 * time.Millisecond,
4791 }
4792 }
4793 config.ConnectTimeout = 5 * time.Second
4794
4795 pgConn, err := pgconn.ConnectConfig(context.Background(), config)
4796 require.NoError(t, err)
4797 defer closeConn(t, pgConn)
4798
4799 ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
4800 defer cancel()
4801
4802 _, err = pgConn.Exec(ctx, "select 1, pg_sleep(0.250)").ReadAll()
4803 require.NoError(t, err)
4804
4805 ensureConnValid(t, pgConn)
4806 })
4807
4808 t.Run("DeadlineExceeded sets conn deadline with DeadlineDelay", func(t *testing.T) {
4809 config, err := pgconn.ParseConfig(os.Getenv("PGX_TEST_DATABASE"))
4810 require.NoError(t, err)

Callers

nothing calls this directly

Calls 9

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

Tested by

no test coverage detected