RedisManagerTests.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. /* eslint-disable
  2. camelcase,
  3. mocha/no-identical-title,
  4. no-return-assign,
  5. no-undef,
  6. no-unused-vars,
  7. */
  8. // TODO: This file was created by bulk-decaffeinate.
  9. // Fix any style issues and re-enable lint.
  10. /*
  11. * decaffeinate suggestions:
  12. * DS101: Remove unnecessary use of Array.from
  13. * DS102: Remove unnecessary code created because of implicit returns
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. import sinon from 'sinon'
  17. import { expect } from 'chai'
  18. import { strict as esmock } from 'esmock'
  19. const MODULE_PATH = '../../../../app/js/RedisManager.js'
  20. describe('RedisManager', function () {
  21. beforeEach(async function () {
  22. this.rclient = {
  23. auth: sinon.stub(),
  24. exec: sinon.stub().yields(),
  25. lrange: sinon.stub(),
  26. lrem: sinon.stub(),
  27. srem: sinon.stub(),
  28. }
  29. this.rclient.multi = sinon.stub().returns(this.rclient)
  30. this.RedisWrapper = {
  31. createClient: sinon.stub().returns(this.rclient),
  32. }
  33. this.Settings = {
  34. redis: {
  35. project_history: {
  36. key_schema: {
  37. projectHistoryOps({ project_id }) {
  38. return `Project:HistoryOps:${project_id}`
  39. },
  40. },
  41. },
  42. },
  43. }
  44. this.Metrics = {
  45. timing: sinon.stub(),
  46. summary: sinon.stub(),
  47. globalGauge: sinon.stub(),
  48. }
  49. this.RedisManager = await esmock(MODULE_PATH, {
  50. '@overleaf/redis-wrapper': this.RedisWrapper,
  51. '@overleaf/settings': this.Settings,
  52. '@overleaf/metrics': this.Metrics,
  53. })
  54. this.project_id = 'project-id-123'
  55. this.batch_size = 100
  56. this.updates = [
  57. { v: 42, op: 'mock-op-42' },
  58. { v: 45, op: 'mock-op-45' },
  59. ]
  60. this.json_updates = Array.from(this.updates).map(update =>
  61. JSON.stringify(update)
  62. )
  63. return (this.callback = sinon.stub())
  64. })
  65. describe('getOldestDocUpdates', function () {
  66. beforeEach(function () {
  67. this.rclient.lrange.yields(null, this.json_updates)
  68. return this.RedisManager.getOldestDocUpdates(
  69. this.project_id,
  70. this.batch_size,
  71. this.callback
  72. )
  73. })
  74. it('should read the updates from redis', function () {
  75. return this.rclient.lrange
  76. .calledWith(
  77. `Project:HistoryOps:${this.project_id}`,
  78. 0,
  79. this.batch_size - 1
  80. )
  81. .should.equal(true)
  82. })
  83. return it('should call the callback with the unparsed ops', function () {
  84. return this.callback
  85. .calledWith(null, this.json_updates)
  86. .should.equal(true)
  87. })
  88. })
  89. describe('parseDocUpdates', function () {
  90. beforeEach(function () {
  91. return this.RedisManager.parseDocUpdates(this.json_updates, this.callback)
  92. })
  93. return it('should call the callback with the parsed ops', function () {
  94. return this.callback.calledWith(null, this.updates).should.equal(true)
  95. })
  96. })
  97. describe('deleteAppliedDocUpdates', function () {
  98. beforeEach(function () {
  99. return this.RedisManager.deleteAppliedDocUpdates(
  100. this.project_id,
  101. this.json_updates,
  102. this.callback
  103. )
  104. })
  105. it('should delete the first update from redis', function () {
  106. this.rclient.lrem.should.have.been.calledWith(
  107. `Project:HistoryOps:${this.project_id}`,
  108. 1,
  109. this.json_updates[0]
  110. )
  111. })
  112. it('should delete the second update from redis', function () {
  113. return this.rclient.lrem
  114. .calledWith(
  115. `Project:HistoryOps:${this.project_id}`,
  116. 1,
  117. this.json_updates[1]
  118. )
  119. .should.equal(true)
  120. })
  121. return it('should call the callback ', function () {
  122. return this.callback.called.should.equal(true)
  123. })
  124. })
  125. return describe('getUpdatesInBatches', function () {
  126. beforeEach(function () {
  127. this.rawUpdates = ['raw-update-1', 'raw-update-2']
  128. this.expandedUpdates = ['expanded-update-1', 'expanded-update-2']
  129. this.RedisManager._mocks.deleteAppliedDocUpdates = sinon.stub().yields()
  130. this.isProjectHistoryEnabled = true
  131. return (this.runner = sinon
  132. .stub()
  133. .yields(null, this.isProjectHistoryEnabled))
  134. })
  135. describe('single batch smaller than batch size', function () {
  136. beforeEach(function (done) {
  137. this.RedisManager._mocks.getOldestDocUpdates = sinon
  138. .stub()
  139. .yields(null, this.rawUpdates)
  140. this.RedisManager._mocks.parseDocUpdates = sinon
  141. .stub()
  142. .yields(null, this.expandedUpdates)
  143. return this.RedisManager.getUpdatesInBatches(
  144. this.project_id,
  145. 3,
  146. this.runner,
  147. (error, isProjectHistoryEnabled) => {
  148. this.callback(error, isProjectHistoryEnabled)
  149. return done()
  150. }
  151. )
  152. })
  153. it('requests a single batch of updates', function () {
  154. return this.RedisManager._mocks.getOldestDocUpdates.callCount.should.equal(
  155. 1
  156. )
  157. })
  158. it('calls the runner once', function () {
  159. return this.runner.callCount.should.equal(1)
  160. })
  161. it('calls the runner with the updates', function () {
  162. return this.runner.calledWith(this.expandedUpdates).should.equal(true)
  163. })
  164. it('deletes the applied updates', function () {
  165. return this.RedisManager._mocks.deleteAppliedDocUpdates
  166. .calledWith(
  167. this.project_id,
  168. sinon.match.array.deepEquals(this.rawUpdates)
  169. )
  170. .should.equal(true)
  171. })
  172. return it('calls the callback with the result of the runner', function () {
  173. return this.callback
  174. .calledWith(null, this.isProjectHistoryEnabled)
  175. .should.equal(true)
  176. })
  177. })
  178. describe('single batch at batch size', function () {
  179. beforeEach(function (done) {
  180. this.RedisManager._mocks.getOldestDocUpdates = sinon.stub()
  181. this.RedisManager._mocks.getOldestDocUpdates
  182. .onCall(0)
  183. .yields(null, this.rawUpdates)
  184. this.RedisManager._mocks.getOldestDocUpdates.onCall(1).yields(null, [])
  185. this.RedisManager._mocks.parseDocUpdates = sinon.stub()
  186. this.RedisManager._mocks.parseDocUpdates
  187. .onCall(0)
  188. .yields(null, this.expandedUpdates)
  189. return this.RedisManager.getUpdatesInBatches(
  190. this.project_id,
  191. 2,
  192. this.runner,
  193. (error, isProjectHistoryEnabled) => {
  194. this.callback(error, isProjectHistoryEnabled)
  195. return done()
  196. }
  197. )
  198. })
  199. it('requests a second batch of updates', function () {
  200. return this.RedisManager._mocks.getOldestDocUpdates.callCount.should.equal(
  201. 2
  202. )
  203. })
  204. it('calls the runner once', function () {
  205. return this.runner.callCount.should.equal(1)
  206. })
  207. it('calls the runner with the updates', function () {
  208. return this.runner.calledWith(this.expandedUpdates).should.equal(true)
  209. })
  210. it('deletes the applied updates', function () {
  211. return this.RedisManager._mocks.deleteAppliedDocUpdates
  212. .calledWith(
  213. this.project_id,
  214. sinon.match.array.deepEquals(this.rawUpdates)
  215. )
  216. .should.equal(true)
  217. })
  218. return it('calls the callback with the result of the runner', function () {
  219. return this.callback
  220. .calledWith(null, this.isProjectHistoryEnabled)
  221. .should.equal(true)
  222. })
  223. })
  224. describe('single batch exceeding size limit on updates', function () {
  225. beforeEach(function (done) {
  226. this.rawUpdates0 = ['raw-update-1-12345678', 'raw-update-2-12345678']
  227. this.rawUpdates1 = ['raw-update-2-12345678']
  228. this.expandedUpdates0 = ['expanded-update-1']
  229. this.expandedUpdates1 = ['expanded-update-2']
  230. // set the threshold below the size of the first update
  231. this.RedisManager.setRawUpdateSizeThreshold(
  232. this.rawUpdates0[0].length - 1
  233. )
  234. this.RedisManager._mocks.getOldestDocUpdates = sinon.stub()
  235. this.RedisManager._mocks.getOldestDocUpdates
  236. .onCall(0)
  237. .yields(null, this.rawUpdates0)
  238. this.RedisManager._mocks.getOldestDocUpdates
  239. .onCall(1)
  240. .yields(null, this.rawUpdates1)
  241. this.RedisManager._mocks.parseDocUpdates = sinon.stub()
  242. this.RedisManager._mocks.parseDocUpdates
  243. .onCall(0)
  244. .yields(null, this.expandedUpdates0)
  245. this.RedisManager._mocks.parseDocUpdates
  246. .onCall(1)
  247. .yields(null, this.expandedUpdates1)
  248. return this.RedisManager.getUpdatesInBatches(
  249. this.project_id,
  250. 2,
  251. this.runner,
  252. (error, isProjectHistoryEnabled) => {
  253. this.callback(error, isProjectHistoryEnabled)
  254. return done()
  255. }
  256. )
  257. })
  258. it('requests a second batch of updates', function () {
  259. return this.RedisManager._mocks.getOldestDocUpdates.callCount.should.equal(
  260. 2
  261. )
  262. })
  263. it('calls the runner twice', function () {
  264. return this.runner.callCount.should.equal(2)
  265. })
  266. it('calls the runner with the first updates', function () {
  267. return this.runner.calledWith(this.expandedUpdates0).should.equal(true)
  268. })
  269. it('deletes the first set of applied updates', function () {
  270. return this.RedisManager._mocks.deleteAppliedDocUpdates
  271. .calledWith(
  272. this.project_id,
  273. sinon.match.array.deepEquals([this.rawUpdates0[0]])
  274. )
  275. .should.equal(true)
  276. })
  277. it('calls the runner with the second updates', function () {
  278. return this.runner.calledWith(this.expandedUpdates1).should.equal(true)
  279. })
  280. it('deletes the second set of applied updates', function () {
  281. return this.RedisManager._mocks.deleteAppliedDocUpdates
  282. .calledWith(
  283. this.project_id,
  284. sinon.match.array.deepEquals([this.rawUpdates0[1]])
  285. )
  286. .should.equal(true)
  287. })
  288. return it('calls the callback with the result of the runner', function () {
  289. return this.callback
  290. .calledWith(null, this.isProjectHistoryEnabled)
  291. .should.equal(true)
  292. })
  293. })
  294. describe('two batches with first update below and second update above the size limit on updates', function () {
  295. beforeEach(function (done) {
  296. this.rawUpdates0 = ['raw-update-1', 'raw-update-2-12345678']
  297. this.rawUpdates1 = ['raw-update-2-12345678']
  298. this.expandedUpdates0 = ['expanded-update-1']
  299. this.expandedUpdates1 = ['expanded-update-2']
  300. // set the threshold above the size of the first update, but below the total size
  301. this.RedisManager.setRawUpdateSizeThreshold(
  302. this.rawUpdates0[0].length + 1
  303. )
  304. this.RedisManager._mocks.getOldestDocUpdates = sinon.stub()
  305. this.RedisManager._mocks.getOldestDocUpdates
  306. .onCall(0)
  307. .yields(null, this.rawUpdates0)
  308. this.RedisManager._mocks.getOldestDocUpdates
  309. .onCall(1)
  310. .yields(null, this.rawUpdates1)
  311. this.RedisManager._mocks.parseDocUpdates = sinon.stub()
  312. this.RedisManager._mocks.parseDocUpdates
  313. .onCall(0)
  314. .yields(null, this.expandedUpdates0)
  315. this.RedisManager._mocks.parseDocUpdates
  316. .onCall(1)
  317. .yields(null, this.expandedUpdates1)
  318. return this.RedisManager.getUpdatesInBatches(
  319. this.project_id,
  320. 2,
  321. this.runner,
  322. (error, isProjectHistoryEnabled) => {
  323. this.callback(error, isProjectHistoryEnabled)
  324. return done()
  325. }
  326. )
  327. })
  328. it('requests a second batch of updates', function () {
  329. return this.RedisManager._mocks.getOldestDocUpdates.callCount.should.equal(
  330. 2
  331. )
  332. })
  333. it('calls the runner twice', function () {
  334. return this.runner.callCount.should.equal(2)
  335. })
  336. it('calls the runner with the first update', function () {
  337. return this.runner.calledWith(this.expandedUpdates0).should.equal(true)
  338. })
  339. it('deletes the first set of applied updates', function () {
  340. return this.RedisManager._mocks.deleteAppliedDocUpdates
  341. .calledWith(
  342. this.project_id,
  343. sinon.match.array.deepEquals([this.rawUpdates0[0]])
  344. )
  345. .should.equal(true)
  346. })
  347. it('calls the runner with the second update', function () {
  348. return this.runner.calledWith(this.expandedUpdates1).should.equal(true)
  349. })
  350. it('deletes the second set of applied updates', function () {
  351. return this.RedisManager._mocks.deleteAppliedDocUpdates
  352. .calledWith(
  353. this.project_id,
  354. sinon.match.array.deepEquals([this.rawUpdates0[1]])
  355. )
  356. .should.equal(true)
  357. })
  358. return it('calls the callback with the result of the runner', function () {
  359. return this.callback
  360. .calledWith(null, this.isProjectHistoryEnabled)
  361. .should.equal(true)
  362. })
  363. })
  364. describe('single batch exceeding op count limit on updates', function () {
  365. beforeEach(function (done) {
  366. this.rawUpdates0 = [
  367. "{op: ['a', 'b', 'c', 'd']}",
  368. "{op:['e', 'f', 'g', 'h']}",
  369. ]
  370. this.rawUpdates1 = ["{op:['e', 'f', 'g', 'h']}"]
  371. this.expandedUpdates0 = [
  372. { op: ['a', 'b', 'c', 'd'] },
  373. { op: ['e', 'f', 'g', 'h'] },
  374. ]
  375. this.expandedUpdates1 = [{ op: ['e', 'f', 'g', 'h'] }]
  376. // set the threshold below the size of the first update
  377. this.RedisManager.setMaxUpdateOpLength(
  378. this.expandedUpdates0[0].op.length - 1
  379. )
  380. this.RedisManager._mocks.getOldestDocUpdates = sinon.stub()
  381. this.RedisManager._mocks.getOldestDocUpdates
  382. .onCall(0)
  383. .yields(null, this.rawUpdates0)
  384. this.RedisManager._mocks.getOldestDocUpdates
  385. .onCall(1)
  386. .yields(null, this.rawUpdates1)
  387. this.RedisManager._mocks.parseDocUpdates = sinon.stub()
  388. this.RedisManager._mocks.parseDocUpdates
  389. .onCall(0)
  390. .yields(null, this.expandedUpdates0)
  391. this.RedisManager._mocks.parseDocUpdates
  392. .onCall(1)
  393. .yields(null, this.expandedUpdates1)
  394. return this.RedisManager.getUpdatesInBatches(
  395. this.project_id,
  396. 2,
  397. this.runner,
  398. (error, isProjectHistoryEnabled) => {
  399. this.callback(error, isProjectHistoryEnabled)
  400. return done()
  401. }
  402. )
  403. })
  404. it('requests a second batch of updates', function () {
  405. return this.RedisManager._mocks.getOldestDocUpdates.callCount.should.equal(
  406. 2
  407. )
  408. })
  409. it('calls the runner twice', function () {
  410. return this.runner.callCount.should.equal(2)
  411. })
  412. it('calls the runner with the first updates', function () {
  413. return this.runner
  414. .calledWith([this.expandedUpdates0[0]])
  415. .should.equal(true)
  416. })
  417. it('deletes the first set of applied updates', function () {
  418. return this.RedisManager._mocks.deleteAppliedDocUpdates
  419. .calledWith(
  420. this.project_id,
  421. sinon.match.array.deepEquals([this.rawUpdates0[0]])
  422. )
  423. .should.equal(true)
  424. })
  425. it('calls the runner with the second updates', function () {
  426. return this.runner.calledWith(this.expandedUpdates1).should.equal(true)
  427. })
  428. it('deletes the second set of applied updates', function () {
  429. return this.RedisManager._mocks.deleteAppliedDocUpdates
  430. .calledWith(
  431. this.project_id,
  432. sinon.match.array.deepEquals([this.rawUpdates0[1]])
  433. )
  434. .should.equal(true)
  435. })
  436. return it('calls the callback with the result of the runner', function () {
  437. return this.callback
  438. .calledWith(null, this.isProjectHistoryEnabled)
  439. .should.equal(true)
  440. })
  441. })
  442. describe('single batch exceeding doc content count', function () {
  443. beforeEach(function (done) {
  444. this.rawUpdates0 = [
  445. '{resyncDocContent: 123}',
  446. '{resyncDocContent: 456}',
  447. ]
  448. this.rawUpdates1 = ['{resyncDocContent: 456}']
  449. this.expandedUpdates0 = [
  450. { resyncDocContent: 123 },
  451. { resyncDocContent: 456 },
  452. ]
  453. this.expandedUpdates1 = [{ resyncDocContent: 456 }]
  454. // set the threshold below the size of the first update
  455. this.RedisManager.setMaxNewDocContentCount(
  456. this.expandedUpdates0.length - 1
  457. )
  458. this.RedisManager._mocks.getOldestDocUpdates = sinon.stub()
  459. this.RedisManager._mocks.getOldestDocUpdates
  460. .onCall(0)
  461. .yields(null, this.rawUpdates0)
  462. this.RedisManager._mocks.getOldestDocUpdates
  463. .onCall(1)
  464. .yields(null, this.rawUpdates1)
  465. this.RedisManager._mocks.parseDocUpdates = sinon.stub()
  466. this.RedisManager._mocks.parseDocUpdates
  467. .onCall(0)
  468. .yields(null, this.expandedUpdates0)
  469. this.RedisManager._mocks.parseDocUpdates
  470. .onCall(1)
  471. .yields(null, this.expandedUpdates1)
  472. return this.RedisManager.getUpdatesInBatches(
  473. this.project_id,
  474. 2,
  475. this.runner,
  476. (error, isProjectHistoryEnabled) => {
  477. this.callback(error, isProjectHistoryEnabled)
  478. return done()
  479. }
  480. )
  481. })
  482. it('requests a second batch of updates', function () {
  483. return this.RedisManager._mocks.getOldestDocUpdates.callCount.should.equal(
  484. 2
  485. )
  486. })
  487. it('calls the runner twice', function () {
  488. return this.runner.callCount.should.equal(2)
  489. })
  490. it('calls the runner with the first updates', function () {
  491. return this.runner
  492. .calledWith([this.expandedUpdates0[0]])
  493. .should.equal(true)
  494. })
  495. it('deletes the first set of applied updates', function () {
  496. return this.RedisManager._mocks.deleteAppliedDocUpdates
  497. .calledWith(
  498. this.project_id,
  499. sinon.match.array.deepEquals([this.rawUpdates0[0]])
  500. )
  501. .should.equal(true)
  502. })
  503. it('calls the runner with the second updates', function () {
  504. return this.runner.calledWith(this.expandedUpdates1).should.equal(true)
  505. })
  506. it('deletes the second set of applied updates', function () {
  507. return this.RedisManager._mocks.deleteAppliedDocUpdates
  508. .calledWith(
  509. this.project_id,
  510. sinon.match.array.deepEquals([this.rawUpdates0[1]])
  511. )
  512. .should.equal(true)
  513. })
  514. return it('calls the callback with the result of the runner', function () {
  515. return this.callback
  516. .calledWith(null, this.isProjectHistoryEnabled)
  517. .should.equal(true)
  518. })
  519. })
  520. describe('two batches with first update below and second update above the size limit on updates', function () {
  521. beforeEach(function (done) {
  522. this.rawUpdates0 = [
  523. "{op: ['a', 'b', 'c', 'd']}",
  524. "{op:['e', 'f', 'g', 'h']}",
  525. ]
  526. this.rawUpdates1 = ["{op:['e', 'f', 'g', 'h']}"]
  527. this.expandedUpdates0 = [
  528. { op: ['a', 'b', 'c', 'd'] },
  529. { op: ['e', 'f', 'g', 'h'] },
  530. ]
  531. this.expandedUpdates1 = [{ op: ['e', 'f', 'g', 'h'] }]
  532. // set the threshold below the size of the first update
  533. this.RedisManager.setMaxUpdateOpLength(
  534. this.expandedUpdates0[0].op.length + 1
  535. )
  536. this.RedisManager._mocks.getOldestDocUpdates = sinon.stub()
  537. this.RedisManager._mocks.getOldestDocUpdates
  538. .onCall(0)
  539. .yields(null, this.rawUpdates0)
  540. this.RedisManager._mocks.getOldestDocUpdates
  541. .onCall(1)
  542. .yields(null, this.rawUpdates1)
  543. this.RedisManager._mocks.parseDocUpdates = sinon.stub()
  544. this.RedisManager._mocks.parseDocUpdates
  545. .onCall(0)
  546. .yields(null, this.expandedUpdates0)
  547. this.RedisManager._mocks.parseDocUpdates
  548. .onCall(1)
  549. .yields(null, this.expandedUpdates1)
  550. return this.RedisManager.getUpdatesInBatches(
  551. this.project_id,
  552. 2,
  553. this.runner,
  554. (error, isProjectHistoryEnabled) => {
  555. this.callback(error, isProjectHistoryEnabled)
  556. return done()
  557. }
  558. )
  559. })
  560. it('requests a second batch of updates', function () {
  561. return this.RedisManager._mocks.getOldestDocUpdates.callCount.should.equal(
  562. 2
  563. )
  564. })
  565. it('calls the runner twice', function () {
  566. return this.runner.callCount.should.equal(2)
  567. })
  568. it('calls the runner with the first updates', function () {
  569. return this.runner
  570. .calledWith([this.expandedUpdates0[0]])
  571. .should.equal(true)
  572. })
  573. it('deletes the first set of applied updates', function () {
  574. return this.RedisManager._mocks.deleteAppliedDocUpdates
  575. .calledWith(
  576. this.project_id,
  577. sinon.match.array.deepEquals([this.rawUpdates0[0]])
  578. )
  579. .should.equal(true)
  580. })
  581. it('calls the runner with the second updates', function () {
  582. return this.runner.calledWith(this.expandedUpdates1).should.equal(true)
  583. })
  584. it('deletes the second set of applied updates', function () {
  585. return this.RedisManager._mocks.deleteAppliedDocUpdates
  586. .calledWith(
  587. this.project_id,
  588. sinon.match.array.deepEquals([this.rawUpdates0[1]])
  589. )
  590. .should.equal(true)
  591. })
  592. return it('calls the callback with the result of the runner', function () {
  593. return this.callback
  594. .calledWith(null, this.isProjectHistoryEnabled)
  595. .should.equal(true)
  596. })
  597. })
  598. describe('two batches', function () {
  599. beforeEach(function (done) {
  600. this.RedisManager._mocks.getOldestDocUpdates = sinon.stub()
  601. this.RedisManager._mocks.getOldestDocUpdates
  602. .onCall(0)
  603. .yields(null, this.rawUpdates)
  604. this.RedisManager._mocks.getOldestDocUpdates
  605. .onCall(1)
  606. .yields(null, ['raw-update-3'])
  607. this.RedisManager._mocks.parseDocUpdates = sinon.stub()
  608. this.RedisManager._mocks.parseDocUpdates
  609. .onCall(0)
  610. .yields(null, this.expandedUpdates)
  611. this.RedisManager._mocks.parseDocUpdates
  612. .onCall(1)
  613. .yields(null, ['expanded-update-3'])
  614. return this.RedisManager.getUpdatesInBatches(
  615. this.project_id,
  616. 2,
  617. this.runner,
  618. (error, isProjectHistoryEnabled) => {
  619. this.callback(error, isProjectHistoryEnabled)
  620. return done()
  621. }
  622. )
  623. })
  624. it('requests a second batch of updates', function () {
  625. return this.RedisManager._mocks.getOldestDocUpdates.callCount.should.equal(
  626. 2
  627. )
  628. })
  629. it('calls the runner twice', function () {
  630. return this.runner.callCount.should.equal(2)
  631. })
  632. it('calls the runner with the updates', function () {
  633. return this.runner.calledWith(this.expandedUpdates).should.equal(true)
  634. })
  635. it('deletes the first set of applied updates', function () {
  636. return this.RedisManager._mocks.deleteAppliedDocUpdates
  637. .calledWith(
  638. this.project_id,
  639. sinon.match.array.deepEquals(this.rawUpdates)
  640. )
  641. .should.equal(true)
  642. })
  643. it('deletes the second set of applied updates', function () {
  644. return this.RedisManager._mocks.deleteAppliedDocUpdates
  645. .calledWith(
  646. this.project_id,
  647. sinon.match.array.deepEquals(['raw-update-3'])
  648. )
  649. .should.equal(true)
  650. })
  651. return it('calls the callback with the result of the runner', function () {
  652. return this.callback
  653. .calledWith(null, this.isProjectHistoryEnabled)
  654. .should.equal(true)
  655. })
  656. })
  657. describe('error when first reading updates', function () {
  658. beforeEach(function (done) {
  659. this.error = new Error('error')
  660. this.RedisManager._mocks.getOldestDocUpdates = sinon
  661. .stub()
  662. .yields(this.error)
  663. return this.RedisManager.getUpdatesInBatches(
  664. this.project_id,
  665. 2,
  666. this.runner,
  667. (error, isProjectHistoryEnabled) => {
  668. this.callback(error, isProjectHistoryEnabled)
  669. return done()
  670. }
  671. )
  672. })
  673. it('does not delete any updates', function () {
  674. return this.RedisManager._mocks.deleteAppliedDocUpdates.called.should.equal(
  675. false
  676. )
  677. })
  678. return it('calls the callback with the error', function () {
  679. return this.callback
  680. .calledWith(this.error, undefined)
  681. .should.equal(true)
  682. })
  683. })
  684. return describe('error when reading updates for a second batch', function () {
  685. beforeEach(function (done) {
  686. this.error = new Error('error')
  687. this.RedisManager._mocks.getOldestDocUpdates = sinon.stub()
  688. this.RedisManager._mocks.getOldestDocUpdates
  689. .onCall(0)
  690. .yields(null, this.rawUpdates)
  691. this.RedisManager._mocks.getOldestDocUpdates
  692. .onCall(1)
  693. .yields(this.error)
  694. this.RedisManager._mocks.parseDocUpdates = sinon.stub()
  695. this.RedisManager._mocks.parseDocUpdates
  696. .onCall(0)
  697. .yields(null, this.expandedUpdates)
  698. return this.RedisManager.getUpdatesInBatches(
  699. this.project_id,
  700. 2,
  701. this.runner,
  702. (error, isProjectHistoryEnabled) => {
  703. this.callback(error, isProjectHistoryEnabled)
  704. return done()
  705. }
  706. )
  707. })
  708. it('deletes the first set of applied updates', function () {
  709. return this.RedisManager._mocks.deleteAppliedDocUpdates
  710. .calledWith(
  711. this.project_id,
  712. sinon.match.array.deepEquals(this.rawUpdates)
  713. )
  714. .should.equal(true)
  715. })
  716. it('deletes applied updates only once', function () {
  717. return this.RedisManager._mocks.deleteAppliedDocUpdates.callCount.should.equal(
  718. 1
  719. )
  720. })
  721. return it('calls the callback with the error and the first result of the runner', function () {
  722. return this.callback
  723. .calledWith(this.error, this.isProjectHistoryEnabled)
  724. .should.equal(true)
  725. })
  726. })
  727. })
  728. })