Fixed tests for Easer to use gorm.open in order to inject the required configurations
This commit is contained in:
parent
a688acb023
commit
76bc83ea2b
12
gorm.go
12
gorm.go
@ -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
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user