Skip to content

Commit

Permalink
feat: goner/xorm 测试用例调整
Browse files Browse the repository at this point in the history
  • Loading branch information
dapeng committed Nov 20, 2024
1 parent 4c3cb87 commit 6bb9295
Show file tree
Hide file tree
Showing 19 changed files with 108 additions and 113 deletions.
2 changes: 1 addition & 1 deletion example/app/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ require (
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/tools v0.25.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240314234333-6e1732d8331c // indirect
google.golang.org/grpc v1.63.2 // indirect
Expand Down
2 changes: 1 addition & 1 deletion example/gen-code/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ require (
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/text v0.20.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978 // indirect
Expand Down
2 changes: 1 addition & 1 deletion example/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ require (
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/text v0.20.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978 // indirect
Expand Down
1 change: 1 addition & 0 deletions example/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
2 changes: 1 addition & 1 deletion example/grpc/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ require (
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/tools v0.25.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
Expand Down
2 changes: 1 addition & 1 deletion example/http-server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ require (
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/tools v0.25.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240314234333-6e1732d8331c // indirect
google.golang.org/grpc v1.63.2 // indirect
Expand Down
2 changes: 1 addition & 1 deletion example/use-config/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ require (
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/text v0.20.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978 // indirect
Expand Down
4 changes: 2 additions & 2 deletions example/use-rdb/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.22.0
toolchain go1.22.2

require (
github.com/go-sql-driver/mysql v1.6.0
github.com/go-sql-driver/mysql v1.7.0
github.com/gone-io/gone v1.0.2
)

Expand Down Expand Up @@ -61,7 +61,7 @@ require (
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/tools v0.25.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240314234333-6e1732d8331c // indirect
google.golang.org/grpc v1.63.2 // indirect
Expand Down
2 changes: 1 addition & 1 deletion example/use-redis/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ require (
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/tools v0.25.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240314234333-6e1732d8331c // indirect
google.golang.org/grpc v1.63.2 // indirect
Expand Down
2 changes: 1 addition & 1 deletion example/use-schedule/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ require (
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/tools v0.25.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240314234333-6e1732d8331c // indirect
google.golang.org/grpc v1.63.2 // indirect
Expand Down
2 changes: 1 addition & 1 deletion example/use_default/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ require (
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/text v0.20.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978 // indirect
Expand Down
4 changes: 2 additions & 2 deletions example/web-mysql/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.22.0
toolchain go1.22.2

require (
github.com/go-sql-driver/mysql v1.6.0
github.com/go-sql-driver/mysql v1.7.0
github.com/gone-io/gone v1.0.1
github.com/sirupsen/logrus v1.9.0
)
Expand Down Expand Up @@ -61,7 +61,7 @@ require (
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/tools v0.25.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240314234333-6e1732d8331c // indirect
google.golang.org/grpc v1.63.2 // indirect
Expand Down
2 changes: 1 addition & 1 deletion example/web/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ require (
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/tools v0.25.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240314234333-6e1732d8331c // indirect
google.golang.org/grpc v1.63.2 // indirect
Expand Down
25 changes: 16 additions & 9 deletions goner/xorm/implement.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,27 @@ import (
)

func NewXormEngine() (gone.Angel, gone.GonerId, gone.GonerOption, gone.GonerOption) {
return &engine{
newFunc: newEngine,
return &wrappedEngine{
newFunc: newEngine,
newSession: newSession,
}, gone.IdGoneXorm, gone.IsDefault(new(gone.XormEngine)), gone.Order3
}

func newEngine(driverName string, dataSourceName string) (xorm.EngineInterface, error) {
return xorm.NewEngine(driverName, dataSourceName)
}

func newSession(eng xorm.EngineInterface) XInterface {
return eng.NewSession()
}

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

//go:generate mockgen -package xorm -destination=./engine_mock_test.go xorm.io/xorm EngineInterface
type engine struct {
type wrappedEngine struct {
gone.Flag
xorm.EngineInterface
gone.Logger `gone:"gone-logger"`
Expand All @@ -40,21 +45,23 @@ type engine struct {
group *xorm.EngineGroup

newFunc func(driverName string, dataSourceName string) (xorm.EngineInterface, error)

newSession func(xorm.EngineInterface) XInterface
}

func (e *engine) GetOriginEngine() xorm.EngineInterface {
func (e *wrappedEngine) GetOriginEngine() xorm.EngineInterface {
return e.EngineInterface
}

func (e *engine) Start(gone.Cemetery) error {
func (e *wrappedEngine) Start(gone.Cemetery) error {
err := e.create()
if err != nil {
return err
}
e.config()
return e.Ping()
}
func (e *engine) create() error {
func (e *wrappedEngine) create() error {
if e.EngineInterface != nil {
return gone.NewInnerError("duplicate call Start()", gone.StartError)
}
Expand Down Expand Up @@ -96,22 +103,22 @@ func (e *engine) create() error {
return nil
}

func (e *engine) config() {
func (e *wrappedEngine) config() {
e.SetConnMaxLifetime(e.maxLifetime)
e.SetMaxOpenConns(e.maxOpen)
e.SetMaxIdleConns(e.maxIdleCount)
e.SetLogger(&dbLogger{Logger: e.Logger, showSql: e.showSql})
}

func (e *engine) Stop(gone.Cemetery) error {
func (e *wrappedEngine) Stop(gone.Cemetery) error {
if e.group != nil {
return e.group.Close()
} else {
return e.EngineInterface.(*xorm.Engine).Close()
}
}

func (e *engine) Sqlx(sql string, args ...any) *xorm.Session {
func (e *wrappedEngine) Sqlx(sql string, args ...any) *xorm.Session {
sql, args = sqlDeal(sql, args...)
return e.SQL(sql, args...)
}
4 changes: 2 additions & 2 deletions goner/xorm/implement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func Test_engine(t *testing.T) {
engineInterface.EXPECT().Ping()
engineInterface.EXPECT().SQL(gomock.Any(), gomock.Any()).Return(nil)

e := engine{
e := wrappedEngine{
Logger: in.logger,
newFunc: func(driverName string, dataSourceName string) (xorm.EngineInterface, error) {
return nil, errors.New("test")
Expand All @@ -50,7 +50,7 @@ func Test_engine(t *testing.T) {
assert.NoError(t, err)

originEngine := e.GetOriginEngine()
assert.Equalf(t, engineInterface, originEngine, "origin engine is not equal")
assert.Equalf(t, engineInterface, originEngine, "origin wrappedEngine is not equal")

_ = e.Sqlx("select * from user where id = ?", 1)

Expand Down
2 changes: 1 addition & 1 deletion goner/xorm/priest.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import (
func Priest(cemetery gone.Cemetery) error {
xormEngine, id, option, g := NewXormEngine()
cemetery.BuryOnce(xormEngine, id, option, g)
cemetery.BuryOnce(NewProvider(xormEngine.(*engine)))
cemetery.BuryOnce(NewProvider(xormEngine.(*wrappedEngine)))
return nil
}
10 changes: 5 additions & 5 deletions goner/xorm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
"strings"
)

func NewProvider(engine *engine) (gone.Vampire, gone.GonerOption) {
func NewProvider(engine *wrappedEngine) (gone.Vampire, gone.GonerOption) {
return &provider{
engine: engine,
}, gone.GonerId("xorm")
}

type provider struct {
gone.Flag
engine *engine
engine *wrappedEngine
gone.Logger `gone:"*"`
}

Expand All @@ -31,7 +31,7 @@ func (e *provider) Suck(conf string, v reflect.Value) gone.SuckError {
switch v.Type() {
case xormInterface:
if conf == "master" {
v.Set(reflect.ValueOf(&engine{
v.Set(reflect.ValueOf(&wrappedEngine{
EngineInterface: e.engine.group.Master(),
}))
return nil
Expand All @@ -45,7 +45,7 @@ func (e *provider) Suck(conf string, v reflect.Value) gone.SuckError {
slaves := e.engine.group.Slaves()
if int(i) < len(slaves) {
v.Set(reflect.ValueOf(
&engine{
&wrappedEngine{
EngineInterface: slaves[i],
},
))
Expand All @@ -63,7 +63,7 @@ func (e *provider) Suck(conf string, v reflect.Value) gone.SuckError {
engines := e.engine.group.Slaves()
xormEngines := make([]gone.XormEngine, 0, len(engines))
for _, eng := range engines {
xormEngines = append(xormEngines, &engine{
xormEngines = append(xormEngines, &wrappedEngine{
EngineInterface: eng,
})
}
Expand Down
10 changes: 5 additions & 5 deletions goner/xorm/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@ type XInterface interface {
}

// ============================================================================
func (e *engine) getTransaction(id uint) (XInterface, bool) {
func (e *wrappedEngine) getTransaction(id uint) (XInterface, bool) {
session, suc := sessionMap.Load(id)
if suc {
return session.(XInterface), false
} else {
s := e.NewSession()
s := e.newSession(e)
sessionMap.Store(id, s)
return s, true
}
}

func (e *engine) delTransaction(id uint, session XInterface) error {
func (e *wrappedEngine) delTransaction(id uint, session XInterface) error {
defer sessionMap.Delete(id)
return session.Close()
}

// Transaction 事物处理 不允许在事物中新开协程,否则事物会失效
func (e *engine) Transaction(fn func(session Interface) error) error {
func (e *wrappedEngine) Transaction(fn func(session Interface) error) error {
var err error
gls.EnsureGoroutineId(func(gid uint) {
session, isNew := e.getTransaction(gid)
Expand All @@ -53,7 +53,7 @@ func (e *engine) Transaction(fn func(session Interface) error) error {
}

isRollback := false
defer func(e *engine, id uint, session XInterface) {
defer func(e *wrappedEngine, id uint, session XInterface) {
err := e.delTransaction(id, session)
if err != nil {
e.Errorf("del session err:%v", err)
Expand Down
Loading

0 comments on commit 6bb9295

Please sign in to comment.