Fixed tests for Easer to use gorm.open in order to inject the required configurations

This commit is contained in:
ktsivkov 2023-03-20 16:16:34 +01:00
parent a688acb023
commit 76bc83ea2b
2 changed files with 13 additions and 10 deletions

10
gorm.go
View File

@ -58,10 +58,10 @@ type Config struct {
Plugins map[string]Plugin Plugins map[string]Plugin
// Ease database load by grouping identical queries // Ease database load by grouping identical queries
Ease bool Ease bool
EaseQueue *sync.Map
callbacks *callbacks callbacks *callbacks
cacheStore *sync.Map cacheStore *sync.Map
easeQueue *sync.Map
} }
// Apply update config to new config // Apply update config to new config
@ -170,8 +170,8 @@ func Open(dialector Dialector, opts ...Option) (db *DB, err error) {
config.cacheStore = &sync.Map{} config.cacheStore = &sync.Map{}
} }
if config.Ease && config.EaseQueue == nil { if config.Ease && config.easeQueue == nil {
config.EaseQueue = &sync.Map{} config.easeQueue = &sync.Map{}
} }
db = &DB{Config: config, clone: 1} db = &DB{Config: config, clone: 1}
@ -501,13 +501,13 @@ func (db *DB) Ease(cb func(*DB)) *DB {
} }
eq.wg.Add(1) eq.wg.Add(1)
var runner, ok = db.EaseQueue.LoadOrStore(hash, eq) var runner, ok = db.easeQueue.LoadOrStore(hash, eq)
et := runner.(*easedTask) et := runner.(*easedTask)
if !ok { if !ok {
cb(db) cb(db)
et.wg.Done() et.wg.Done()
db.EaseQueue.Delete(hash) db.easeQueue.Delete(hash)
return db return db
} }

View File

@ -2,6 +2,7 @@ package tests_test
import ( import (
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/utils/tests"
"sync" "sync"
"testing" "testing"
"time" "time"
@ -9,8 +10,9 @@ import (
func TestEaser(t *testing.T) { func TestEaser(t *testing.T) {
t.Run("once", func(t *testing.T) { t.Run("once", func(t *testing.T) {
db1 := DB.Unscoped() db1, _ := gorm.Open(tests.DummyDialector{}, &gorm.Config{
db1.Config.EaseQueue = &sync.Map{} Ease: true,
})
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
wg.Add(2) wg.Add(2)
@ -40,8 +42,9 @@ func TestEaser(t *testing.T) {
} }
}) })
t.Run("twice", func(t *testing.T) { t.Run("twice", func(t *testing.T) {
db1 := DB.Unscoped() db1, _ := gorm.Open(tests.DummyDialector{}, &gorm.Config{
db1.Config.EaseQueue = &sync.Map{} Ease: true,
})
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
wg.Add(2) wg.Add(2)