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

Function TestConnCheckConn

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

Source from the content-addressed store, hash-verified

3121}
3122
3123func TestConnCheckConn(t *testing.T) {
3124 t.Parallel()
3125
3126 ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
3127 defer cancel()
3128
3129 // Intentionally using TCP connection for more predictable close behavior. (Not sure if Unix domain sockets would behave subtly different.)
3130
3131 connString := os.Getenv("PGX_TEST_TCP_CONN_STRING")
3132 if connString == "" {
3133 t.Skipf("Skipping due to missing environment variable %v", "PGX_TEST_TCP_CONN_STRING")
3134 }
3135
3136 c1, err := pgconn.Connect(ctx, connString)
3137 require.NoError(t, err)
3138 defer c1.Close(ctx)
3139
3140 if c1.ParameterStatus("crdb_version") != "" {
3141 t.Skip("Server does not support pg_terminate_backend() (https://github.com/cockroachdb/cockroach/issues/35897)")
3142 }
3143
3144 err = c1.CheckConn()
3145 require.NoError(t, err)
3146
3147 c2, err := pgconn.Connect(ctx, connString)
3148 require.NoError(t, err)
3149 defer c2.Close(ctx)
3150
3151 _, err = c2.Exec(ctx, fmt.Sprintf("select pg_terminate_backend(%d)", c1.PID())).ReadAll()
3152 require.NoError(t, err)
3153
3154 // It may take a while for the server to kill the backend. Retry until the error is detected or the test context is
3155 // canceled.
3156 for err == nil && ctx.Err() == nil {
3157 time.Sleep(50 * time.Millisecond)
3158 err = c1.CheckConn()
3159 }
3160 require.Error(t, err)
3161}
3162
3163func TestConnPing(t *testing.T) {
3164 t.Parallel()

Callers

nothing calls this directly

Calls 9

ConnectFunction · 0.92
ParameterStatusMethod · 0.80
CheckConnMethod · 0.80
ReadAllMethod · 0.80
PIDMethod · 0.80
CloseMethod · 0.65
ExecMethod · 0.65
ErrMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected