fix(integration_test): mutex-protect mdb.DB swap in integrationDB helper
The integrationDB helper hot-swaps mdb.DB without mutex protection. With the new GetDB() RLock, t.Cleanup goroutines writing mdb.DB = prev race against production goroutines calling GetDB(). Fix: acquire mu.Lock before the swap and in the Cleanup closure.
This commit is contained in:
@@ -77,12 +77,18 @@ func integrationDB(t *testing.T) *sql.DB {
|
||||
if _, err := conn.ExecContext(ctx2, `DELETE FROM delegations`); err != nil {
|
||||
t.Fatalf("cleanup: %v", err)
|
||||
}
|
||||
// Wire the package-level db.GetDB() so production helpers (recordLedgerInsert,
|
||||
// recordLedgerStatus) see the same connection.
|
||||
// Wire the package-level db.DB so production helpers (recordLedgerInsert,
|
||||
// recordLedgerStatus) see the same connection. Guard the swap with mu.Lock
|
||||
// to prevent races with production goroutines that call GetDB() (which
|
||||
// acquires RLock) while t.Cleanup runs concurrently.
|
||||
prev := mdb.DB
|
||||
mdb.mu.Lock()
|
||||
mdb.DB = conn
|
||||
mdb.mu.Unlock()
|
||||
t.Cleanup(func() {
|
||||
mdb.mu.Lock()
|
||||
mdb.DB = prev
|
||||
mdb.mu.Unlock()
|
||||
conn.Close()
|
||||
})
|
||||
return conn
|
||||
|
||||
Reference in New Issue
Block a user