From c931a3e63fc5ea35d8204e01cf26f1a00aacb930 Mon Sep 17 00:00:00 2001 From: Nia Weiss Date: Mon, 29 Mar 2021 13:06:49 -0400 Subject: [PATCH] fix DB-close race condition in TestTx_CommitRollback/SingleWriter Due to lack of synchronization, this test would sometimes close the DB before terminating a transaction: === RUN TestTx_CommitRollback/SingleWriter tx_test.go:132: db still has 1 active transactions; must closed before closing db The test now waits for the goroutines to terminate before closing the DB. --- rbf/tx_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/rbf/tx_test.go b/rbf/tx_test.go index 23ee02488..5d59fc587 100644 --- a/rbf/tx_test.go +++ b/rbf/tx_test.go @@ -18,6 +18,7 @@ import ( "fmt" "math" "math/rand" + "sync" "testing" "time" @@ -99,17 +100,24 @@ func TestTx_CommitRollback(t *testing.T) { db := MustOpenDB(t) defer MustCloseDB(t, db) + var wg sync.WaitGroup + defer wg.Wait() + // Start write transaction. ch0 := make(chan struct{}) tx0 := MustBegin(t, db, true) + wg.Add(1) go func() { + defer wg.Done() <-ch0 tx0.Rollback() }() // Start separate write transaction in different goroutine. ch1 := make(chan struct{}) + wg.Add(1) go func() { + defer wg.Done() tx1 := MustBegin(t, db, true) close(ch1) _ = tx1.Commit()