Skip to content

Commit 37b5b6a

Browse files
committed
fix mongo unit testing
1 parent 01d5499 commit 37b5b6a

File tree

10 files changed

+400
-100
lines changed

10 files changed

+400
-100
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,7 @@ coverage.html
6868
# customize ignore files
6969
configCacheDir
7070

71-
example/*/*/config-local.toml
71+
example/*/*/config-local.toml
72+
dump.rdb
73+
default.etcd
74+
pkg/datasource/apollo/.SampleApp_default

example/all/internal/app/demo/engine.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (eng *Engine) initSentinel() error {
6161
MetricType: flow.QPS,
6262
Count: 1,
6363
})
64-
return config.InitSentinelCoreComponent()
64+
return config.Build()
6565
}
6666

6767
func (eng *Engine) startJobs() error {

go.mod

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.14
55
require (
66
github.com/BurntSushi/toml v0.3.1
77
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
8-
github.com/alibaba/sentinel-golang v0.4.0
8+
github.com/alibaba/sentinel-golang v0.6.0
99
github.com/apache/rocketmq-client-go/v2 v2.0.0
1010
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect
1111
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0
@@ -21,16 +21,13 @@ require (
2121
github.com/go-redis/redis v6.15.8+incompatible
2222
github.com/go-resty/resty/v2 v2.2.0
2323
github.com/gogf/gf v1.13.3
24-
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
2524
github.com/golang/protobuf v1.4.2
2625
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect
2726
github.com/gorilla/websocket v1.4.2
28-
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4 // indirect
2927
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
3028
github.com/grpc-ecosystem/grpc-gateway v1.9.6 // indirect
3129
github.com/jinzhu/gorm v1.9.12
3230
github.com/json-iterator/go v1.1.10
33-
github.com/kr/pretty v0.2.0 // indirect
3431
github.com/labstack/echo/v4 v4.1.16
3532
github.com/mitchellh/mapstructure v1.3.2
3633
github.com/modern-go/reflect2 v1.0.1
@@ -60,8 +57,7 @@ require (
6057
golang.org/x/sys v0.0.0-20200805065543-0cf7623e9dbd // indirect
6158
golang.org/x/text v0.3.3 // indirect
6259
golang.org/x/tools v0.0.0-20200728235236-e8769ccb4337 // indirect
63-
google.golang.org/genproto v0.0.0-20191108220845-16a3f7862a1a
60+
google.golang.org/genproto v0.0.0-20191216164720-4f79533eabd1
6461
google.golang.org/grpc v1.26.0
6562
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce
66-
sigs.k8s.io/yaml v1.1.0 // indirect
6763
)

go.sum

Lines changed: 368 additions & 15 deletions
Large diffs are not rendered by default.

jupiter.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,7 @@ func (app *Application) initSentinel() error {
498498
// init reliability component sentinel
499499
if conf.Get("jupiter.reliability.sentinel") != nil {
500500
app.logger.Info("init sentinel")
501-
return sentinel.RawConfig("jupiter.reliability.sentinel").
502-
Build()
501+
return sentinel.RawConfig("jupiter.reliability.sentinel").Build()
503502
}
504503
return nil
505504
}

pkg/store/mongo/init.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,3 @@ func init() {
3434
}
3535

3636
var _logger = xlog.JupiterLogger.With(xlog.FieldMod("mongodb"))
37-
38-
// Invoker ...
39-
func Invoker(name string) *mgo.Session {
40-
if client := Get(name); client != nil {
41-
return client
42-
}
43-
44-
return StdNew(name)
45-
}

pkg/store/mongo/mongo.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,28 @@ import (
77
"github.com/globalsign/mgo"
88
)
99

10-
// StdNew ...
11-
func StdNew(name string, opts ...interface{}) *mgo.Session {
12-
return New(name, StdConfig(name))
13-
}
14-
1510
/*
1611
DB: 返回name定义的mysql DB handler
1712
name: 唯一名称
1813
opts: Open Option, 用于覆盖配置文件中定义的配置
1914
example: DB := DB("StdConfig", orm.RawConfig("jupiter.mongodb.StdConfig"))
2015
*/
21-
func New(name string, config Config) *mgo.Session {
22-
if _, ok := _instances.Load(name); ok {
23-
_logger.Panic("duplicated new", xlog.FieldName(name), xlog.FieldExtMessage(config))
24-
}
25-
16+
func newSession(config Config) *mgo.Session {
2617
session, err := mgo.Dial(config.DSN)
2718
if err != nil {
28-
_logger.Panic("dial mongo", xlog.FieldName(name), xlog.FieldAddr(config.DSN), xlog.Any("error", err))
19+
_logger.Panic("dial mongo", xlog.FieldAddr(config.DSN), xlog.Any("error", err))
2920
}
3021

3122
if config.SocketTimeout == time.Duration(0) {
32-
_logger.Panic("invalid config", xlog.FieldName(name), xlog.FieldExtMessage("socketTimeout"))
23+
_logger.Panic("invalid config", xlog.FieldExtMessage("socketTimeout"))
3324
}
3425

3526
if config.PoolLimit == 0 {
36-
_logger.Panic("invalid config", xlog.FieldName(name), xlog.FieldExtMessage("poolLimit"))
27+
_logger.Panic("invalid config", xlog.FieldExtMessage("poolLimit"))
3728
}
3829

3930
session.SetSocketTimeout(config.SocketTimeout)
4031
session.SetPoolLimit(config.PoolLimit)
4132

42-
_instances.Store(name, session)
4333
return session
4434
}

pkg/store/mongo/mongo_test.go

Lines changed: 0 additions & 50 deletions
This file was deleted.

pkg/store/mongo/option.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/douyu/jupiter/pkg/conf"
77
"github.com/douyu/jupiter/pkg/util/xtime"
8+
"github.com/globalsign/mgo"
89
)
910

1011
// Config ...
@@ -27,7 +28,7 @@ func StdConfig(name string) Config {
2728
// RawConfig 裸配置
2829
// example: minerva.mongodb.demo
2930
func RawConfig(key string) Config {
30-
var config Config
31+
var config = DefaultConfig()
3132
if err := conf.UnmarshalKey(key, &config, conf.TagName("toml")); err != nil {
3233
panic(err)
3334
}
@@ -41,3 +42,7 @@ func DefaultConfig() Config {
4142
PoolLimit: 100,
4243
}
4344
}
45+
46+
func (config Config) Build() *mgo.Session {
47+
return newSession(config)
48+
}

pkg/store/mongo/option_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
package mongo
22

33
import (
4+
"bytes"
45
"reflect"
56
"testing"
67
"time"
8+
9+
"github.com/BurntSushi/toml"
10+
"github.com/douyu/jupiter/pkg/conf"
711
)
812

13+
func TestMain(m *testing.M) {
14+
conf.LoadFromReader(bytes.NewBufferString(`
15+
[jupiter.mongo.demo]
16+
dsn = ""
17+
socketTimeout = "5s"
18+
poolLimit = 100
19+
`), toml.Unmarshal)
20+
}
21+
922
func TestStdConfig(t *testing.T) {
1023
type args struct {
1124
name string
@@ -50,7 +63,7 @@ func TestRawConfig(t *testing.T) {
5063
{
5164
name: "raw config",
5265
args: args{
53-
key: "minerva.mongo.demo",
66+
key: "jupiter.mongo.demo",
5467
},
5568
want: Config{
5669
DSN: "",

0 commit comments

Comments
 (0)