refac: hardening pass with multiple overhauls
This commit is contained in:
1 parent
b0f7b7892b
commit
da36c78bc0
28 files changed
+1375
-39
No files matched your search
@@ -0,0 +1,49 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"RenewCMS/api"
|
||||
"RenewCMS/internal/infrastructure/persistence/models"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
func TestProductionSQLiteDSNWaitsForTheWriteLock(t *testing.T) {
|
||||
t.Run("A second writer waits for the first instead of failing with a locked database", func(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "busy.db")
|
||||
open := func() *gorm.DB {
|
||||
db, err := gorm.Open(sqlite.Open(api.SQLiteDSN(path)), &gorm.Config{Logger: logger.Discard})
|
||||
if err != nil {
|
||||
t.Fatalf("open: %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if sqlDB, err := db.DB(); err == nil {
|
||||
_ = sqlDB.Close()
|
||||
}
|
||||
})
|
||||
return db
|
||||
}
|
||||
first, second := open(), open()
|
||||
if err := first.AutoMigrate(&models.User{}); err != nil {
|
||||
t.Fatalf("migrate: %v", err)
|
||||
}
|
||||
tx := first.Begin()
|
||||
if err := tx.Create(&models.User{Username: "a", Password: "x", Email: "a@x.example"}).Error; err != nil {
|
||||
t.Fatalf("first write: %v", err)
|
||||
}
|
||||
go func() {
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
tx.Commit()
|
||||
}()
|
||||
|
||||
err := second.Create(&models.User{Username: "b", Password: "x", Email: "b@x.example"}).Error
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("second writer must wait for the lock, got %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user