mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
database/sql: de-flake TestTxCannotCommitAfterRollback
Do not cancel rows during test. Only cancel the Tx. Correct the referenced issue number on the test. Fixes #38597 Change-Id: I0e8ba1bf2a8ba638d121c9c6938501fec1d5e961 Reviewed-on: https://go-review.googlesource.com/c/go/+/229478 Run-TryBot: Daniel Theophanes <kardianos@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
parent
9a93baf4d7
commit
ed7888aea6
2 changed files with 12 additions and 1 deletions
|
|
@ -2793,10 +2793,17 @@ func (rs *Rows) lasterrOrErrLocked(err error) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// bypassRowsAwaitDone is only used for testing.
|
||||||
|
// If true, it will not close the Rows automatically from the context.
|
||||||
|
var bypassRowsAwaitDone = false
|
||||||
|
|
||||||
func (rs *Rows) initContextClose(ctx, txctx context.Context) {
|
func (rs *Rows) initContextClose(ctx, txctx context.Context) {
|
||||||
if ctx.Done() == nil && (txctx == nil || txctx.Done() == nil) {
|
if ctx.Done() == nil && (txctx == nil || txctx.Done() == nil) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if bypassRowsAwaitDone {
|
||||||
|
return
|
||||||
|
}
|
||||||
ctx, rs.cancel = context.WithCancel(ctx)
|
ctx, rs.cancel = context.WithCancel(ctx)
|
||||||
go rs.awaitDone(ctx, txctx)
|
go rs.awaitDone(ctx, txctx)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2742,7 +2742,7 @@ func TestManyErrBadConn(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Issue 34755: Ensure that a Tx cannot commit after a rollback.
|
// Issue 34775: Ensure that a Tx cannot commit after a rollback.
|
||||||
func TestTxCannotCommitAfterRollback(t *testing.T) {
|
func TestTxCannotCommitAfterRollback(t *testing.T) {
|
||||||
db := newTestDB(t, "tx_status")
|
db := newTestDB(t, "tx_status")
|
||||||
defer closeDB(t, db)
|
defer closeDB(t, db)
|
||||||
|
|
@ -2784,6 +2784,9 @@ func TestTxCannotCommitAfterRollback(t *testing.T) {
|
||||||
// 2. (A) Start a query, (B) begin Tx rollback through a ctx cancel.
|
// 2. (A) Start a query, (B) begin Tx rollback through a ctx cancel.
|
||||||
// 3. Check if 2.A has committed in Tx (pass) or outside of Tx (fail).
|
// 3. Check if 2.A has committed in Tx (pass) or outside of Tx (fail).
|
||||||
sendQuery := make(chan struct{})
|
sendQuery := make(chan struct{})
|
||||||
|
// The Tx status is returned through the row results, ensure
|
||||||
|
// that the rows results are not cancelled.
|
||||||
|
bypassRowsAwaitDone = true
|
||||||
hookTxGrabConn = func() {
|
hookTxGrabConn = func() {
|
||||||
cancel()
|
cancel()
|
||||||
<-sendQuery
|
<-sendQuery
|
||||||
|
|
@ -2794,6 +2797,7 @@ func TestTxCannotCommitAfterRollback(t *testing.T) {
|
||||||
defer func() {
|
defer func() {
|
||||||
hookTxGrabConn = nil
|
hookTxGrabConn = nil
|
||||||
rollbackHook = nil
|
rollbackHook = nil
|
||||||
|
bypassRowsAwaitDone = false
|
||||||
}()
|
}()
|
||||||
|
|
||||||
err = tx.QueryRow("SELECT|tx_status|tx_status|").Scan(&txStatus)
|
err = tx.QueryRow("SELECT|tx_status|tx_status|").Scan(&txStatus)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue