diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:36:04 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:36:04 +0000 |
commit | b09c6d56832eb1718c07d74abf3bc6ae3fe4e030 (patch) | |
tree | d2caec2610d4ea887803ec9e9c3cd77136c448ba /dependencies/pkg/mod/github.com/lib/pq@v1.10.7/go19_test.go | |
parent | Initial commit. (diff) | |
download | icingadb-upstream.tar.xz icingadb-upstream.zip |
Adding upstream version 1.1.0.upstream/1.1.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | dependencies/pkg/mod/github.com/lib/pq@v1.10.7/go19_test.go | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/dependencies/pkg/mod/github.com/lib/pq@v1.10.7/go19_test.go b/dependencies/pkg/mod/github.com/lib/pq@v1.10.7/go19_test.go new file mode 100644 index 0000000..f810e48 --- /dev/null +++ b/dependencies/pkg/mod/github.com/lib/pq@v1.10.7/go19_test.go @@ -0,0 +1,99 @@ +//go:build go1.9 +// +build go1.9 + +package pq + +import ( + "context" + "database/sql" + "database/sql/driver" + "reflect" + "testing" +) + +func TestPing(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + db := openTestConn(t) + defer db.Close() + + if _, ok := reflect.TypeOf(db).MethodByName("Conn"); !ok { + t.Skipf("Conn method undefined on type %T, skipping test (requires at least go1.9)", db) + } + + if err := db.PingContext(ctx); err != nil { + t.Fatal("expected Ping to succeed") + } + defer cancel() + + // grab a connection + conn, err := db.Conn(ctx) + if err != nil { + t.Fatal(err) + } + + // start a transaction and read backend pid of our connection + tx, err := conn.BeginTx(ctx, &sql.TxOptions{ + Isolation: sql.LevelDefault, + ReadOnly: true, + }) + if err != nil { + t.Fatal(err) + } + + rows, err := tx.Query("SELECT pg_backend_pid()") + if err != nil { + t.Fatal(err) + } + defer rows.Close() + + // read the pid from result + var pid int + for rows.Next() { + if err := rows.Scan(&pid); err != nil { + t.Fatal(err) + } + } + if rows.Err() != nil { + t.Fatal(err) + } + // Fail the transaction and make sure we can still ping. + if _, err := tx.Query("INVALID SQL"); err == nil { + t.Fatal("expected error") + } + if err := conn.PingContext(ctx); err != nil { + t.Fatal(err) + } + if err := tx.Rollback(); err != nil { + t.Fatal(err) + } + + // kill the process which handles our connection and test if the ping fails + if _, err := db.Exec("SELECT pg_terminate_backend($1)", pid); err != nil { + t.Fatal(err) + } + if err := conn.PingContext(ctx); err != driver.ErrBadConn { + t.Fatalf("expected error %s, instead got %s", driver.ErrBadConn, err) + } +} + +func TestCommitInFailedTransactionWithCancelContext(t *testing.T) { + db := openTestConn(t) + defer db.Close() + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + txn, err := db.BeginTx(ctx, nil) + if err != nil { + t.Fatal(err) + } + rows, err := txn.Query("SELECT error") + if err == nil { + rows.Close() + t.Fatal("expected failure") + } + err = txn.Commit() + if err != ErrInFailedTransaction { + t.Fatalf("expected ErrInFailedTransaction; got %#v", err) + } +} |