Merge pull request #8732 from ellemouton/guardNewTestSqliteDB

invoices: ensure synchronous access to NewTestSqliteDB
This commit is contained in:
Oliver Gugger 2024-05-07 08:40:56 +02:00 committed by GitHub
commit f523f52ab4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 0 deletions

View File

@ -6,6 +6,7 @@ import (
"database/sql"
"fmt"
"math"
"sync"
"testing"
"testing/quick"
"time"
@ -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
@ -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
}

View File

@ -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
}