Add *Tx.AfterClose hook
ConnPool now implements its connection with Tx via this hook instead of manipulating the internals of Tx.
This commit is contained in:
+36
@@ -3,6 +3,7 @@ package pgx_test
|
||||
import (
|
||||
"github.com/jackc/pgx"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestTransactionSuccessfulCommit(t *testing.T) {
|
||||
@@ -114,3 +115,38 @@ func TestBeginIso(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTxAfterClose(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
conn := mustConnect(t, *defaultConnConfig)
|
||||
defer closeConn(t, conn)
|
||||
|
||||
tx, err := conn.Begin()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var zeroTime, t1, t2 time.Time
|
||||
tx.AfterClose(func(tx *pgx.Tx) {
|
||||
t1 = time.Now()
|
||||
})
|
||||
|
||||
tx.AfterClose(func(tx *pgx.Tx) {
|
||||
t2 = time.Now()
|
||||
})
|
||||
|
||||
tx.Rollback()
|
||||
|
||||
if t1 == zeroTime {
|
||||
t.Error("First Tx.AfterClose callback not called")
|
||||
}
|
||||
|
||||
if t2 == zeroTime {
|
||||
t.Error("Second Tx.AfterClose callback not called")
|
||||
}
|
||||
|
||||
if t1.After(t2) {
|
||||
t.Error("AfterClose callbacks called out of order: %v, %v", t1, t2)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user