Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement driver.Pinger interface #737

Merged
merged 1 commit into from
Mar 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions conn_go18.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ func (cn *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx,
return tx, nil
}

func (cn *conn) Ping(ctx context.Context) error {
if finish := cn.watchCancel(ctx); finish != nil {
defer finish()
}
rows, err := cn.simpleQuery("SELECT 'lib/pq ping test';")
if err != nil {
return driver.ErrBadConn // https://golang.org/pkg/database/sql/driver/#Pinger
}
rows.Close()
return nil
}

func (cn *conn) watchCancel(ctx context.Context) func() {
if done := ctx.Done(); done != nil {
finished := make(chan struct{})
Expand Down
69 changes: 69 additions & 0 deletions go19_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// +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)
}
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)
}
}