123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- package mongo
- import (
- "errors"
- "testing"
- "github.com/globalsign/mgo"
- "github.com/golang/mock/gomock"
- "github.com/stretchr/testify/assert"
- "github.com/tal-tech/go-zero/core/breaker"
- "github.com/tal-tech/go-zero/core/logx"
- "github.com/tal-tech/go-zero/core/stores/mongo/internal"
- "github.com/tal-tech/go-zero/core/stringx"
- )
- var errDummy = errors.New("dummy")
- func init() {
- logx.Disable()
- }
- func TestKeepPromise_accept(t *testing.T) {
- p := new(mockPromise)
- kp := keepablePromise{
- promise: p,
- log: func(error) {},
- }
- assert.Nil(t, kp.accept(nil))
- assert.Equal(t, mgo.ErrNotFound, kp.accept(mgo.ErrNotFound))
- }
- func TestKeepPromise_keep(t *testing.T) {
- tests := []struct {
- err error
- accepted bool
- reason string
- }{
- {
- err: nil,
- accepted: true,
- reason: "",
- },
- {
- err: mgo.ErrNotFound,
- accepted: true,
- reason: "",
- },
- {
- err: errors.New("any"),
- accepted: false,
- reason: "any",
- },
- }
- for _, test := range tests {
- t.Run(stringx.RandId(), func(t *testing.T) {
- p := new(mockPromise)
- kp := keepablePromise{
- promise: p,
- log: func(error) {},
- }
- assert.Equal(t, test.err, kp.keep(test.err))
- assert.Equal(t, test.accepted, p.accepted)
- assert.Equal(t, test.reason, p.reason)
- })
- }
- }
- func TestNewCollection(t *testing.T) {
- col := newCollection(&mgo.Collection{
- Database: nil,
- Name: "foo",
- FullName: "bar",
- })
- assert.Equal(t, "bar", col.(*decoratedCollection).name)
- }
- func TestCollectionFind(t *testing.T) {
- ctrl := gomock.NewController(t)
- defer ctrl.Finish()
- var query mgo.Query
- col := internal.NewMockMgoCollection(ctrl)
- col.EXPECT().Find(gomock.Any()).Return(&query)
- c := decoratedCollection{
- collection: col,
- brk: breaker.NewBreaker(),
- }
- actual := c.Find(nil)
- switch v := actual.(type) {
- case promisedQuery:
- assert.Equal(t, &query, v.Query)
- assert.Equal(t, errDummy, v.promise.keep(errDummy))
- default:
- t.Fail()
- }
- c.brk = new(dropBreaker)
- actual = c.Find(nil)
- assert.Equal(t, rejectedQuery{}, actual)
- }
- func TestCollectionFindId(t *testing.T) {
- ctrl := gomock.NewController(t)
- defer ctrl.Finish()
- var query mgo.Query
- col := internal.NewMockMgoCollection(ctrl)
- col.EXPECT().FindId(gomock.Any()).Return(&query)
- c := decoratedCollection{
- collection: col,
- brk: breaker.NewBreaker(),
- }
- actual := c.FindId(nil)
- switch v := actual.(type) {
- case promisedQuery:
- assert.Equal(t, &query, v.Query)
- assert.Equal(t, errDummy, v.promise.keep(errDummy))
- default:
- t.Fail()
- }
- c.brk = new(dropBreaker)
- actual = c.FindId(nil)
- assert.Equal(t, rejectedQuery{}, actual)
- }
- func TestCollectionInsert(t *testing.T) {
- ctrl := gomock.NewController(t)
- defer ctrl.Finish()
- col := internal.NewMockMgoCollection(ctrl)
- col.EXPECT().Insert(nil, nil).Return(errDummy)
- c := decoratedCollection{
- collection: col,
- brk: breaker.NewBreaker(),
- }
- err := c.Insert(nil, nil)
- assert.Equal(t, errDummy, err)
- c.brk = new(dropBreaker)
- err = c.Insert(nil, nil)
- assert.Equal(t, errDummy, err)
- }
- func TestCollectionPipe(t *testing.T) {
- ctrl := gomock.NewController(t)
- defer ctrl.Finish()
- var pipe mgo.Pipe
- col := internal.NewMockMgoCollection(ctrl)
- col.EXPECT().Pipe(gomock.Any()).Return(&pipe)
- c := decoratedCollection{
- collection: col,
- brk: breaker.NewBreaker(),
- }
- actual := c.Pipe(nil)
- switch v := actual.(type) {
- case promisedPipe:
- assert.Equal(t, &pipe, v.Pipe)
- assert.Equal(t, errDummy, v.promise.keep(errDummy))
- default:
- t.Fail()
- }
- c.brk = new(dropBreaker)
- actual = c.Pipe(nil)
- assert.Equal(t, rejectedPipe{}, actual)
- }
- func TestCollectionRemove(t *testing.T) {
- ctrl := gomock.NewController(t)
- defer ctrl.Finish()
- col := internal.NewMockMgoCollection(ctrl)
- col.EXPECT().Remove(gomock.Any()).Return(errDummy)
- c := decoratedCollection{
- collection: col,
- brk: breaker.NewBreaker(),
- }
- err := c.Remove(nil)
- assert.Equal(t, errDummy, err)
- c.brk = new(dropBreaker)
- err = c.Remove(nil)
- assert.Equal(t, errDummy, err)
- }
- func TestCollectionRemoveAll(t *testing.T) {
- ctrl := gomock.NewController(t)
- defer ctrl.Finish()
- col := internal.NewMockMgoCollection(ctrl)
- col.EXPECT().RemoveAll(gomock.Any()).Return(nil, errDummy)
- c := decoratedCollection{
- collection: col,
- brk: breaker.NewBreaker(),
- }
- _, err := c.RemoveAll(nil)
- assert.Equal(t, errDummy, err)
- c.brk = new(dropBreaker)
- _, err = c.RemoveAll(nil)
- assert.Equal(t, errDummy, err)
- }
- func TestCollectionRemoveId(t *testing.T) {
- ctrl := gomock.NewController(t)
- defer ctrl.Finish()
- col := internal.NewMockMgoCollection(ctrl)
- col.EXPECT().RemoveId(gomock.Any()).Return(errDummy)
- c := decoratedCollection{
- collection: col,
- brk: breaker.NewBreaker(),
- }
- err := c.RemoveId(nil)
- assert.Equal(t, errDummy, err)
- c.brk = new(dropBreaker)
- err = c.RemoveId(nil)
- assert.Equal(t, errDummy, err)
- }
- func TestCollectionUpdate(t *testing.T) {
- ctrl := gomock.NewController(t)
- defer ctrl.Finish()
- col := internal.NewMockMgoCollection(ctrl)
- col.EXPECT().Update(gomock.Any(), gomock.Any()).Return(errDummy)
- c := decoratedCollection{
- collection: col,
- brk: breaker.NewBreaker(),
- }
- err := c.Update(nil, nil)
- assert.Equal(t, errDummy, err)
- c.brk = new(dropBreaker)
- err = c.Update(nil, nil)
- assert.Equal(t, errDummy, err)
- }
- func TestCollectionUpdateId(t *testing.T) {
- ctrl := gomock.NewController(t)
- defer ctrl.Finish()
- col := internal.NewMockMgoCollection(ctrl)
- col.EXPECT().UpdateId(gomock.Any(), gomock.Any()).Return(errDummy)
- c := decoratedCollection{
- collection: col,
- brk: breaker.NewBreaker(),
- }
- err := c.UpdateId(nil, nil)
- assert.Equal(t, errDummy, err)
- c.brk = new(dropBreaker)
- err = c.UpdateId(nil, nil)
- assert.Equal(t, errDummy, err)
- }
- func TestCollectionUpsert(t *testing.T) {
- ctrl := gomock.NewController(t)
- defer ctrl.Finish()
- col := internal.NewMockMgoCollection(ctrl)
- col.EXPECT().Upsert(gomock.Any(), gomock.Any()).Return(nil, errDummy)
- c := decoratedCollection{
- collection: col,
- brk: breaker.NewBreaker(),
- }
- _, err := c.Upsert(nil, nil)
- assert.Equal(t, errDummy, err)
- c.brk = new(dropBreaker)
- _, err = c.Upsert(nil, nil)
- assert.Equal(t, errDummy, err)
- }
- type mockPromise struct {
- accepted bool
- reason string
- }
- func (p *mockPromise) Accept() {
- p.accepted = true
- }
- func (p *mockPromise) Reject(reason string) {
- p.reason = reason
- }
- type dropBreaker struct {
- }
- func (d *dropBreaker) Name() string {
- return "dummy"
- }
- func (d *dropBreaker) Allow() (breaker.Promise, error) {
- return nil, errDummy
- }
- func (d *dropBreaker) Do(req func() error) error {
- return nil
- }
- func (d *dropBreaker) DoWithAcceptable(req func() error, acceptable breaker.Acceptable) error {
- return errDummy
- }
- func (d *dropBreaker) DoWithFallback(req func() error, fallback func(err error) error) error {
- return nil
- }
- func (d *dropBreaker) DoWithFallbackAcceptable(req func() error, fallback func(err error) error,
- acceptable breaker.Acceptable) error {
- return nil
- }
|