Skip to content

Commit

Permalink
Update Open func to support keepalive
Browse files Browse the repository at this point in the history
Got rid of OpenConnector for simplicity's sake.

Noticed "pq: current transaction is aborted" in go18_test.go:212,
but it doesn't seem to be related. The same issue popped up in
#921.
  • Loading branch information
marselester committed Oct 1, 2020
1 parent 51502e0 commit 5f9a0c9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
12 changes: 5 additions & 7 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ func (d *Driver) Open(name string) (driver.Conn, error) {
return Open(name)
}

// OpenConnector parses the name in the same format that Driver.Open
// parses the name parameter.
func (d *Driver) OpenConnector(name string) (driver.Connector, error) {
return NewConnector(name)
}

func init() {
sql.Register("postgres", &Driver{})
}
Expand Down Expand Up @@ -278,7 +272,11 @@ func (cn *conn) writeBuf(b byte) *writeBuf {
// Most users should only use it through database/sql package from the standard
// library.
func Open(dsn string) (_ driver.Conn, err error) {
return DialOpen(defaultDialer{}, dsn)
c, err := NewConnector(dsn)
if err != nil {
return nil, err
}
return c.open(context.Background())
}

// DialOpen opens a new connection to the database using a dialer.
Expand Down
53 changes: 41 additions & 12 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ import (
"time"
)

// testDriver is the Postgres database driver that doesn't implement DriverContext interface.
type testDriver struct{}

func (d *testDriver) Open(name string) (driver.Conn, error) {
return Open(name)
}

func init() {
sql.Register("postgres-test", &testDriver{})
}

type Fatalistic interface {
Fatal(args ...interface{})
}
Expand Down Expand Up @@ -59,7 +48,7 @@ func testConninfo(conninfo string) string {
}

func openTestConnConninfo(conninfo string) (*sql.DB, error) {
return sql.Open("postgres-test", testConninfo(conninfo))
return sql.Open("postgres", testConninfo(conninfo))
}

func openTestConn(t Fatalistic) *sql.DB {
Expand Down Expand Up @@ -151,6 +140,46 @@ func TestOpenURL(t *testing.T) {
testURL("postgresql://")
}

func TestOpen(t *testing.T) {
dsn := "keepalives_interval=10"
c, err := Open(dsn)
if err != nil {
t.Fatal(err)
}
defer c.Close()

d := c.(*conn).dialer.(defaultDialer)
want := 10 * time.Second
if want != d.d.KeepAlive {
t.Fatalf("expected: %v, got: %v", want, d.d.KeepAlive)
}
}

func TestSQLOpen(t *testing.T) {
dsn := "keepalives_interval=10"
db, err := sql.Open("postgres", dsn)
if err != nil {
t.Fatal(err)
}
defer db.Close()
if err = db.Ping(); err != nil {
t.Fatal(err)
}

drv := db.Driver()
c, err := drv.Open(dsn)
if err != nil {
t.Fatal(err)
}
defer c.Close()

d := c.(*conn).dialer.(defaultDialer)
want := 10 * time.Second
if want != d.d.KeepAlive {
t.Fatalf("expected: %v, got: %v", want, d.d.KeepAlive)
}
}

const pgpassFile = "/tmp/pqgotest_pgpass"

func TestPgpass(t *testing.T) {
Expand Down

0 comments on commit 5f9a0c9

Please sign in to comment.