diff --git a/bench_test.go b/bench_test.go index d0362e20..6a3d0ad6 100644 --- a/bench_test.go +++ b/bench_test.go @@ -7,21 +7,7 @@ import ( "testing" ) -var testJoinsDataLoaded bool -var narrowTestDataLoaded bool -var int2TextVsBinaryTestDataLoaded bool -var int4TextVsBinaryTestDataLoaded bool -var int8TextVsBinaryTestDataLoaded bool -var float4TextVsBinaryTestDataLoaded bool -var float8TextVsBinaryTestDataLoaded bool -var boolTextVsBinaryTestDataLoaded bool -var timestampTzTextVsBinaryTestDataLoaded bool - func createNarrowTestData(b *testing.B, conn *pgx.Conn) { - if narrowTestDataLoaded { - return - } - mustExecute(b, conn, ` drop table if exists narrow; @@ -43,8 +29,6 @@ func createNarrowTestData(b *testing.B, conn *pgx.Conn) { mustPrepare(b, conn, "getNarrowById", "select * from narrow where id=$1") mustPrepare(b, conn, "getMultipleNarrowById", "select * from narrow where id between $1 and $2") mustPrepare(b, conn, "getMultipleNarrowByIdAsJSON", "select json_agg(row_to_json(narrow)) from narrow where id between $1 and $2") - - narrowTestDataLoaded = true } func removeBinaryEncoders() (encoders map[pgx.Oid]func(*pgx.MessageReader, int32) interface{}) { @@ -63,7 +47,8 @@ func restoreBinaryEncoders(encoders map[pgx.Oid]func(*pgx.MessageReader, int32) } func BenchmarkSelectRowSimpleNarrow(b *testing.B) { - conn := getSharedConnection(b) + conn := mustConnect(b, *defaultConnConfig) + defer closeConn(b, conn) createNarrowTestData(b, conn) // Get random ids outside of timing @@ -79,7 +64,8 @@ func BenchmarkSelectRowSimpleNarrow(b *testing.B) { } func BenchmarkSelectRowPreparedNarrow(b *testing.B) { - conn := getSharedConnection(b) + conn := mustConnect(b, *defaultConnConfig) + defer closeConn(b, conn) createNarrowTestData(b, conn) // Get random ids outside of timing @@ -95,7 +81,8 @@ func BenchmarkSelectRowPreparedNarrow(b *testing.B) { } func BenchmarkSelectRowsSimpleNarrow(b *testing.B) { - conn := getSharedConnection(b) + conn := mustConnect(b, *defaultConnConfig) + defer closeConn(b, conn) createNarrowTestData(b, conn) // Get random ids outside of timing @@ -111,7 +98,8 @@ func BenchmarkSelectRowsSimpleNarrow(b *testing.B) { } func BenchmarkSelectRowsPreparedNarrow(b *testing.B) { - conn := getSharedConnection(b) + conn := mustConnect(b, *defaultConnConfig) + defer closeConn(b, conn) createNarrowTestData(b, conn) // Get random ids outside of timing @@ -127,7 +115,8 @@ func BenchmarkSelectRowsPreparedNarrow(b *testing.B) { } func BenchmarkSelectValuePreparedNarrow(b *testing.B) { - conn := getSharedConnection(b) + conn := mustConnect(b, *defaultConnConfig) + defer closeConn(b, conn) createNarrowTestData(b, conn) // Get random ids outside of timing @@ -143,7 +132,8 @@ func BenchmarkSelectValuePreparedNarrow(b *testing.B) { } func BenchmarkSelectValueToPreparedNarrow(b *testing.B) { - conn := getSharedConnection(b) + conn := mustConnect(b, *defaultConnConfig) + defer closeConn(b, conn) createNarrowTestData(b, conn) // Get random ids outside of timing @@ -159,10 +149,6 @@ func BenchmarkSelectValueToPreparedNarrow(b *testing.B) { } func createJoinsTestData(b *testing.B, conn *pgx.Conn) { - if testJoinsDataLoaded { - return - } - mustExecute(b, conn, ` drop table if exists product_component; drop table if exists component; @@ -225,12 +211,11 @@ func createJoinsTestData(b *testing.B, conn *pgx.Conn) { having sum(weight*quantity) > 10 order by total_cost desc `) - - testJoinsDataLoaded = true } func BenchmarkSelectRowsSimpleJoins(b *testing.B) { - conn := getSharedConnection(b) + conn := mustConnect(b, *defaultConnConfig) + defer closeConn(b, conn) createJoinsTestData(b, conn) sql := ` @@ -250,7 +235,8 @@ func BenchmarkSelectRowsSimpleJoins(b *testing.B) { } func BenchmarkSelectRowsPreparedJoins(b *testing.B) { - conn := getSharedConnection(b) + conn := mustConnect(b, *defaultConnConfig) + defer closeConn(b, conn) createJoinsTestData(b, conn) b.ResetTimer() @@ -260,10 +246,6 @@ func BenchmarkSelectRowsPreparedJoins(b *testing.B) { } func createInt2TextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { - if int2TextVsBinaryTestDataLoaded { - return - } - mustExecute(b, conn, ` drop table if exists t; @@ -280,19 +262,17 @@ func createInt2TextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { (random() * 32000)::int2, (random() * 32000)::int2, (random() * 32000)::int2, (random() * 32000)::int2, (random() * 32000)::int2 from generate_series(1, 10); `) - - int2TextVsBinaryTestDataLoaded = true } func BenchmarkInt2Text(b *testing.B) { - conn := getSharedConnection(b) + conn := mustConnect(b, *defaultConnConfig) + defer closeConn(b, conn) createInt2TextVsBinaryTestData(b, conn) encoders := removeBinaryEncoders() defer func() { restoreBinaryEncoders(encoders) }() mustPrepare(b, conn, "selectInt16", "select * from t") - defer func() { conn.Deallocate("selectInt16") }() b.ResetTimer() for i := 0; i < b.N; i++ { @@ -301,10 +281,11 @@ func BenchmarkInt2Text(b *testing.B) { } func BenchmarkInt2Binary(b *testing.B) { - conn := getSharedConnection(b) + conn := mustConnect(b, *defaultConnConfig) + defer closeConn(b, conn) createInt2TextVsBinaryTestData(b, conn) + mustPrepare(b, conn, "selectInt16", "select * from t") - defer func() { conn.Deallocate("selectInt16") }() b.ResetTimer() for i := 0; i < b.N; i++ { @@ -313,10 +294,6 @@ func BenchmarkInt2Binary(b *testing.B) { } func createInt4TextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { - if int4TextVsBinaryTestDataLoaded { - return - } - mustExecute(b, conn, ` drop table if exists t; @@ -333,19 +310,17 @@ func createInt4TextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { (random() * 1000000)::int4, (random() * 1000000)::int4, (random() * 1000000)::int4, (random() * 1000000)::int4, (random() * 1000000)::int4 from generate_series(1, 10); `) - - int4TextVsBinaryTestDataLoaded = true } func BenchmarkInt4Text(b *testing.B) { - conn := getSharedConnection(b) + conn := mustConnect(b, *defaultConnConfig) + defer closeConn(b, conn) createInt4TextVsBinaryTestData(b, conn) encoders := removeBinaryEncoders() defer func() { restoreBinaryEncoders(encoders) }() mustPrepare(b, conn, "selectInt32", "select * from t") - defer func() { conn.Deallocate("selectInt32") }() b.ResetTimer() for i := 0; i < b.N; i++ { @@ -354,10 +329,11 @@ func BenchmarkInt4Text(b *testing.B) { } func BenchmarkInt4Binary(b *testing.B) { - conn := getSharedConnection(b) + conn := mustConnect(b, *defaultConnConfig) + defer closeConn(b, conn) createInt4TextVsBinaryTestData(b, conn) + mustPrepare(b, conn, "selectInt32", "select * from t") - defer func() { conn.Deallocate("selectInt32") }() b.ResetTimer() for i := 0; i < b.N; i++ { @@ -366,10 +342,6 @@ func BenchmarkInt4Binary(b *testing.B) { } func createInt8TextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { - if int8TextVsBinaryTestDataLoaded { - return - } - mustExecute(b, conn, ` drop table if exists t; @@ -386,19 +358,17 @@ func createInt8TextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { (random() * 1000000)::int8, (random() * 1000000)::int8, (random() * 1000000)::int8, (random() * 1000000)::int8, (random() * 1000000)::int8 from generate_series(1, 10); `) - - int8TextVsBinaryTestDataLoaded = true } func BenchmarkInt8Text(b *testing.B) { - conn := getSharedConnection(b) + conn := mustConnect(b, *defaultConnConfig) + defer closeConn(b, conn) createInt8TextVsBinaryTestData(b, conn) encoders := removeBinaryEncoders() defer func() { restoreBinaryEncoders(encoders) }() mustPrepare(b, conn, "selectInt64", "select * from t") - defer func() { conn.Deallocate("selectInt64") }() b.ResetTimer() for i := 0; i < b.N; i++ { @@ -407,10 +377,10 @@ func BenchmarkInt8Text(b *testing.B) { } func BenchmarkInt8Binary(b *testing.B) { - conn := getSharedConnection(b) + conn := mustConnect(b, *defaultConnConfig) + defer closeConn(b, conn) createInt8TextVsBinaryTestData(b, conn) mustPrepare(b, conn, "selectInt64", "select * from t") - defer func() { conn.Deallocate("selectInt64") }() b.ResetTimer() for i := 0; i < b.N; i++ { @@ -419,10 +389,6 @@ func BenchmarkInt8Binary(b *testing.B) { } func createFloat4TextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { - if float4TextVsBinaryTestDataLoaded { - return - } - mustExecute(b, conn, ` drop table if exists t; @@ -439,19 +405,17 @@ func createFloat4TextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { (random() * 1000000)::float4, (random() * 1000000)::float4, (random() * 1000000)::float4, (random() * 1000000)::float4, (random() * 1000000)::float4 from generate_series(1, 10); `) - - float4TextVsBinaryTestDataLoaded = true } func BenchmarkFloat4Text(b *testing.B) { - conn := getSharedConnection(b) + conn := mustConnect(b, *defaultConnConfig) + defer closeConn(b, conn) createFloat4TextVsBinaryTestData(b, conn) encoders := removeBinaryEncoders() defer func() { restoreBinaryEncoders(encoders) }() mustPrepare(b, conn, "selectFloat32", "select * from t") - defer func() { conn.Deallocate("selectFloat32") }() b.ResetTimer() for i := 0; i < b.N; i++ { @@ -460,10 +424,10 @@ func BenchmarkFloat4Text(b *testing.B) { } func BenchmarkFloat4Binary(b *testing.B) { - conn := getSharedConnection(b) + conn := mustConnect(b, *defaultConnConfig) + defer closeConn(b, conn) createFloat4TextVsBinaryTestData(b, conn) mustPrepare(b, conn, "selectFloat32", "select * from t") - defer func() { conn.Deallocate("selectFloat32") }() b.ResetTimer() for i := 0; i < b.N; i++ { @@ -472,10 +436,6 @@ func BenchmarkFloat4Binary(b *testing.B) { } func createFloat8TextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { - if float8TextVsBinaryTestDataLoaded { - return - } - mustExecute(b, conn, ` drop table if exists t; @@ -492,19 +452,17 @@ func createFloat8TextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { (random() * 1000000)::float8, (random() * 1000000)::float8, (random() * 1000000)::float8, (random() * 1000000)::float8, (random() * 1000000)::float8 from generate_series(1, 10); `) - - float8TextVsBinaryTestDataLoaded = true } func BenchmarkFloat8Text(b *testing.B) { - conn := getSharedConnection(b) + conn := mustConnect(b, *defaultConnConfig) + defer closeConn(b, conn) createFloat8TextVsBinaryTestData(b, conn) encoders := removeBinaryEncoders() defer func() { restoreBinaryEncoders(encoders) }() mustPrepare(b, conn, "selectFloat32", "select * from t") - defer func() { conn.Deallocate("selectFloat32") }() b.ResetTimer() for i := 0; i < b.N; i++ { @@ -513,10 +471,10 @@ func BenchmarkFloat8Text(b *testing.B) { } func BenchmarkFloat8Binary(b *testing.B) { - conn := getSharedConnection(b) + conn := mustConnect(b, *defaultConnConfig) + defer closeConn(b, conn) createFloat8TextVsBinaryTestData(b, conn) mustPrepare(b, conn, "selectFloat32", "select * from t") - defer func() { conn.Deallocate("selectFloat32") }() b.ResetTimer() for i := 0; i < b.N; i++ { @@ -525,10 +483,6 @@ func BenchmarkFloat8Binary(b *testing.B) { } func createBoolTextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { - if boolTextVsBinaryTestDataLoaded { - return - } - mustExecute(b, conn, ` drop table if exists t; @@ -545,19 +499,17 @@ func createBoolTextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { random() > 0.5, random() > 0.5, random() > 0.5, random() > 0.5, random() > 0.5 from generate_series(1, 10); `) - - boolTextVsBinaryTestDataLoaded = true } func BenchmarkBoolText(b *testing.B) { - conn := getSharedConnection(b) + conn := mustConnect(b, *defaultConnConfig) + defer closeConn(b, conn) createBoolTextVsBinaryTestData(b, conn) encoders := removeBinaryEncoders() defer func() { restoreBinaryEncoders(encoders) }() mustPrepare(b, conn, "selectBool", "select * from t") - defer func() { conn.Deallocate("selectBool") }() b.ResetTimer() for i := 0; i < b.N; i++ { @@ -566,10 +518,10 @@ func BenchmarkBoolText(b *testing.B) { } func BenchmarkBoolBinary(b *testing.B) { - conn := getSharedConnection(b) + conn := mustConnect(b, *defaultConnConfig) + defer closeConn(b, conn) createBoolTextVsBinaryTestData(b, conn) mustPrepare(b, conn, "selectBool", "select * from t") - defer func() { conn.Deallocate("selectBool") }() b.ResetTimer() for i := 0; i < b.N; i++ { @@ -578,10 +530,6 @@ func BenchmarkBoolBinary(b *testing.B) { } func createTimestampTzTextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { - if timestampTzTextVsBinaryTestDataLoaded { - return - } - mustExecute(b, conn, ` drop table if exists t; @@ -602,19 +550,17 @@ func createTimestampTzTextVsBinaryTestData(b *testing.B, conn *pgx.Conn) { now() - '10 years'::interval * random() from generate_series(1, 10); `) - - timestampTzTextVsBinaryTestDataLoaded = true } func BenchmarkTimestampTzText(b *testing.B) { - conn := getSharedConnection(b) + conn := mustConnect(b, *defaultConnConfig) + defer closeConn(b, conn) createTimestampTzTextVsBinaryTestData(b, conn) encoders := removeBinaryEncoders() defer func() { restoreBinaryEncoders(encoders) }() mustPrepare(b, conn, "selectTimestampTz", "select * from t") - defer func() { conn.Deallocate("selectTimestampTz") }() b.ResetTimer() for i := 0; i < b.N; i++ { @@ -623,10 +569,10 @@ func BenchmarkTimestampTzText(b *testing.B) { } func BenchmarkTimestampTzBinary(b *testing.B) { - conn := getSharedConnection(b) + conn := mustConnect(b, *defaultConnConfig) + defer closeConn(b, conn) createTimestampTzTextVsBinaryTestData(b, conn) mustPrepare(b, conn, "selectTimestampTz", "select * from t") - defer func() { conn.Deallocate("selectTimestampTz") }() b.ResetTimer() for i := 0; i < b.N; i++ { @@ -650,5 +596,4 @@ func BenchmarkConnPool(b *testing.B) { } pool.Release(conn) } - } diff --git a/conn_test.go b/conn_test.go index d5bd350d..07a73de4 100644 --- a/conn_test.go +++ b/conn_test.go @@ -216,7 +216,8 @@ func TestParseURI(t *testing.T) { } func TestExecute(t *testing.T) { - conn := getSharedConnection(t) + conn := mustConnect(t, *defaultConnConfig) + defer closeConn(t, conn) if results := mustExecute(t, conn, "create temporary table foo(id integer primary key);"); results != "CREATE TABLE" { t.Error("Unexpected results from Execute") @@ -243,11 +244,8 @@ func TestExecute(t *testing.T) { } func TestExecuteFailure(t *testing.T) { - conn, err := pgx.Connect(*defaultConnConfig) - if err != nil { - t.Fatalf("Unable to establish connection: %v", err) - } - defer conn.Close() + conn := mustConnect(t, *defaultConnConfig) + defer closeConn(t, conn) if _, err := conn.Execute("select;"); err == nil { t.Fatal("Expected SQL syntax error") @@ -259,7 +257,8 @@ func TestExecuteFailure(t *testing.T) { } func TestSelectFunc(t *testing.T) { - conn := getSharedConnection(t) + conn := mustConnect(t, *defaultConnConfig) + defer closeConn(t, conn) var sum, rowCount int32 onDataRow := func(r *pgx.DataRowReader) error { @@ -281,11 +280,8 @@ func TestSelectFunc(t *testing.T) { } func TestSelectFuncFailure(t *testing.T) { - conn, err := pgx.Connect(*defaultConnConfig) - if err != nil { - t.Fatalf("Unable to establish connection: %v", err) - } - defer conn.Close() + conn := mustConnect(t, *defaultConnConfig) + defer closeConn(t, conn) // using SelectValue as it delegates to SelectFunc and is easier to work with if _, err := conn.SelectValue("select;"); err == nil { @@ -322,7 +318,8 @@ func Example_connectionSelectFunc() { } func TestSelectRows(t *testing.T) { - conn := getSharedConnection(t) + conn := mustConnect(t, *defaultConnConfig) + defer closeConn(t, conn) rows := mustSelectRows(t, conn, "select $1 as name, null as position", "Jack") @@ -367,7 +364,8 @@ func Example_connectionSelectRows() { } func TestSelectRow(t *testing.T) { - conn := getSharedConnection(t) + conn := mustConnect(t, *defaultConnConfig) + defer closeConn(t, conn) row := mustSelectRow(t, conn, "select $1 as name, null as position", "Jack") if row["name"] != "Jack" { @@ -394,7 +392,8 @@ func TestSelectRow(t *testing.T) { } func TestConnectionSelectValue(t *testing.T) { - conn := getSharedConnection(t) + conn := mustConnect(t, *defaultConnConfig) + defer closeConn(t, conn) test := func(sql string, expected interface{}, arguments ...interface{}) { v, err := conn.SelectValue(sql, arguments...) @@ -434,7 +433,9 @@ func TestConnectionSelectValue(t *testing.T) { } func TestConnectionSelectValueTo(t *testing.T) { - conn := getSharedConnection(t) + conn := mustConnect(t, *defaultConnConfig) + defer closeConn(t, conn) + var err error var buf bytes.Buffer @@ -481,7 +482,8 @@ func TestConnectionSelectValueTo(t *testing.T) { } func TestSelectValues(t *testing.T) { - conn := getSharedConnection(t) + conn := mustConnect(t, *defaultConnConfig) + defer closeConn(t, conn) test := func(sql string, expected []interface{}, arguments ...interface{}) { values, err := conn.SelectValues(sql, arguments...) @@ -513,14 +515,11 @@ func TestSelectValues(t *testing.T) { } func TestPrepare(t *testing.T) { - conn, err := pgx.Connect(*defaultConnConfig) - if err != nil { - t.Fatalf("Unable to establish connection: %v", err) - } - defer conn.Close() + conn := mustConnect(t, *defaultConnConfig) + defer closeConn(t, conn) testTranscode := func(sql string, value interface{}) { - if _, err = conn.Prepare("testTranscode", sql); err != nil { + if _, err := conn.Prepare("testTranscode", sql); err != nil { t.Errorf("Unable to prepare statement: %v", err) return } @@ -531,8 +530,7 @@ func TestPrepare(t *testing.T) { } }() - var result interface{} - result, err = conn.SelectValue("testTranscode", value) + result, err := conn.SelectValue("testTranscode", value) if err != nil { t.Errorf("%v while running %v", err, "testTranscode") } else { @@ -555,7 +553,7 @@ func TestPrepare(t *testing.T) { // Ensure that unknown types are just treated as strings testTranscode("select $1::point", "(0,0)") - if _, err = conn.Prepare("testByteSliceTranscode", "select $1::bytea"); err != nil { + if _, err := conn.Prepare("testByteSliceTranscode", "select $1::bytea"); err != nil { t.Errorf("Unable to prepare statement: %v", err) return } @@ -577,8 +575,8 @@ func TestPrepare(t *testing.T) { } else if sql != `select E'\\x000fff11'` { t.Error("Failed to sanitize []byte") } - var result interface{} - result, err = conn.SelectValue("testByteSliceTranscode", bytea) + + result, err := conn.SelectValue("testByteSliceTranscode", bytea) if err != nil { t.Errorf("%v while running %v", err, "testByteSliceTranscode") } else { @@ -594,27 +592,21 @@ func TestPrepare(t *testing.T) { } func TestPrepareFailure(t *testing.T) { - conn, err := pgx.Connect(*defaultConnConfig) - if err != nil { - t.Fatalf("Unable to establish connection: %v", err) - } - defer conn.Close() + conn := mustConnect(t, *defaultConnConfig) + defer closeConn(t, conn) - if _, err = conn.Prepare("badSQL", "select foo"); err == nil { + if _, err := conn.Prepare("badSQL", "select foo"); err == nil { t.Fatal("Prepare should have failed with syntax error") } - if _, err = conn.SelectValue("select 1"); err != nil { + if _, err := conn.SelectValue("select 1"); err != nil { t.Fatalf("Prepare failure appears to have broken connection: %v", err) } } func TestTransaction(t *testing.T) { - conn, err := pgx.Connect(*defaultConnConfig) - if err != nil { - t.Fatalf("Unable to establish connection: %v", err) - } - defer conn.Close() + conn := mustConnect(t, *defaultConnConfig) + defer closeConn(t, conn) createSql := ` create temporary table foo( @@ -627,10 +619,8 @@ func TestTransaction(t *testing.T) { t.Fatalf("Failed to create table: %v", err) } - var committed bool - // Transaction happy path -- it executes function and commits - committed, err = conn.Transaction(func() bool { + committed, err := conn.Transaction(func() bool { mustExecute(t, conn, "insert into foo(id) values (1)") return true }) @@ -721,11 +711,8 @@ func TestTransaction(t *testing.T) { } func TestTransactionIso(t *testing.T) { - conn, err := pgx.Connect(*defaultConnConfig) - if err != nil { - t.Fatalf("Unable to establish connection: %v", err) - } - defer conn.Close() + conn := mustConnect(t, *defaultConnConfig) + defer closeConn(t, conn) isoLevels := []string{pgx.Serializable, pgx.RepeatableRead, pgx.ReadCommitted, pgx.ReadUncommitted} for _, iso := range isoLevels { @@ -742,17 +729,16 @@ func TestTransactionIso(t *testing.T) { } func TestListenNotify(t *testing.T) { - listener, err := pgx.Connect(*defaultConnConfig) - if err != nil { - t.Fatalf("Unable to establish connection: %v", err) - } - defer listener.Close() + listener := mustConnect(t, *defaultConnConfig) + defer closeConn(t, listener) if err := listener.Listen("chat"); err != nil { t.Fatalf("Unable to start listening: %v", err) } - notifier := getSharedConnection(t) + notifier := mustConnect(t, *defaultConnConfig) + defer closeConn(t, notifier) + mustExecute(t, notifier, "notify chat") // when notification is waiting on the socket to be read @@ -796,11 +782,8 @@ func TestListenNotify(t *testing.T) { } func TestFatalRxError(t *testing.T) { - conn, err := pgx.Connect(*defaultConnConfig) - if err != nil { - t.Fatalf("Unable to establish connection: %v", err) - } - defer conn.Close() + conn := mustConnect(t, *defaultConnConfig) + defer closeConn(t, conn) var wg sync.WaitGroup wg.Add(1) @@ -830,11 +813,8 @@ func TestFatalRxError(t *testing.T) { } func TestFatalTxError(t *testing.T) { - conn, err := pgx.Connect(*defaultConnConfig) - if err != nil { - t.Fatalf("Unable to establish connection: %v", err) - } - defer conn.Close() + conn := mustConnect(t, *defaultConnConfig) + defer closeConn(t, conn) otherConn, err := pgx.Connect(*defaultConnConfig) if err != nil { diff --git a/data_row_reader_test.go b/data_row_reader_test.go index 35980624..49a14d67 100644 --- a/data_row_reader_test.go +++ b/data_row_reader_test.go @@ -6,7 +6,8 @@ import ( ) func TestDataRowReaderReadValue(t *testing.T) { - conn := getSharedConnection(t) + conn := mustConnect(t, *defaultConnConfig) + defer closeConn(t, conn) test := func(sql string, expected interface{}) { var v interface{} diff --git a/helper_test.go b/helper_test.go index 057c9a9e..f3960ea1 100644 --- a/helper_test.go +++ b/helper_test.go @@ -6,18 +6,19 @@ import ( "testing" ) -var sharedConnection *pgx.Conn - -func getSharedConnection(t testing.TB) (c *pgx.Conn) { - if sharedConnection == nil || !sharedConnection.IsAlive() { - var err error - sharedConnection, err = pgx.Connect(*defaultConnConfig) - if err != nil { - t.Fatalf("Unable to establish connection: %v", err) - } - +func mustConnect(t testing.TB, config pgx.ConnConfig) *pgx.Conn { + conn, err := pgx.Connect(config) + if err != nil { + t.Fatalf("Unable to establish connection: %v", err) + } + return conn +} + +func closeConn(t testing.TB, conn *pgx.Conn) { + err := conn.Close() + if err != nil { + t.Fatalf("conn.Close unexpectedly failed: %v", err) } - return sharedConnection } func mustPrepare(t testing.TB, conn *pgx.Conn, name, sql string) { diff --git a/sanitize_test.go b/sanitize_test.go index ef02db99..f49ff22c 100644 --- a/sanitize_test.go +++ b/sanitize_test.go @@ -5,7 +5,8 @@ import ( ) func TestQuoteString(t *testing.T) { - conn := getSharedConnection(t) + conn := mustConnect(t, *defaultConnConfig) + defer closeConn(t, conn) if conn.QuoteString("test") != "'test'" { t.Error("Failed to quote string") @@ -17,7 +18,8 @@ func TestQuoteString(t *testing.T) { } func TestSanitizeSql(t *testing.T) { - conn := getSharedConnection(t) + conn := mustConnect(t, *defaultConnConfig) + defer closeConn(t, conn) if san, err := conn.SanitizeSql("select $1", nil); err != nil || san != "select null" { t.Errorf("Failed to translate nil to null: %v - %v", san, err) diff --git a/value_transcoder_test.go b/value_transcoder_test.go index 660e7f2a..ec2efa19 100644 --- a/value_transcoder_test.go +++ b/value_transcoder_test.go @@ -6,7 +6,8 @@ import ( ) func TestTranscodeError(t *testing.T) { - conn := getSharedConnection(t) + conn := mustConnect(t, *defaultConnConfig) + defer closeConn(t, conn) mustPrepare(t, conn, "testTranscode", "select $1::integer") defer func() { @@ -27,7 +28,8 @@ func TestTranscodeError(t *testing.T) { } func TestNilTranscode(t *testing.T) { - conn := getSharedConnection(t) + conn := mustConnect(t, *defaultConnConfig) + defer closeConn(t, conn) var inputNil interface{} inputNil = nil @@ -51,7 +53,8 @@ func TestNilTranscode(t *testing.T) { } func TestDateTranscode(t *testing.T) { - conn := getSharedConnection(t) + conn := mustConnect(t, *defaultConnConfig) + defer closeConn(t, conn) actualDate := time.Date(2013, 1, 2, 0, 0, 0, 0, time.Local) @@ -79,7 +82,8 @@ func TestDateTranscode(t *testing.T) { } func TestTimestampTzTranscode(t *testing.T) { - conn := getSharedConnection(t) + conn := mustConnect(t, *defaultConnConfig) + defer closeConn(t, conn) inputTime := time.Date(2013, 1, 2, 3, 4, 5, 6000, time.Local) @@ -118,7 +122,8 @@ func TestInt2SliceTranscode(t *testing.T) { } } - conn := getSharedConnection(t) + conn := mustConnect(t, *defaultConnConfig) + defer closeConn(t, conn) inputNumbers := []int16{1, 2, 3, 4, 5, 6, 7, 8} var outputNumbers []int16 @@ -149,7 +154,8 @@ func TestInt4SliceTranscode(t *testing.T) { } } - conn := getSharedConnection(t) + conn := mustConnect(t, *defaultConnConfig) + defer closeConn(t, conn) inputNumbers := []int32{1, 2, 3, 4, 5, 6, 7, 8} var outputNumbers []int32 @@ -180,7 +186,8 @@ func TestInt8SliceTranscode(t *testing.T) { } } - conn := getSharedConnection(t) + conn := mustConnect(t, *defaultConnConfig) + defer closeConn(t, conn) inputNumbers := []int64{1, 2, 3, 4, 5, 6, 7, 8} var outputNumbers []int64