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

12
gorm.go
View File

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

View File

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