Add driver.Pinger support to stdlib.Conn
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx"
|
||||
"github.com/jackc/pgx/pgmock"
|
||||
@@ -827,3 +828,52 @@ func TestAcquireConn(t *testing.T) {
|
||||
|
||||
ensureConnValid(t, db)
|
||||
}
|
||||
|
||||
func TestConnPingContextSuccess(t *testing.T) {
|
||||
db := openDB(t)
|
||||
defer closeDB(t, db)
|
||||
|
||||
if err := db.PingContext(context.Background()); err != nil {
|
||||
t.Fatalf("db.PingContext failed: %v", err)
|
||||
}
|
||||
|
||||
ensureConnValid(t, db)
|
||||
}
|
||||
|
||||
func TestConnPingContextCancel(t *testing.T) {
|
||||
script := &pgmock.Script{
|
||||
Steps: pgmock.AcceptUnauthenticatedConnRequestSteps(),
|
||||
}
|
||||
script.Steps = append(script.Steps, pgmock.PgxInitSteps()...)
|
||||
script.Steps = append(script.Steps,
|
||||
pgmock.ExpectMessage(&pgproto3.Query{String: ";"}),
|
||||
)
|
||||
|
||||
server, err := pgmock.NewServer(script)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer server.Close()
|
||||
|
||||
errChan := make(chan error)
|
||||
go func() {
|
||||
errChan <- server.ServeOne()
|
||||
}()
|
||||
|
||||
db, err := sql.Open("pgx", fmt.Sprintf("postgres://pgx_md5:secret@%s/pgx_test?sslmode=disable", server.Addr()))
|
||||
if err != nil {
|
||||
t.Fatalf("sql.Open failed: %v", err)
|
||||
}
|
||||
// defer closeDB(t, db) // mock DB doesn't close correctly yet
|
||||
|
||||
ctx, _ := context.WithTimeout(context.Background(), 100*time.Millisecond)
|
||||
|
||||
err = db.PingContext(ctx)
|
||||
if err != context.DeadlineExceeded {
|
||||
t.Errorf("err => %v, want %v", err, context.DeadlineExceeded)
|
||||
}
|
||||
|
||||
if err := <-errChan; err != nil {
|
||||
t.Errorf("mock server err: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user