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.
This commit is contained in:
Nia Weiss 2021-03-29 13:06:49 -04:00
parent b835221ded
commit c931a3e63f
No known key found for this signature in database
GPG key ID: 895E83409BFDA1BB

View file

@ -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()