123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- 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/stringx"
- "github.com/tal-tech/go-zero/core/syncx"
- )
- func TestClosableIter_Close(t *testing.T) {
- errs := []error{
- nil,
- mgo.ErrNotFound,
- }
- for _, err := range errs {
- t.Run(stringx.RandId(), func(t *testing.T) {
- ctrl := gomock.NewController(t)
- defer ctrl.Finish()
- cleaned := syncx.NewAtomicBool()
- iter := NewMockIter(ctrl)
- iter.EXPECT().Close().Return(err)
- ci := ClosableIter{
- Iter: iter,
- Cleanup: func() {
- cleaned.Set(true)
- },
- }
- assert.Equal(t, err, ci.Close())
- assert.True(t, cleaned.True())
- })
- }
- }
- func TestPromisedIter_AllAndClose(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) {
- ctrl := gomock.NewController(t)
- defer ctrl.Finish()
- iter := NewMockIter(ctrl)
- iter.EXPECT().All(gomock.Any()).Return(test.err)
- promise := new(mockPromise)
- pi := promisedIter{
- Iter: iter,
- promise: keepablePromise{
- promise: promise,
- log: func(error) {},
- },
- }
- assert.Equal(t, test.err, pi.All(nil))
- assert.Equal(t, test.accepted, promise.accepted)
- assert.Equal(t, test.reason, promise.reason)
- })
- }
- for _, test := range tests {
- t.Run(stringx.RandId(), func(t *testing.T) {
- ctrl := gomock.NewController(t)
- defer ctrl.Finish()
- iter := NewMockIter(ctrl)
- iter.EXPECT().Close().Return(test.err)
- promise := new(mockPromise)
- pi := promisedIter{
- Iter: iter,
- promise: keepablePromise{
- promise: promise,
- log: func(error) {},
- },
- }
- assert.Equal(t, test.err, pi.Close())
- assert.Equal(t, test.accepted, promise.accepted)
- assert.Equal(t, test.reason, promise.reason)
- })
- }
- }
- func TestPromisedIter_Err(t *testing.T) {
- errs := []error{
- nil,
- mgo.ErrNotFound,
- }
- for _, err := range errs {
- t.Run(stringx.RandId(), func(t *testing.T) {
- ctrl := gomock.NewController(t)
- defer ctrl.Finish()
- iter := NewMockIter(ctrl)
- iter.EXPECT().Err().Return(err)
- promise := new(mockPromise)
- pi := promisedIter{
- Iter: iter,
- promise: keepablePromise{
- promise: promise,
- log: func(error) {},
- },
- }
- assert.Equal(t, err, pi.Err())
- })
- }
- }
- func TestPromisedIter_For(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) {
- ctrl := gomock.NewController(t)
- defer ctrl.Finish()
- iter := NewMockIter(ctrl)
- iter.EXPECT().For(gomock.Any(), gomock.Any()).Return(test.err)
- promise := new(mockPromise)
- pi := promisedIter{
- Iter: iter,
- promise: keepablePromise{
- promise: promise,
- log: func(error) {},
- },
- }
- assert.Equal(t, test.err, pi.For(nil, nil))
- assert.Equal(t, test.accepted, promise.accepted)
- assert.Equal(t, test.reason, promise.reason)
- })
- }
- }
- func TestRejectedIter_All(t *testing.T) {
- assert.Equal(t, breaker.ErrServiceUnavailable, new(rejectedIter).All(nil))
- }
- func TestRejectedIter_Close(t *testing.T) {
- assert.Equal(t, breaker.ErrServiceUnavailable, new(rejectedIter).Close())
- }
- func TestRejectedIter_Done(t *testing.T) {
- assert.False(t, new(rejectedIter).Done())
- }
- func TestRejectedIter_Err(t *testing.T) {
- assert.Equal(t, breaker.ErrServiceUnavailable, new(rejectedIter).Err())
- }
- func TestRejectedIter_For(t *testing.T) {
- assert.Equal(t, breaker.ErrServiceUnavailable, new(rejectedIter).For(nil, nil))
- }
- func TestRejectedIter_Next(t *testing.T) {
- assert.False(t, new(rejectedIter).Next(nil))
- }
- func TestRejectedIter_State(t *testing.T) {
- n, raw := new(rejectedIter).State()
- assert.Equal(t, int64(0), n)
- assert.Nil(t, raw)
- }
- func TestRejectedIter_Timeout(t *testing.T) {
- assert.False(t, new(rejectedIter).Timeout())
- }
- func TestIter_Done(t *testing.T) {
- ctrl := gomock.NewController(t)
- defer ctrl.Finish()
- iter := NewMockIter(ctrl)
- iter.EXPECT().Done().Return(true)
- ci := ClosableIter{
- Iter: iter,
- Cleanup: nil,
- }
- assert.True(t, ci.Done())
- }
- func TestIter_Next(t *testing.T) {
- ctrl := gomock.NewController(t)
- defer ctrl.Finish()
- iter := NewMockIter(ctrl)
- iter.EXPECT().Next(gomock.Any()).Return(true)
- ci := ClosableIter{
- Iter: iter,
- Cleanup: nil,
- }
- assert.True(t, ci.Next(nil))
- }
- func TestIter_State(t *testing.T) {
- ctrl := gomock.NewController(t)
- defer ctrl.Finish()
- iter := NewMockIter(ctrl)
- iter.EXPECT().State().Return(int64(1), nil)
- ci := ClosableIter{
- Iter: iter,
- Cleanup: nil,
- }
- n, raw := ci.State()
- assert.Equal(t, int64(1), n)
- assert.Nil(t, raw)
- }
- func TestIter_Timeout(t *testing.T) {
- ctrl := gomock.NewController(t)
- defer ctrl.Finish()
- iter := NewMockIter(ctrl)
- iter.EXPECT().Timeout().Return(true)
- ci := ClosableIter{
- Iter: iter,
- Cleanup: nil,
- }
- assert.True(t, ci.Timeout())
- }
|