From 5f9a0c9107af01eeca5f19b957065570c6333bf9 Mon Sep 17 00:00:00 2001 From: marselester Date: Thu, 1 Oct 2020 11:54:48 -0400 Subject: [PATCH] Update Open func to support keepalive 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 https://github.com/lib/pq/pull/921. --- conn.go | 12 +++++------- conn_test.go | 53 ++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/conn.go b/conn.go index 63665156..571de61d 100644 --- a/conn.go +++ b/conn.go @@ -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{}) } @@ -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. diff --git a/conn_test.go b/conn_test.go index a6b6a3e5..82b18d12 100644 --- a/conn_test.go +++ b/conn_test.go @@ -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{}) } @@ -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 { @@ -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) {