Skip to content

Commit

Permalink
Merge pull request #8732 from ellemouton/guardNewTestSqliteDB
Browse files Browse the repository at this point in the history
invoices: ensure synchronous access to NewTestSqliteDB
  • Loading branch information
guggero authored May 7, 2024
2 parents 6bcbc8d + d4f6366 commit f523f52
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
9 changes: 9 additions & 0 deletions invoices/invoiceregistry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"database/sql"
"fmt"
"math"
"sync"
"testing"
"testing/quick"
"time"
Expand All @@ -23,6 +24,12 @@ import (
"github.com/stretchr/testify/require"
)

// sqliteConstructorMu is used to ensure that only one thread can call the
// sqldb.NewTestSqliteDB constructor at a time. This is a temporary workaround
// that can be removed once this race condition in the sqlite repo is resolved:
// https://gitlab.com/cznic/sqlite/-/issues/180
var sqliteConstructorMu sync.Mutex

// TestInvoiceRegistry is a master test which encompasses all tests using an
// InvoiceDB instance. The purpose of this test is to be able to run all tests
// with a custom DB instance, so that we can test the same logic with different
Expand Down Expand Up @@ -130,7 +137,9 @@ func TestInvoiceRegistry(t *testing.T) {

var db *sqldb.BaseDB
if sqlite {
sqliteConstructorMu.Lock()
db = sqldb.NewTestSqliteDB(t).BaseDB
sqliteConstructorMu.Unlock()
} else {
db = sqldb.NewTestPostgresDB(t, pgFixture).BaseDB
}
Expand Down
2 changes: 2 additions & 0 deletions invoices/invoices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ func TestInvoices(t *testing.T) {
makeSQLDB := func(t *testing.T, sqlite bool) invpkg.InvoiceDB {
var db *sqldb.BaseDB
if sqlite {
sqliteConstructorMu.Lock()
db = sqldb.NewTestSqliteDB(t).BaseDB
sqliteConstructorMu.Unlock()
} else {
db = sqldb.NewTestPostgresDB(t, pgFixture).BaseDB
}
Expand Down

0 comments on commit f523f52

Please sign in to comment.