From 72fbf23deaae79765e2be17a73b3377008326d62 Mon Sep 17 00:00:00 2001 From: James Hartig Date: Fri, 22 Dec 2023 15:03:54 -0600 Subject: [PATCH] update description cache after exec prepare --- conn.go | 1 + conn_test.go | 42 ++++++++++++++++++++++-------------------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/conn.go b/conn.go index d85cf127f..64ae48cab 100644 --- a/conn.go +++ b/conn.go @@ -513,6 +513,7 @@ optionLoop: if err != nil { return pgconn.CommandTag{}, err } + c.descriptionCache.Put(sd) } return c.execParams(ctx, sd, arguments) diff --git a/conn_test.go b/conn_test.go index 989249757..a7f7f2f88 100644 --- a/conn_test.go +++ b/conn_test.go @@ -482,31 +482,33 @@ func TestPrepareBadSQLFailure(t *testing.T) { func TestPrepareIdempotency(t *testing.T) { t.Parallel() - conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) - defer closeConn(t, conn) + ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second) + defer cancel() - for i := 0; i < 2; i++ { - _, err := conn.Prepare(context.Background(), "test", "select 42::integer") - if err != nil { - t.Fatalf("%d. Unable to prepare statement: %v", i, err) - } + pgxtest.RunWithQueryExecModes(ctx, t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) { + for i := 0; i < 2; i++ { + _, err := conn.Prepare(context.Background(), "test", "select 42::integer") + if err != nil { + t.Fatalf("%d. Unable to prepare statement: %v", i, err) + } - var n int32 - err = conn.QueryRow(context.Background(), "test").Scan(&n) - if err != nil { - t.Errorf("%d. Executing prepared statement failed: %v", i, err) - } + var n int32 + err = conn.QueryRow(context.Background(), "test").Scan(&n) + if err != nil { + t.Errorf("%d. Executing prepared statement failed: %v", i, err) + } - if n != int32(42) { - t.Errorf("%d. Prepared statement did not return expected value: %v", i, n) + if n != int32(42) { + t.Errorf("%d. Prepared statement did not return expected value: %v", i, n) + } } - } - _, err := conn.Prepare(context.Background(), "test", "select 'fail'::varchar") - if err == nil { - t.Fatalf("Prepare statement with same name but different SQL should have failed but it didn't") - return - } + _, err := conn.Prepare(context.Background(), "test", "select 'fail'::varchar") + if err == nil { + t.Fatalf("Prepare statement with same name but different SQL should have failed but it didn't") + return + } + }) } func TestPrepareStatementCacheModes(t *testing.T) {