Skip to content

Commit

Permalink
feat: Adapt 'goner/xorm' to 'goner/viper' and test
Browse files Browse the repository at this point in the history
  • Loading branch information
dapeng committed Nov 22, 2024
1 parent d3cbdde commit 02a590f
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 63 deletions.
File renamed without changes.
29 changes: 14 additions & 15 deletions goner/xorm/implement.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,17 @@ func newSession(eng xorm.EngineInterface) XInterface {
}

type ClusterNodeConf struct {
DriverName string `properties:"driver-name,default=driver" mapstructure:"driver-name"`
DSN string `properties:"dsn,default=dsn" mapstructure:"dsn"`
DriverName string `properties:"driver-name,default=" mapstructure:"driver-name"`
DSN string `properties:"dsn,default=" mapstructure:"dsn,default=yyyy"`
}

type Conf struct {
DriverName string `properties:"driver-name,default=driver" mapstructure:"driver-name"`
Dsn string `properties:"dsn,default=dsn" mapstructure:"dsn"`
MaxIdleCount int `properties:"max-idle-count,default=5" mapstructure:"max-idle-count"`
MaxOpen int `properties:"max-open,default=20" mapstructure:"max-open"`
MaxLifetime time.Duration `properties:"max-lifetime,default=10m" mapstructure:"max-lifetime"`
ShowSql bool `properties:"show-sql,default=true" mapstructure:"show-sql"`
EnableCluster bool `properties:"cluster.enable,default=false" mapstructure:"cluster.enable"`
DriverName string `properties:"driver-name,default=" mapstructure:"driver-name"`
Dsn string `properties:"dsn,default=" mapstructure:"dsn"`
MaxIdleCount int `properties:"max-idle-count,default=5" mapstructure:"max-idle-count,default=5"`
MaxOpen int `properties:"max-open,default=20" mapstructure:"max-open,default=20"`
MaxLifetime time.Duration `properties:"max-lifetime,default=10m" mapstructure:"max-lifetime,default=10m"`
ShowSql bool `properties:"show-sql,default=true" mapstructure:"show-sql,default=true"`
}

