collection_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. package mon
  2. import (
  3. "context"
  4. "errors"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/wuntsong-org/go-zero-plus/core/breaker"
  8. "github.com/wuntsong-org/go-zero-plus/core/logx/logtest"
  9. "github.com/wuntsong-org/go-zero-plus/core/stringx"
  10. "github.com/wuntsong-org/go-zero-plus/core/timex"
  11. "go.mongodb.org/mongo-driver/bson"
  12. "go.mongodb.org/mongo-driver/bson/primitive"
  13. "go.mongodb.org/mongo-driver/mongo"
  14. "go.mongodb.org/mongo-driver/mongo/integration/mtest"
  15. mopt "go.mongodb.org/mongo-driver/mongo/options"
  16. )
  17. var errDummy = errors.New("dummy")
  18. func TestKeepPromise_accept(t *testing.T) {
  19. p := new(mockPromise)
  20. kp := keepablePromise{
  21. promise: p,
  22. log: func(error) {},
  23. }
  24. assert.Nil(t, kp.accept(nil))
  25. assert.Equal(t, ErrNotFound, kp.accept(ErrNotFound))
  26. }
  27. func TestKeepPromise_keep(t *testing.T) {
  28. tests := []struct {
  29. err error
  30. accepted bool
  31. reason string
  32. }{
  33. {
  34. err: nil,
  35. accepted: true,
  36. reason: "",
  37. },
  38. {
  39. err: ErrNotFound,
  40. accepted: true,
  41. reason: "",
  42. },
  43. {
  44. err: errors.New("any"),
  45. accepted: false,
  46. reason: "any",
  47. },
  48. }
  49. for _, test := range tests {
  50. t.Run(stringx.RandId(), func(t *testing.T) {
  51. p := new(mockPromise)
  52. kp := keepablePromise{
  53. promise: p,
  54. log: func(error) {},
  55. }
  56. assert.Equal(t, test.err, kp.keep(test.err))
  57. assert.Equal(t, test.accepted, p.accepted)
  58. assert.Equal(t, test.reason, p.reason)
  59. })
  60. }
  61. }
  62. func TestNewCollection(t *testing.T) {
  63. mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
  64. mt.Run("test", func(mt *mtest.T) {
  65. coll := mt.Coll
  66. assert.NotNil(t, coll)
  67. col := newCollection(coll, breaker.GetBreaker("localhost"))
  68. assert.Equal(t, t.Name()+"/test", col.(*decoratedCollection).name)
  69. })
  70. }
  71. func TestCollection_Aggregate(t *testing.T) {
  72. mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
  73. mt.Run("test", func(mt *mtest.T) {
  74. coll := mt.Coll
  75. assert.NotNil(t, coll)
  76. col := newCollection(coll, breaker.GetBreaker("localhost"))
  77. ns := mt.Coll.Database().Name() + "." + mt.Coll.Name()
  78. aggRes := mtest.CreateCursorResponse(1, ns, mtest.FirstBatch)
  79. mt.AddMockResponses(aggRes)
  80. assert.Equal(t, t.Name()+"/test", col.(*decoratedCollection).name)
  81. cursor, err := col.Aggregate(context.Background(), mongo.Pipeline{}, mopt.Aggregate())
  82. assert.Nil(t, err)
  83. cursor.Close(context.Background())
  84. })
  85. }
  86. func TestCollection_BulkWrite(t *testing.T) {
  87. mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
  88. mt.Run("test", func(mt *mtest.T) {
  89. c := decoratedCollection{
  90. Collection: mt.Coll,
  91. brk: breaker.NewBreaker(),
  92. }
  93. mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "ok", Value: 1}}...))
  94. res, err := c.BulkWrite(context.Background(), []mongo.WriteModel{
  95. mongo.NewInsertOneModel().SetDocument(bson.D{{Key: "foo", Value: 1}}),
  96. })
  97. assert.Nil(t, err)
  98. assert.NotNil(t, res)
  99. c.brk = new(dropBreaker)
  100. _, err = c.BulkWrite(context.Background(), []mongo.WriteModel{
  101. mongo.NewInsertOneModel().SetDocument(bson.D{{Key: "foo", Value: 1}}),
  102. })
  103. assert.Equal(t, errDummy, err)
  104. })
  105. }
  106. func TestCollection_CountDocuments(t *testing.T) {
  107. mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
  108. mt.Run("test", func(mt *mtest.T) {
  109. c := decoratedCollection{
  110. Collection: mt.Coll,
  111. brk: breaker.NewBreaker(),
  112. }
  113. mt.AddMockResponses(mtest.CreateCursorResponse(
  114. 1,
  115. "DBName.CollectionName",
  116. mtest.FirstBatch,
  117. bson.D{
  118. {Key: "n", Value: 1},
  119. }))
  120. res, err := c.CountDocuments(context.Background(), bson.D{})
  121. assert.Nil(t, err)
  122. assert.Equal(t, int64(1), res)
  123. c.brk = new(dropBreaker)
  124. _, err = c.CountDocuments(context.Background(), bson.D{{Key: "foo", Value: 1}})
  125. assert.Equal(t, errDummy, err)
  126. })
  127. }
  128. func TestDecoratedCollection_DeleteMany(t *testing.T) {
  129. mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
  130. mt.Run("test", func(mt *mtest.T) {
  131. c := decoratedCollection{
  132. Collection: mt.Coll,
  133. brk: breaker.NewBreaker(),
  134. }
  135. mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "n", Value: 1}}...))
  136. res, err := c.DeleteMany(context.Background(), bson.D{})
  137. assert.Nil(t, err)
  138. assert.Equal(t, int64(1), res.DeletedCount)
  139. c.brk = new(dropBreaker)
  140. _, err = c.DeleteMany(context.Background(), bson.D{{Key: "foo", Value: 1}})
  141. assert.Equal(t, errDummy, err)
  142. })
  143. }
  144. func TestCollection_Distinct(t *testing.T) {
  145. mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
  146. mt.Run("test", func(mt *mtest.T) {
  147. c := decoratedCollection{
  148. Collection: mt.Coll,
  149. brk: breaker.NewBreaker(),
  150. }
  151. mt.AddMockResponses(bson.D{{Key: "ok", Value: 1}, {Key: "values", Value: []int{1}}})
  152. resp, err := c.Distinct(context.Background(), "foo", bson.D{})
  153. assert.Nil(t, err)
  154. assert.Equal(t, 1, len(resp))
  155. c.brk = new(dropBreaker)
  156. _, err = c.Distinct(context.Background(), "foo", bson.D{{Key: "foo", Value: 1}})
  157. assert.Equal(t, errDummy, err)
  158. })
  159. }
  160. func TestCollection_EstimatedDocumentCount(t *testing.T) {
  161. mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
  162. mt.Run("test", func(mt *mtest.T) {
  163. c := decoratedCollection{
  164. Collection: mt.Coll,
  165. brk: breaker.NewBreaker(),
  166. }
  167. mt.AddMockResponses(bson.D{{Key: "ok", Value: 1}, {Key: "n", Value: 1}})
  168. res, err := c.EstimatedDocumentCount(context.Background())
  169. assert.Nil(t, err)
  170. assert.Equal(t, int64(1), res)
  171. c.brk = new(dropBreaker)
  172. _, err = c.EstimatedDocumentCount(context.Background())
  173. assert.Equal(t, errDummy, err)
  174. })
  175. }
  176. func TestCollection_Find(t *testing.T) {
  177. mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
  178. mt.Run("test", func(mt *mtest.T) {
  179. c := decoratedCollection{
  180. Collection: mt.Coll,
  181. brk: breaker.NewBreaker(),
  182. }
  183. find := mtest.CreateCursorResponse(
  184. 1,
  185. "DBName.CollectionName",
  186. mtest.FirstBatch,
  187. bson.D{
  188. {Key: "name", Value: "John"},
  189. })
  190. getMore := mtest.CreateCursorResponse(
  191. 1,
  192. "DBName.CollectionName",
  193. mtest.NextBatch,
  194. bson.D{
  195. {Key: "name", Value: "Mary"},
  196. })
  197. killCursors := mtest.CreateCursorResponse(
  198. 0,
  199. "DBName.CollectionName",
  200. mtest.NextBatch)
  201. mt.AddMockResponses(find, getMore, killCursors)
  202. filter := bson.D{{Key: "x", Value: 1}}
  203. cursor, err := c.Find(context.Background(), filter, mopt.Find())
  204. assert.Nil(t, err)
  205. defer cursor.Close(context.Background())
  206. var val []struct {
  207. ID primitive.ObjectID `bson:"_id"`
  208. Name string `bson:"name"`
  209. }
  210. assert.Nil(t, cursor.All(context.Background(), &val))
  211. assert.Equal(t, 2, len(val))
  212. assert.Equal(t, "John", val[0].Name)
  213. assert.Equal(t, "Mary", val[1].Name)
  214. c.brk = new(dropBreaker)
  215. _, err = c.Find(context.Background(), filter, mopt.Find())
  216. assert.Equal(t, errDummy, err)
  217. })
  218. }
  219. func TestCollection_FindOne(t *testing.T) {
  220. mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
  221. mt.Run("test", func(mt *mtest.T) {
  222. c := decoratedCollection{
  223. Collection: mt.Coll,
  224. brk: breaker.NewBreaker(),
  225. }
  226. find := mtest.CreateCursorResponse(
  227. 1,
  228. "DBName.CollectionName",
  229. mtest.FirstBatch,
  230. bson.D{
  231. {Key: "name", Value: "John"},
  232. })
  233. getMore := mtest.CreateCursorResponse(
  234. 1,
  235. "DBName.CollectionName",
  236. mtest.NextBatch,
  237. bson.D{
  238. {Key: "name", Value: "Mary"},
  239. })
  240. killCursors := mtest.CreateCursorResponse(
  241. 0,
  242. "DBName.CollectionName",
  243. mtest.NextBatch)
  244. mt.AddMockResponses(find, getMore, killCursors)
  245. filter := bson.D{{Key: "x", Value: 1}}
  246. resp, err := c.FindOne(context.Background(), filter)
  247. assert.Nil(t, err)
  248. var val struct {
  249. ID primitive.ObjectID `bson:"_id"`
  250. Name string `bson:"name"`
  251. }
  252. assert.Nil(t, resp.Decode(&val))
  253. assert.Equal(t, "John", val.Name)
  254. c.brk = new(dropBreaker)
  255. _, err = c.FindOne(context.Background(), filter)
  256. assert.Equal(t, errDummy, err)
  257. })
  258. }
  259. func TestCollection_FindOneAndDelete(t *testing.T) {
  260. mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
  261. mt.Run("test", func(mt *mtest.T) {
  262. c := decoratedCollection{
  263. Collection: mt.Coll,
  264. brk: breaker.NewBreaker(),
  265. }
  266. filter := bson.D{}
  267. mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{}...))
  268. _, err := c.FindOneAndDelete(context.Background(), filter, mopt.FindOneAndDelete())
  269. assert.Equal(t, mongo.ErrNoDocuments, err)
  270. mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{
  271. {Key: "value", Value: bson.D{{Key: "name", Value: "John"}}},
  272. }...))
  273. resp, err := c.FindOneAndDelete(context.Background(), filter, mopt.FindOneAndDelete())
  274. assert.Nil(t, err)
  275. var val struct {
  276. Name string `bson:"name"`
  277. }
  278. assert.Nil(t, resp.Decode(&val))
  279. assert.Equal(t, "John", val.Name)
  280. c.brk = new(dropBreaker)
  281. _, err = c.FindOneAndDelete(context.Background(), bson.D{{Key: "foo", Value: "bar"}})
  282. assert.Equal(t, errDummy, err)
  283. })
  284. }
  285. func TestCollection_FindOneAndReplace(t *testing.T) {
  286. mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
  287. mt.Run("test", func(mt *mtest.T) {
  288. c := decoratedCollection{
  289. Collection: mt.Coll,
  290. brk: breaker.NewBreaker(),
  291. }
  292. mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{}...))
  293. filter := bson.D{{Key: "x", Value: 1}}
  294. replacement := bson.D{{Key: "x", Value: 2}}
  295. opts := mopt.FindOneAndReplace().SetUpsert(true)
  296. _, err := c.FindOneAndReplace(context.Background(), filter, replacement, opts)
  297. assert.Equal(t, mongo.ErrNoDocuments, err)
  298. mt.AddMockResponses(bson.D{{Key: "ok", Value: 1}, {Key: "value", Value: bson.D{
  299. {Key: "name", Value: "John"},
  300. }}})
  301. resp, err := c.FindOneAndReplace(context.Background(), filter, replacement, opts)
  302. assert.Nil(t, err)
  303. var val struct {
  304. Name string `bson:"name"`
  305. }
  306. assert.Nil(t, resp.Decode(&val))
  307. assert.Equal(t, "John", val.Name)
  308. c.brk = new(dropBreaker)
  309. _, err = c.FindOneAndReplace(context.Background(), filter, replacement, opts)
  310. assert.Equal(t, errDummy, err)
  311. })
  312. }
  313. func TestCollection_FindOneAndUpdate(t *testing.T) {
  314. mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
  315. mt.Run("test", func(mt *mtest.T) {
  316. c := decoratedCollection{
  317. Collection: mt.Coll,
  318. brk: breaker.NewBreaker(),
  319. }
  320. mt.AddMockResponses(bson.D{{Key: "ok", Value: 1}})
  321. filter := bson.D{{Key: "x", Value: 1}}
  322. update := bson.D{{Key: "$x", Value: 2}}
  323. opts := mopt.FindOneAndUpdate().SetUpsert(true)
  324. _, err := c.FindOneAndUpdate(context.Background(), filter, update, opts)
  325. assert.Equal(t, mongo.ErrNoDocuments, err)
  326. mt.AddMockResponses(bson.D{{Key: "ok", Value: 1}, {Key: "value", Value: bson.D{
  327. {Key: "name", Value: "John"},
  328. }}})
  329. resp, err := c.FindOneAndUpdate(context.Background(), filter, update, opts)
  330. assert.Nil(t, err)
  331. var val struct {
  332. Name string `bson:"name"`
  333. }
  334. assert.Nil(t, resp.Decode(&val))
  335. assert.Equal(t, "John", val.Name)
  336. c.brk = new(dropBreaker)
  337. _, err = c.FindOneAndUpdate(context.Background(), filter, update, opts)
  338. assert.Equal(t, errDummy, err)
  339. })
  340. }
  341. func TestCollection_InsertOne(t *testing.T) {
  342. mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
  343. mt.Run("test", func(mt *mtest.T) {
  344. c := decoratedCollection{
  345. Collection: mt.Coll,
  346. brk: breaker.NewBreaker(),
  347. }
  348. mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "ok", Value: 1}}...))
  349. res, err := c.InsertOne(context.Background(), bson.D{{Key: "foo", Value: "bar"}})
  350. assert.Nil(t, err)
  351. assert.NotNil(t, res)
  352. c.brk = new(dropBreaker)
  353. _, err = c.InsertOne(context.Background(), bson.D{{Key: "foo", Value: "bar"}})
  354. assert.Equal(t, errDummy, err)
  355. })
  356. }
  357. func TestCollection_InsertMany(t *testing.T) {
  358. mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
  359. mt.Run("test", func(mt *mtest.T) {
  360. c := decoratedCollection{
  361. Collection: mt.Coll,
  362. brk: breaker.NewBreaker(),
  363. }
  364. mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "ok", Value: 1}}...))
  365. res, err := c.InsertMany(context.Background(), []any{
  366. bson.D{{Key: "foo", Value: "bar"}},
  367. bson.D{{Key: "foo", Value: "baz"}},
  368. })
  369. assert.Nil(t, err)
  370. assert.NotNil(t, res)
  371. assert.Equal(t, 2, len(res.InsertedIDs))
  372. c.brk = new(dropBreaker)
  373. _, err = c.InsertMany(context.Background(), []any{bson.D{{Key: "foo", Value: "bar"}}})
  374. assert.Equal(t, errDummy, err)
  375. })
  376. }
  377. func TestCollection_DeleteOne(t *testing.T) {
  378. mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
  379. mt.Run("test", func(mt *mtest.T) {
  380. c := decoratedCollection{
  381. Collection: mt.Coll,
  382. brk: breaker.NewBreaker(),
  383. }
  384. mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "n", Value: 1}}...))
  385. res, err := c.DeleteOne(context.Background(), bson.D{{Key: "foo", Value: "bar"}})
  386. assert.Nil(t, err)
  387. assert.Equal(t, int64(1), res.DeletedCount)
  388. c.brk = new(dropBreaker)
  389. _, err = c.DeleteOne(context.Background(), bson.D{{Key: "foo", Value: "bar"}})
  390. assert.Equal(t, errDummy, err)
  391. })
  392. }
  393. func TestCollection_DeleteMany(t *testing.T) {
  394. mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
  395. mt.Run("test", func(mt *mtest.T) {
  396. c := decoratedCollection{
  397. Collection: mt.Coll,
  398. brk: breaker.NewBreaker(),
  399. }
  400. mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "n", Value: 1}}...))
  401. res, err := c.DeleteMany(context.Background(), bson.D{{Key: "foo", Value: "bar"}})
  402. assert.Nil(t, err)
  403. assert.Equal(t, int64(1), res.DeletedCount)
  404. c.brk = new(dropBreaker)
  405. _, err = c.DeleteMany(context.Background(), bson.D{{Key: "foo", Value: "bar"}})
  406. assert.Equal(t, errDummy, err)
  407. })
  408. }
  409. func TestCollection_ReplaceOne(t *testing.T) {
  410. mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
  411. mt.Run("test", func(mt *mtest.T) {
  412. c := decoratedCollection{
  413. Collection: mt.Coll,
  414. brk: breaker.NewBreaker(),
  415. }
  416. mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "n", Value: 1}}...))
  417. res, err := c.ReplaceOne(context.Background(), bson.D{{Key: "foo", Value: "bar"}},
  418. bson.D{{Key: "foo", Value: "baz"}},
  419. )
  420. assert.Nil(t, err)
  421. assert.Equal(t, int64(1), res.MatchedCount)
  422. c.brk = new(dropBreaker)
  423. _, err = c.ReplaceOne(context.Background(), bson.D{{Key: "foo", Value: "bar"}},
  424. bson.D{{Key: "foo", Value: "baz"}})
  425. assert.Equal(t, errDummy, err)
  426. })
  427. }
  428. func TestCollection_UpdateOne(t *testing.T) {
  429. mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
  430. mt.Run("test", func(mt *mtest.T) {
  431. c := decoratedCollection{
  432. Collection: mt.Coll,
  433. brk: breaker.NewBreaker(),
  434. }
  435. mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "n", Value: 1}}...))
  436. resp, err := c.UpdateOne(context.Background(), bson.D{{Key: "foo", Value: "bar"}},
  437. bson.D{{Key: "$set", Value: bson.D{{Key: "baz", Value: "qux"}}}})
  438. assert.Nil(t, err)
  439. assert.Equal(t, int64(1), resp.MatchedCount)
  440. c.brk = new(dropBreaker)
  441. _, err = c.UpdateOne(context.Background(), bson.D{{Key: "foo", Value: "bar"}},
  442. bson.D{{Key: "$set", Value: bson.D{{Key: "baz", Value: "qux"}}}})
  443. assert.Equal(t, errDummy, err)
  444. })
  445. }
  446. func TestCollection_UpdateByID(t *testing.T) {
  447. mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
  448. mt.Run("test", func(mt *mtest.T) {
  449. c := decoratedCollection{
  450. Collection: mt.Coll,
  451. brk: breaker.NewBreaker(),
  452. }
  453. mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "n", Value: 1}}...))
  454. resp, err := c.UpdateByID(context.Background(), primitive.NewObjectID(),
  455. bson.D{{Key: "$set", Value: bson.D{{Key: "baz", Value: "qux"}}}})
  456. assert.Nil(t, err)
  457. assert.Equal(t, int64(1), resp.MatchedCount)
  458. c.brk = new(dropBreaker)
  459. _, err = c.UpdateByID(context.Background(), primitive.NewObjectID(),
  460. bson.D{{Key: "$set", Value: bson.D{{Key: "baz", Value: "qux"}}}})
  461. assert.Equal(t, errDummy, err)
  462. })
  463. }
  464. func TestCollection_UpdateMany(t *testing.T) {
  465. mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
  466. mt.Run("test", func(mt *mtest.T) {
  467. c := decoratedCollection{
  468. Collection: mt.Coll,
  469. brk: breaker.NewBreaker(),
  470. }
  471. mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "n", Value: 1}}...))
  472. resp, err := c.UpdateMany(context.Background(), bson.D{{Key: "foo", Value: "bar"}},
  473. bson.D{{Key: "$set", Value: bson.D{{Key: "baz", Value: "qux"}}}})
  474. assert.Nil(t, err)
  475. assert.Equal(t, int64(1), resp.MatchedCount)
  476. c.brk = new(dropBreaker)
  477. _, err = c.UpdateMany(context.Background(), bson.D{{Key: "foo", Value: "bar"}},
  478. bson.D{{Key: "$set", Value: bson.D{{Key: "baz", Value: "qux"}}}})
  479. assert.Equal(t, errDummy, err)
  480. })
  481. }
  482. func TestDecoratedCollection_LogDuration(t *testing.T) {
  483. mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
  484. c := decoratedCollection{
  485. Collection: mt.Coll,
  486. brk: breaker.NewBreaker(),
  487. }
  488. buf := logtest.NewCollector(t)
  489. buf.Reset()
  490. c.logDuration(context.Background(), "foo", timex.Now(), nil, "bar")
  491. assert.Contains(t, buf.String(), "foo")
  492. assert.Contains(t, buf.String(), "bar")
  493. buf.Reset()
  494. c.logDuration(context.Background(), "foo", timex.Now(), errors.New("bar"), make(chan int))
  495. assert.Contains(t, buf.String(), "foo")
  496. assert.Contains(t, buf.String(), "bar")
  497. buf.Reset()
  498. c.logDuration(context.Background(), "foo", timex.Now(), nil, make(chan int))
  499. assert.Contains(t, buf.String(), "foo")
  500. buf.Reset()
  501. c.logDuration(context.Background(), "foo", timex.Now()-slowThreshold.Load()*2,
  502. nil, make(chan int))
  503. assert.Contains(t, buf.String(), "foo")
  504. assert.Contains(t, buf.String(), "slowcall")
  505. buf.Reset()
  506. c.logDuration(context.Background(), "foo", timex.Now()-slowThreshold.Load()*2,
  507. errors.New("bar"), make(chan int))
  508. assert.Contains(t, buf.String(), "foo")
  509. assert.Contains(t, buf.String(), "bar")
  510. buf.Reset()
  511. c.logDuration(context.Background(), "foo", timex.Now()-slowThreshold.Load()*2,
  512. errors.New("bar"))
  513. assert.Contains(t, buf.String(), "foo")
  514. buf.Reset()
  515. c.logDuration(context.Background(), "foo", timex.Now()-slowThreshold.Load()*2, nil)
  516. assert.Contains(t, buf.String(), "foo")
  517. assert.Contains(t, buf.String(), "slowcall")
  518. }
  519. type mockPromise struct {
  520. accepted bool
  521. reason string
  522. }
  523. func (p *mockPromise) Accept() {
  524. p.accepted = true
  525. }
  526. func (p *mockPromise) Reject(reason string) {
  527. p.reason = reason
  528. }
  529. type dropBreaker struct{}
  530. func (d *dropBreaker) Name() string {
  531. return "dummy"
  532. }
  533. func (d *dropBreaker) Allow() (breaker.Promise, error) {
  534. return nil, errDummy
  535. }
  536. func (d *dropBreaker) Do(_ func() error) error {
  537. return nil
  538. }
  539. func (d *dropBreaker) DoWithAcceptable(_ func() error, _ breaker.Acceptable) error {
  540. return errDummy
  541. }
  542. func (d *dropBreaker) DoWithFallback(_ func() error, _ func(err error) error) error {
  543. return nil
  544. }
  545. func (d *dropBreaker) DoWithFallbackAcceptable(_ func() error, _ func(err error) error,
  546. _ breaker.Acceptable) error {
  547. return nil
  548. }