diff options
author | Daniel Theophanes <kardianos@gmail.com> | 2020-04-22 12:45:44 -0700 |
---|---|---|
committer | Dmitri Shuralyov <dmitshur@golang.org> | 2020-10-08 21:03:57 +0000 |
commit | eadc935508a39b1e6b0e2257960832df4c0ac79d (patch) | |
tree | 52ac3aff63457d15052c327f39dc412d514da701 | |
parent | 878da0bf881b94ce1405efade8802662d1c41674 (diff) | |
download | go-eadc935508a39b1e6b0e2257960832df4c0ac79d.tar.gz go-eadc935508a39b1e6b0e2257960832df4c0ac79d.zip |
[release-branch.go1.14] database/sql: de-flake TestTxCannotCommitAfterRollback
Do not cancel rows during test. Only cancel the Tx.
Correct the referenced issue number on the test.
Updates #38597.
Fixes #41815.
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>
(cherry picked from commit ed7888aea6021e25b0ea58bcad3f26da2b139432)
Reviewed-on: https://go-review.googlesource.com/c/go/+/259858
Trust: Dmitri Shuralyov <dmitshur@golang.org>
Trust: Emmanuel Odeke <emm.odeke@gmail.com>
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
-rw-r--r-- | src/database/sql/sql.go | 7 | ||||
-rw-r--r-- | src/database/sql/sql_test.go | 6 |
2 files changed, 12 insertions, 1 deletions
diff --git a/src/database/sql/sql.go b/src/database/sql/sql.go index a0b7ca8f08..74447d1945 100644 --- a/src/database/sql/sql.go +++ b/src/database/sql/sql.go @@ -2729,10 +2729,17 @@ func (rs *Rows) lasterrOrErrLocked(err error) error { 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) { if ctx.Done() == nil && (txctx == nil || txctx.Done() == nil) { return } + if bypassRowsAwaitDone { + return + } ctx, rs.cancel = context.WithCancel(ctx) go rs.awaitDone(ctx, txctx) } diff --git a/src/database/sql/sql_test.go b/src/database/sql/sql_test.go index a9e18004fb..7be5fc917a 100644 --- a/src/database/sql/sql_test.go +++ b/src/database/sql/sql_test.go @@ -2724,7 +2724,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) { db := newTestDB(t, "tx_status") defer closeDB(t, db) @@ -2766,6 +2766,9 @@ func TestTxCannotCommitAfterRollback(t *testing.T) { // 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). 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() { cancel() <-sendQuery @@ -2776,6 +2779,7 @@ func TestTxCannotCommitAfterRollback(t *testing.T) { defer func() { hookTxGrabConn = nil rollbackHook = nil + bypassRowsAwaitDone = false }() err = tx.QueryRow("SELECT|tx_status|tx_status|").Scan(&txStatus) |