Skip to content

Commit

Permalink
fix mongo unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
gorexlv committed Sep 7, 2020
1 parent 01d5499 commit 37b5b6a
Show file tree
Hide file tree
Showing 10 changed files with 400 additions and 100 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,7 @@ coverage.html
# customize ignore files
configCacheDir

example/*/*/config-local.toml
example/*/*/config-local.toml
dump.rdb
default.etcd
pkg/datasource/apollo/.SampleApp_default
2 changes: 1 addition & 1 deletion example/all/internal/app/demo/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (eng *Engine) initSentinel() error {
MetricType: flow.QPS,
Count: 1,
})
return config.InitSentinelCoreComponent()
return config.Build()
}

func (eng *Engine) startJobs() error {
Expand Down
8 changes: 2 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.14
require (
github.com/BurntSushi/toml v0.3.1
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/alibaba/sentinel-golang v0.4.0
github.com/alibaba/sentinel-golang v0.6.0
github.com/apache/rocketmq-client-go/v2 v2.0.0
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0
Expand All @@ -21,16 +21,13 @@ require (
github.com/go-redis/redis v6.15.8+incompatible
github.com/go-resty/resty/v2 v2.2.0
github.com/gogf/gf v1.13.3
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.4.2
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect
github.com/gorilla/websocket v1.4.2
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/grpc-ecosystem/grpc-gateway v1.9.6 // indirect
github.com/jinzhu/gorm v1.9.12
github.com/json-iterator/go v1.1.10
github.com/kr/pretty v0.2.0 // indirect
github.com/labstack/echo/v4 v4.1.16
github.com/mitchellh/mapstructure v1.3.2
github.com/modern-go/reflect2 v1.0.1
Expand Down Expand Up @@ -60,8 +57,7 @@ require (
golang.org/x/sys v0.0.0-20200805065543-0cf7623e9dbd // indirect
golang.org/x/text v0.3.3 // indirect
golang.org/x/tools v0.0.0-20200728235236-e8769ccb4337 // indirect
google.golang.org/genproto v0.0.0-20191108220845-16a3f7862a1a
google.golang.org/genproto v0.0.0-20191216164720-4f79533eabd1
google.golang.org/grpc v1.26.0
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce
sigs.k8s.io/yaml v1.1.0 // indirect
)
383 changes: 368 additions & 15 deletions go.sum

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions jupiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,7 @@ func (app *Application) initSentinel() error {
// init reliability component sentinel
if conf.Get("jupiter.reliability.sentinel") != nil {
app.logger.Info("init sentinel")
return sentinel.RawConfig("jupiter.reliability.sentinel").
Build()
return sentinel.RawConfig("jupiter.reliability.sentinel").Build()
}
return nil
}
Expand Down
9 changes: 0 additions & 9 deletions pkg/store/mongo/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,3 @@ func init() {
}

var _logger = xlog.JupiterLogger.With(xlog.FieldMod("mongodb"))

// Invoker ...
func Invoker(name string) *mgo.Session {
if client := Get(name); client != nil {
return client
}

return StdNew(name)
}
18 changes: 4 additions & 14 deletions pkg/store/mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,28 @@ import (
"github.com/globalsign/mgo"
)

// StdNew ...
func StdNew(name string, opts ...interface{}) *mgo.Session {
return New(name, StdConfig(name))
}

/*
DB: 返回name定义的mysql DB handler
name: 唯一名称
opts: Open Option, 用于覆盖配置文件中定义的配置
example: DB := DB("StdConfig", orm.RawConfig("jupiter.mongodb.StdConfig"))
*/
func New(name string, config Config) *mgo.Session {
if _, ok := _instances.Load(name); ok {
_logger.Panic("duplicated new", xlog.FieldName(name), xlog.FieldExtMessage(config))
}

func newSession(config Config) *mgo.Session {
session, err := mgo.Dial(config.DSN)
if err != nil {
_logger.Panic("dial mongo", xlog.FieldName(name), xlog.FieldAddr(config.DSN), xlog.Any("error", err))
_logger.Panic("dial mongo", xlog.FieldAddr(config.DSN), xlog.Any("error", err))
}

if config.SocketTimeout == time.Duration(0) {
_logger.Panic("invalid config", xlog.FieldName(name), xlog.FieldExtMessage("socketTimeout"))
_logger.Panic("invalid config", xlog.FieldExtMessage("socketTimeout"))
}

if config.PoolLimit == 0 {
_logger.Panic("invalid config", xlog.FieldName(name), xlog.FieldExtMessage("poolLimit"))
_logger.Panic("invalid config", xlog.FieldExtMessage("poolLimit"))
}

session.SetSocketTimeout(config.SocketTimeout)
session.SetPoolLimit(config.PoolLimit)

_instances.Store(name, session)
return session
}
50 changes: 0 additions & 50 deletions pkg/store/mongo/mongo_test.go

This file was deleted.

7 changes: 6 additions & 1 deletion pkg/store/mongo/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/douyu/jupiter/pkg/conf"
"github.com/douyu/jupiter/pkg/util/xtime"
"github.com/globalsign/mgo"
)

// Config ...
Expand All @@ -27,7 +28,7 @@ func StdConfig(name string) Config {
// RawConfig 裸配置
// example: minerva.mongodb.demo
func RawConfig(key string) Config {
var config Config
var config = DefaultConfig()
if err := conf.UnmarshalKey(key, &config, conf.TagName("toml")); err != nil {
panic(err)
}
Expand All @@ -41,3 +42,7 @@ func DefaultConfig() Config {
PoolLimit: 100,
}
}

func (config Config) Build() *mgo.Session {
return newSession(config)
}
15 changes: 14 additions & 1 deletion pkg/store/mongo/option_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
package mongo

import (
"bytes"
"reflect"
"testing"
"time"

"github.com/BurntSushi/toml"
"github.com/douyu/jupiter/pkg/conf"
)

func TestMain(m *testing.M) {
conf.LoadFromReader(bytes.NewBufferString(`
[jupiter.mongo.demo]
dsn = ""
socketTimeout = "5s"
poolLimit = 100
`), toml.Unmarshal)
}

func TestStdConfig(t *testing.T) {
type args struct {
name string
Expand Down Expand Up @@ -50,7 +63,7 @@ func TestRawConfig(t *testing.T) {
{
name: "raw config",
args: args{
key: "minerva.mongo.demo",
key: "jupiter.mongo.demo",
},
want: Config{
DSN: "",
Expand Down

0 comments on commit 37b5b6a

Please sign in to comment.