//go:generate mockgen -package xorm -destination=./engine_mock_test.go xorm.io/xorm EngineInterface
Expand All @@ -50,11 +49,11 @@ type wrappedEngine struct {
newFunc func(driverName string, dataSourceName string) (xorm.EngineInterface, error)
newSession func(xorm.EngineInterface) XInterface

log gone.Logger `gone:"gone-logger"`
conf Conf `gone:"config,database"`

masterConf *ClusterNodeConf `gone:"config,database.cluster.master"`
slavesConf []*ClusterNodeConf `gone:"config,database.cluster.slaves"`
log gone.Logger `gone:"gone-logger"`
conf Conf `gone:"config,database"`
enableCluster bool `gone:"config,database.cluster.enable,default=false"`
masterConf *ClusterNodeConf `gone:"config,database.cluster.master"`
slavesConf []*ClusterNodeConf `gone:"config,database.cluster.slaves"`

policy xorm.GroupPolicy
unitTest bool
Expand Down Expand Up @@ -87,7 +86,7 @@ func (e *wrappedEngine) create() error {
return gone.NewInnerError("duplicate call Start()", gone.StartError)
}

if e.conf.EnableCluster {
if e.enableCluster {
if e.masterConf == nil {
return gone.NewInnerError("master config(database.cluster.master) is nil", gone.StartError)
}
Expand Down
55 changes: 10 additions & 45 deletions goner/xorm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ func (p *provider) Suck(conf string, v reflect.Value) gone.SuckError {
return gone.NewInnerError("failed to get config for cluster: "+clusterName, gone.InjectError)
}

var enableCluster bool
err = p.configure.Get(clusterName+".cluster.enable", &enableCluster, "false")
if err != nil {
return gone.NewInnerError("failed to get cluster enable config for cluster: "+clusterName, gone.InjectError)
}

var masterConf ClusterNodeConf
err = p.configure.Get(clusterName+".cluster.master", &masterConf, "")
if err != nil {
Expand All @@ -85,6 +91,7 @@ func (p *provider) Suck(conf string, v reflect.Value) gone.SuckError {

db = newWrappedEngine()
db.conf = config
db.enableCluster = enableCluster
db.masterConf = &masterConf
db.slavesConf = slavesConf

Expand All @@ -107,7 +114,7 @@ func (p *provider) Suck(conf string, v reflect.Value) gone.SuckError {
}

if v.Type() == xormInterfaceSlice {
if !db.conf.EnableCluster {
if !db.enableCluster {
return gone.NewInnerError(fmt.Sprintf("database(name=%s) is not enable cluster, cannot inject []gone.XormEngine", clusterName), gone.InjectError)
}

Expand All @@ -124,7 +131,7 @@ func (p *provider) Suck(conf string, v reflect.Value) gone.SuckError {

if v.Type() == xormInterface {
if _, ok := m["master"]; ok {
if !db.conf.EnableCluster {
if !db.enableCluster {
return gone.NewInnerError(fmt.Sprintf("database(name=%s) is not enable cluster, cannot inject master into gone.XormEngine", clusterName), gone.InjectError)
}

Expand All @@ -135,7 +142,7 @@ func (p *provider) Suck(conf string, v reflect.Value) gone.SuckError {
}

if slaveIndex, ok := m["slave"]; ok {
if !db.conf.EnableCluster {
if !db.enableCluster {
return gone.NewInnerError(fmt.Sprintf("database(name=%s) is not enable cluster, cannot inject slave into gone.XormEngine", clusterName), gone.InjectError)
}

Expand All @@ -159,45 +166,3 @@ func (p *provider) Suck(conf string, v reflect.Value) gone.SuckError {
}
return gone.CannotFoundGonerByTypeError(v.Type())
}

//database.cluster.enable=true
//database.cluster.master.driver-name=mysql
//database.cluster.master.dsn=${db.username}:${db.password}@tcp(${db.host}:${db.port})/${db.name}?charset=utf8mb4&loc=Local
//
//database.cluster.slaves[0].driver-name=mysql
//database.cluster.slaves[0].dsn=${db.username}:${db.password}@tcp(${db.host}:${db.port})/${db.name}?charset=utf8mb4&loc=Local
//
//database.cluster.slaves[1].driver-name=mysql
//database.cluster.slaves[1].dsn=${db.username}:${db.password}@tcp(${db.host}:${db.port})/${db.name}?charset=utf8mb4&loc=Local
//
//database.cluster.slaves[2].driver-name=mysql
//database.cluster.slaves[2].dsn=${db.username}:${db.password}@tcp(${db.host}:${db.port})/${db.name}?charset=utf8mb4&loc=Local

//func Test_iCommission_Tmp(t *testing.T) {
// gone.RunTest(func(e struct {
// group gone.XormEngine `gone:"*"`
// master gone.XormEngine `gone:"xorm,master"`
// slave0 gone.XormEngine `gone:"xorm,slave0"`
// slave1 gone.XormEngine `gone:"xorm,slave1"`
// slave2 gone.XormEngine `gone:"xorm,slave2"`
// slaves []gone.XormEngine `gone:"xorm,xxx"`
// }) {
// assert.Equal(t, 3, len(e.slaves))
// assert.Equal(t, e.slaves[0], e.slave0)
// assert.Equal(t, e.slaves[1], e.slave1)
// assert.Equal(t, e.slaves[2], e.slave2)
// assert.NotNil(t, e.master)
//
// err := e.master.Ping()
// assert.Nil(t, err)
// err = e.slave0.Ping()
// assert.Nil(t, err)
// err = e.slave1.Ping()
// assert.Nil(t, err)
// err = e.slave2.Ping()
// assert.Nil(t, err)
//
// err = e.group.Ping()
// assert.Nil(t, err)
// }, goner.XormPriest)
//}
90 changes: 87 additions & 3 deletions goner/xorm/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/gone-io/gone"
"github.com/gone-io/gone/goner/config"
gone_viper "github.com/gone-io/gone/goner/viper"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
"reflect"
Expand Down Expand Up @@ -111,9 +112,8 @@ func Test_provider_Suck(t *testing.T) {
session := NewMockXInterface(controller)
return session
},
conf: Conf{
EnableCluster: true,
},
conf: Conf{},
enableCluster: true,
masterConf: &ClusterNodeConf{
DriverName: "mysql",
DSN: "db0",
Expand Down Expand Up @@ -144,3 +144,87 @@ func Test_provider_Suck(t *testing.T) {
return nil
})
}

func Test_provider_Suck_ReadConfByViper(t *testing.T) {
controller := gomock.NewController(t)
engineInterface := NewMockEngineInterface(controller)
//var defaultDb gone.XormEngine

enginesMap := make(map[string]*xorm.Engine)
for i := 0; i < 10; i++ {
enginesMap[fmt.Sprintf("db%d", i)] = &xorm.Engine{}
}

gone.RunTest(func(i struct {
p *provider `gone:"*"`
}) {
t.Run("get user.database gone.XormEngine", func(t *testing.T) {
var X gone.XormEngine
err := i.p.Suck("db=user.database", reflect.ValueOf(&X).Elem())
assert.Nil(t, err)

err = i.p.Suck("db=user.database,master", reflect.ValueOf(&X).Elem())
assert.Nil(t, err)
assert.Equal(t, X.(*wrappedEngine).EngineInterface, enginesMap["db3"])
})

t.Run("get user.database slave gone.XormEngine", func(t *testing.T) {
var X gone.XormEngine
err := i.p.Suck("db=user.database,slave=1", reflect.ValueOf(&X).Elem())
assert.Nil(t, err)
assert.Equal(t, X.(*wrappedEngine).EngineInterface, enginesMap["db5"])

err = i.p.Suck("db=user.database,slave=2", reflect.ValueOf(&X).Elem())
assert.Error(t, err)
})

t.Run("get user.database slave with []gone.XormEngine", func(t *testing.T) {
var X []gone.XormEngine
err := i.p.Suck("db=user.database", reflect.ValueOf(&X).Elem())
assert.Nil(t, err)
assert.Equal(t, len(X), 2)
assert.Equal(t, X[0].(*wrappedEngine).EngineInterface, enginesMap["db4"])
assert.Equal(t, X[1].(*wrappedEngine).EngineInterface, enginesMap["db5"])
})
}, func(cemetery gone.Cemetery) error {
newFunc := func(driverName string, dataSourceName string) (xorm.EngineInterface, error) {
return enginesMap[dataSourceName], nil
}

e := wrappedEngine{
newFunc: newFunc,
newSession: func(engineInterface xorm.EngineInterface) XInterface {
session := NewMockXInterface(controller)
return session
},
conf: Conf{},
enableCluster: true,
masterConf: &ClusterNodeConf{
DriverName: "mysql",
DSN: "db0",
},
slavesConf: []*ClusterNodeConf{
{
DriverName: "mysql",
DSN: "db1",
},
{
DriverName: "mysql",
DSN: "db2",
},
},
}

err := e.create()
if err != nil {
return err
}

e.EngineInterface = engineInterface
e.unitTest = true
//defaultDb = &e

cemetery.Bury(NewProvider(&e))
return gone_viper.Priest(cemetery)
})
}
11 changes: 11 additions & 0 deletions goner/xorm/testdata/config/default_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
user.database:
cluster:
enable: true
master:
driver-name: mysql
dsn: db3
slaves:
- driver-name: mysql
dsn: db4
- driver-name: postgresql
dsn: db5

0 comments on commit 02a590f

Please sign in to comment.