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

Function TestConnCancelRequest

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

Source from the content-addressed store, hash-verified

2827}
2828
2829func TestConnCancelRequest(t *testing.T) {
2830 t.Parallel()
2831
2832 ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
2833 defer cancel()
2834
2835 pgConn, err := pgconn.Connect(ctx, os.Getenv("PGX_TEST_DATABASE"))
2836 require.NoError(t, err)
2837 defer closeConn(t, pgConn)
2838
2839 if pgConn.ParameterStatus("crdb_version") != "" {
2840 t.Skip("Server does not support query cancellation (https://github.com/cockroachdb/cockroach/issues/41335)")
2841 }
2842
2843 multiResult := pgConn.Exec(ctx, "select 'Hello, world', pg_sleep(25)")
2844
2845 errChan := make(chan error)
2846 go func() {
2847 // The query is actually sent when multiResult.NextResult() is called. So wait to ensure it is sent.
2848 // Once Flush is available this could use that instead.
2849 time.Sleep(1 * time.Second)
2850
2851 err := pgConn.CancelRequest(ctx)
2852 errChan <- err
2853 }()
2854
2855 for multiResult.NextResult() {
2856 }
2857 err = multiResult.Close()
2858
2859 require.IsType(t, &pgconn.PgError{}, err)
2860 require.Equal(t, "57014", err.(*pgconn.PgError).Code)
2861
2862 err = <-errChan
2863 require.NoError(t, err)
2864
2865 ensureConnValid(t, pgConn)
2866}
2867
2868// https://github.com/jackc/pgx/issues/659
2869func TestConnContextCanceledCancelsRunningQueryOnServer(t *testing.T) {

Callers

nothing calls this directly

Calls 8

ConnectFunction · 0.92
ParameterStatusMethod · 0.80
CancelRequestMethod · 0.80
NextResultMethod · 0.80
closeConnFunction · 0.70
ensureConnValidFunction · 0.70
ExecMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected