collection_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package mongo
  2. import (
  3. "errors"
  4. "testing"
  5. "github.com/globalsign/mgo"
  6. "github.com/golang/mock/gomock"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/tal-tech/go-zero/core/breaker"
  9. "github.com/tal-tech/go-zero/core/logx"
  10. "github.com/tal-tech/go-zero/core/stores/mongo/internal"
  11. "github.com/tal-tech/go-zero/core/stringx"
  12. )
  13. var errDummy = errors.New("dummy")
  14. func init() {
  15. logx.Disable()
  16. }
  17. func TestKeepPromise_accept(t *testing.T) {
  18. p := new(mockPromise)
  19. kp := keepablePromise{
  20. promise: p,
  21. log: func(error) {},
  22. }
  23. assert.Nil(t, kp.accept(nil))
  24. assert.Equal(t, mgo.ErrNotFound, kp.accept(mgo.ErrNotFound))
  25. }
  26. func TestKeepPromise_keep(t *testing.T) {
  27. tests := []struct {
  28. err error
  29. accepted bool
  30. reason string
  31. }{
  32. {
  33. err: nil,
  34. accepted: true,
  35. reason: "",
  36. },
  37. {
  38. err: mgo.ErrNotFound,
  39. accepted: true,
  40. reason: "",
  41. },
  42. {
  43. err: errors.New("any"),
  44. accepted: false,
  45. reason: "any",
  46. },
  47. }
  48. for _, test := range tests {
  49. t.Run(stringx.RandId(), func(t *testing.T) {
  50. p := new(mockPromise)
  51. kp := keepablePromise{
  52. promise: p,
  53. log: func(error) {},
  54. }
  55. assert.Equal(t, test.err, kp.keep(test.err))
  56. assert.Equal(t, test.accepted, p.accepted)
  57. assert.Equal(t, test.reason, p.reason)
  58. })
  59. }
  60. }
  61. func TestNewCollection(t *testing.T) {
  62. col := newCollection(&mgo.Collection{
  63. Database: nil,
  64. Name: "foo",
  65. FullName: "bar",
  66. })
  67. assert.Equal(t, "bar", col.(*decoratedCollection).name)
  68. }
  69. func TestCollectionFind(t *testing.T) {
  70. ctrl := gomock.NewController(t)
  71. defer ctrl.Finish()
  72. var query mgo.Query
  73. col := internal.NewMockMgoCollection(ctrl)
  74. col.EXPECT().Find(gomock.Any()).Return(&query)
  75. c := decoratedCollection{
  76. collection: col,
  77. brk: breaker.NewBreaker(),
  78. }
  79. actual := c.Find(nil)
  80. switch v := actual.(type) {
  81. case promisedQuery:
  82. assert.Equal(t, &query, v.Query)
  83. assert.Equal(t, errDummy, v.promise.keep(errDummy))
  84. default:
  85. t.Fail()
  86. }
  87. c.brk = new(dropBreaker)
  88. actual = c.Find(nil)
  89. assert.Equal(t, rejectedQuery{}, actual)
  90. }
  91. func TestCollectionFindId(t *testing.T) {
  92. ctrl := gomock.NewController(t)
  93. defer ctrl.Finish()
  94. var query mgo.Query
  95. col := internal.NewMockMgoCollection(ctrl)
  96. col.EXPECT().FindId(gomock.Any()).Return(&query)
  97. c := decoratedCollection{
  98. collection: col,
  99. brk: breaker.NewBreaker(),
  100. }
  101. actual := c.FindId(nil)
  102. switch v := actual.(type) {
  103. case promisedQuery:
  104. assert.Equal(t, &query, v.Query)
  105. assert.Equal(t, errDummy, v.promise.keep(errDummy))
  106. default:
  107. t.Fail()
  108. }
  109. c.brk = new(dropBreaker)
  110. actual = c.FindId(nil)
  111. assert.Equal(t, rejectedQuery{}, actual)
  112. }
  113. func TestCollectionInsert(t *testing.T) {
  114. ctrl := gomock.NewController(t)
  115. defer ctrl.Finish()
  116. col := internal.NewMockMgoCollection(ctrl)
  117. col.EXPECT().Insert(nil, nil).Return(errDummy)
  118. c := decoratedCollection{
  119. collection: col,
  120. brk: breaker.NewBreaker(),
  121. }
  122. err := c.Insert(nil, nil)
  123. assert.Equal(t, errDummy, err)
  124. c.brk = new(dropBreaker)
  125. err = c.Insert(nil, nil)
  126. assert.Equal(t, errDummy, err)
  127. }
  128. func TestCollectionPipe(t *testing.T) {
  129. ctrl := gomock.NewController(t)
  130. defer ctrl.Finish()
  131. var pipe mgo.Pipe
  132. col := internal.NewMockMgoCollection(ctrl)
  133. col.EXPECT().Pipe(gomock.Any()).Return(&pipe)
  134. c := decoratedCollection{
  135. collection: col,
  136. brk: breaker.NewBreaker(),
  137. }
  138. actual := c.Pipe(nil)
  139. switch v := actual.(type) {
  140. case promisedPipe:
  141. assert.Equal(t, &pipe, v.Pipe)
  142. assert.Equal(t, errDummy, v.promise.keep(errDummy))
  143. default:
  144. t.Fail()
  145. }
  146. c.brk = new(dropBreaker)
  147. actual = c.Pipe(nil)
  148. assert.Equal(t, rejectedPipe{}, actual)
  149. }
  150. func TestCollectionRemove(t *testing.T) {
  151. ctrl := gomock.NewController(t)
  152. defer ctrl.Finish()
  153. col := internal.NewMockMgoCollection(ctrl)
  154. col.EXPECT().Remove(gomock.Any()).Return(errDummy)
  155. c := decoratedCollection{
  156. collection: col,
  157. brk: breaker.NewBreaker(),
  158. }
  159. err := c.Remove(nil)
  160. assert.Equal(t, errDummy, err)
  161. c.brk = new(dropBreaker)
  162. err = c.Remove(nil)
  163. assert.Equal(t, errDummy, err)
  164. }
  165. func TestCollectionRemoveAll(t *testing.T) {
  166. ctrl := gomock.NewController(t)
  167. defer ctrl.Finish()
  168. col := internal.NewMockMgoCollection(ctrl)
  169. col.EXPECT().RemoveAll(gomock.Any()).Return(nil, errDummy)
  170. c := decoratedCollection{
  171. collection: col,
  172. brk: breaker.NewBreaker(),
  173. }
  174. _, err := c.RemoveAll(nil)
  175. assert.Equal(t, errDummy, err)
  176. c.brk = new(dropBreaker)
  177. _, err = c.RemoveAll(nil)
  178. assert.Equal(t, errDummy, err)
  179. }
  180. func TestCollectionRemoveId(t *testing.T) {
  181. ctrl := gomock.NewController(t)
  182. defer ctrl.Finish()
  183. col := internal.NewMockMgoCollection(ctrl)
  184. col.EXPECT().RemoveId(gomock.Any()).Return(errDummy)
  185. c := decoratedCollection{
  186. collection: col,
  187. brk: breaker.NewBreaker(),
  188. }
  189. err := c.RemoveId(nil)
  190. assert.Equal(t, errDummy, err)
  191. c.brk = new(dropBreaker)
  192. err = c.RemoveId(nil)
  193. assert.Equal(t, errDummy, err)
  194. }
  195. func TestCollectionUpdate(t *testing.T) {
  196. ctrl := gomock.NewController(t)
  197. defer ctrl.Finish()
  198. col := internal.NewMockMgoCollection(ctrl)
  199. col.EXPECT().Update(gomock.Any(), gomock.Any()).Return(errDummy)
  200. c := decoratedCollection{
  201. collection: col,
  202. brk: breaker.NewBreaker(),
  203. }
  204. err := c.Update(nil, nil)
  205. assert.Equal(t, errDummy, err)
  206. c.brk = new(dropBreaker)
  207. err = c.Update(nil, nil)
  208. assert.Equal(t, errDummy, err)
  209. }
  210. func TestCollectionUpdateId(t *testing.T) {
  211. ctrl := gomock.NewController(t)
  212. defer ctrl.Finish()
  213. col := internal.NewMockMgoCollection(ctrl)
  214. col.EXPECT().UpdateId(gomock.Any(), gomock.Any()).Return(errDummy)
  215. c := decoratedCollection{
  216. collection: col,
  217. brk: breaker.NewBreaker(),
  218. }
  219. err := c.UpdateId(nil, nil)
  220. assert.Equal(t, errDummy, err)
  221. c.brk = new(dropBreaker)
  222. err = c.UpdateId(nil, nil)
  223. assert.Equal(t, errDummy, err)
  224. }
  225. func TestCollectionUpsert(t *testing.T) {
  226. ctrl := gomock.NewController(t)
  227. defer ctrl.Finish()
  228. col := internal.NewMockMgoCollection(ctrl)
  229. col.EXPECT().Upsert(gomock.Any(), gomock.Any()).Return(nil, errDummy)
  230. c := decoratedCollection{
  231. collection: col,
  232. brk: breaker.NewBreaker(),
  233. }
  234. _, err := c.Upsert(nil, nil)
  235. assert.Equal(t, errDummy, err)
  236. c.brk = new(dropBreaker)
  237. _, err = c.Upsert(nil, nil)
  238. assert.Equal(t, errDummy, err)
  239. }
  240. type mockPromise struct {
  241. accepted bool
  242. reason string
  243. }
  244. func (p *mockPromise) Accept() {
  245. p.accepted = true
  246. }
  247. func (p *mockPromise) Reject(reason string) {
  248. p.reason = reason
  249. }
  250. type dropBreaker struct {
  251. }
  252. func (d *dropBreaker) Name() string {
  253. return "dummy"
  254. }
  255. func (d *dropBreaker) Allow() (breaker.Promise, error) {
  256. return nil, errDummy
  257. }
  258. func (d *dropBreaker) Do(req func() error) error {
  259. return nil
  260. }
  261. func (d *dropBreaker) DoWithAcceptable(req func() error, acceptable breaker.Acceptable) error {
  262. return errDummy
  263. }
  264. func (d *dropBreaker) DoWithFallback(req func() error, fallback func(err error) error) error {
  265. return nil
  266. }
  267. func (d *dropBreaker) DoWithFallbackAcceptable(req func() error, fallback func(err error) error,
  268. acceptable breaker.Acceptable) error {
  269. return nil
  270. }