UpdateManagerTests.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. // @ts-check
  2. const sinon = require('sinon')
  3. const { expect } = require('chai')
  4. const SandboxedModule = require('sandboxed-module')
  5. const MODULE_PATH = '../../../../app/js/UpdateManager.js'
  6. describe('UpdateManager', function () {
  7. beforeEach(function () {
  8. this.project_id = 'project-id-123'
  9. this.projectHistoryId = 'history-id-123'
  10. this.doc_id = 'document-id-123'
  11. this.lockValue = 'mock-lock-value'
  12. this.Metrics = {
  13. inc: sinon.stub(),
  14. Timer: class Timer {},
  15. }
  16. this.Metrics.Timer.prototype.done = sinon.stub()
  17. this.Profiler = class Profiler {}
  18. this.Profiler.prototype.log = sinon.stub().returns({ end: sinon.stub() })
  19. this.Profiler.prototype.end = sinon.stub()
  20. this.LockManager = {
  21. promises: {
  22. tryLock: sinon.stub().resolves(this.lockValue),
  23. getLock: sinon.stub().resolves(this.lockValue),
  24. releaseLock: sinon.stub().resolves(),
  25. },
  26. }
  27. this.RedisManager = {
  28. promises: {
  29. setDocument: sinon.stub().resolves(),
  30. updateDocument: sinon.stub(),
  31. },
  32. }
  33. this.RealTimeRedisManager = {
  34. sendData: sinon.stub(),
  35. promises: {
  36. getUpdatesLength: sinon.stub(),
  37. getPendingUpdatesForDoc: sinon.stub(),
  38. },
  39. }
  40. this.ShareJsUpdateManager = {
  41. promises: {
  42. applyUpdate: sinon.stub(),
  43. },
  44. }
  45. this.HistoryManager = {
  46. recordAndFlushHistoryOps: sinon.stub(),
  47. }
  48. this.Settings = {}
  49. this.DocumentManager = {
  50. promises: {
  51. getDoc: sinon.stub(),
  52. },
  53. }
  54. this.RangesManager = {
  55. applyUpdate: sinon.stub(),
  56. }
  57. this.SnapshotManager = {
  58. promises: {
  59. recordSnapshot: sinon.stub().resolves(),
  60. },
  61. }
  62. this.ProjectHistoryRedisManager = {
  63. promises: {
  64. queueOps: sinon
  65. .stub()
  66. .callsFake(async (projectId, ...ops) => ops.length),
  67. },
  68. }
  69. this.UpdateManager = SandboxedModule.require(MODULE_PATH, {
  70. requires: {
  71. './LockManager': this.LockManager,
  72. './RedisManager': this.RedisManager,
  73. './RealTimeRedisManager': this.RealTimeRedisManager,
  74. './ShareJsUpdateManager': this.ShareJsUpdateManager,
  75. './HistoryManager': this.HistoryManager,
  76. './Metrics': this.Metrics,
  77. '@overleaf/settings': this.Settings,
  78. './DocumentManager': this.DocumentManager,
  79. './RangesManager': this.RangesManager,
  80. './SnapshotManager': this.SnapshotManager,
  81. './Profiler': this.Profiler,
  82. './ProjectHistoryRedisManager': this.ProjectHistoryRedisManager,
  83. },
  84. })
  85. })
  86. describe('processOutstandingUpdates', function () {
  87. beforeEach(async function () {
  88. this.UpdateManager.promises.fetchAndApplyUpdates = sinon.stub().resolves()
  89. await this.UpdateManager.promises.processOutstandingUpdates(
  90. this.project_id,
  91. this.doc_id
  92. )
  93. })
  94. it('should apply the updates', function () {
  95. this.UpdateManager.promises.fetchAndApplyUpdates
  96. .calledWith(this.project_id, this.doc_id)
  97. .should.equal(true)
  98. })
  99. it('should time the execution', function () {
  100. this.Metrics.Timer.prototype.done.called.should.equal(true)
  101. })
  102. })
  103. describe('processOutstandingUpdatesWithLock', function () {
  104. describe('when the lock is free', function () {
  105. beforeEach(function () {
  106. this.UpdateManager.promises.continueProcessingUpdatesWithLock = sinon
  107. .stub()
  108. .resolves()
  109. this.UpdateManager.promises.processOutstandingUpdates = sinon
  110. .stub()
  111. .resolves()
  112. })
  113. describe('successfully', function () {
  114. beforeEach(async function () {
  115. await this.UpdateManager.promises.processOutstandingUpdatesWithLock(
  116. this.project_id,
  117. this.doc_id
  118. )
  119. })
  120. it('should acquire the lock', function () {
  121. this.LockManager.promises.tryLock
  122. .calledWith(this.doc_id)
  123. .should.equal(true)
  124. })
  125. it('should free the lock', function () {
  126. this.LockManager.promises.releaseLock
  127. .calledWith(this.doc_id, this.lockValue)
  128. .should.equal(true)
  129. })
  130. it('should process the outstanding updates', function () {
  131. this.UpdateManager.promises.processOutstandingUpdates
  132. .calledWith(this.project_id, this.doc_id)
  133. .should.equal(true)
  134. })
  135. it('should do everything with the lock acquired', function () {
  136. this.UpdateManager.promises.processOutstandingUpdates
  137. .calledAfter(this.LockManager.promises.tryLock)
  138. .should.equal(true)
  139. this.UpdateManager.promises.processOutstandingUpdates
  140. .calledBefore(this.LockManager.promises.releaseLock)
  141. .should.equal(true)
  142. })
  143. it('should continue processing new updates that may have come in', function () {
  144. this.UpdateManager.promises.continueProcessingUpdatesWithLock
  145. .calledWith(this.project_id, this.doc_id)
  146. .should.equal(true)
  147. })
  148. })
  149. describe('when processOutstandingUpdates returns an error', function () {
  150. beforeEach(async function () {
  151. this.error = new Error('Something went wrong')
  152. this.UpdateManager.promises.processOutstandingUpdates = sinon
  153. .stub()
  154. .rejects(this.error)
  155. await expect(
  156. this.UpdateManager.promises.processOutstandingUpdatesWithLock(
  157. this.project_id,
  158. this.doc_id
  159. )
  160. ).to.be.rejectedWith(this.error)
  161. })
  162. it('should free the lock', function () {
  163. this.LockManager.promises.releaseLock
  164. .calledWith(this.doc_id, this.lockValue)
  165. .should.equal(true)
  166. })
  167. })
  168. })
  169. describe('when the lock is taken', function () {
  170. beforeEach(async function () {
  171. this.LockManager.promises.tryLock.resolves(null)
  172. this.UpdateManager.promises.processOutstandingUpdates = sinon
  173. .stub()
  174. .resolves()
  175. await this.UpdateManager.promises.processOutstandingUpdatesWithLock(
  176. this.project_id,
  177. this.doc_id
  178. )
  179. })
  180. it('should not process the updates', function () {
  181. this.UpdateManager.promises.processOutstandingUpdates.called.should.equal(
  182. false
  183. )
  184. })
  185. })
  186. })
  187. describe('continueProcessingUpdatesWithLock', function () {
  188. describe('when there are outstanding updates', function () {
  189. beforeEach(async function () {
  190. this.RealTimeRedisManager.promises.getUpdatesLength.resolves(3)
  191. this.UpdateManager.promises.processOutstandingUpdatesWithLock = sinon
  192. .stub()
  193. .resolves()
  194. await this.UpdateManager.promises.continueProcessingUpdatesWithLock(
  195. this.project_id,
  196. this.doc_id
  197. )
  198. })
  199. it('should process the outstanding updates', function () {
  200. this.UpdateManager.promises.processOutstandingUpdatesWithLock
  201. .calledWith(this.project_id, this.doc_id)
  202. .should.equal(true)
  203. })
  204. })
  205. describe('when there are no outstanding updates', function () {
  206. beforeEach(async function () {
  207. this.RealTimeRedisManager.promises.getUpdatesLength.resolves(0)
  208. this.UpdateManager.promises.processOutstandingUpdatesWithLock = sinon
  209. .stub()
  210. .resolves()
  211. await this.UpdateManager.promises.continueProcessingUpdatesWithLock(
  212. this.project_id,
  213. this.doc_id
  214. )
  215. })
  216. it('should not try to process the outstanding updates', function () {
  217. this.UpdateManager.promises.processOutstandingUpdatesWithLock.called.should.equal(
  218. false
  219. )
  220. })
  221. })
  222. })
  223. describe('fetchAndApplyUpdates', function () {
  224. describe('with updates', function () {
  225. beforeEach(async function () {
  226. this.updates = [{ p: 1, t: 'foo' }]
  227. this.updatedDocLines = ['updated', 'lines']
  228. this.version = 34
  229. this.RealTimeRedisManager.promises.getPendingUpdatesForDoc.resolves(
  230. this.updates
  231. )
  232. this.UpdateManager.promises.applyUpdate = sinon.stub().resolves()
  233. await this.UpdateManager.promises.fetchAndApplyUpdates(
  234. this.project_id,
  235. this.doc_id
  236. )
  237. })
  238. it('should get the pending updates', function () {
  239. this.RealTimeRedisManager.promises.getPendingUpdatesForDoc
  240. .calledWith(this.doc_id)
  241. .should.equal(true)
  242. })
  243. it('should apply the updates', function () {
  244. this.updates.map(update =>
  245. this.UpdateManager.promises.applyUpdate
  246. .calledWith(this.project_id, this.doc_id, update)
  247. .should.equal(true)
  248. )
  249. })
  250. })
  251. describe('when there are no updates', function () {
  252. beforeEach(async function () {
  253. this.updates = []
  254. this.RealTimeRedisManager.promises.getPendingUpdatesForDoc.resolves(
  255. this.updates
  256. )
  257. this.UpdateManager.promises.applyUpdate = sinon.stub().resolves()
  258. await this.UpdateManager.promises.fetchAndApplyUpdates(
  259. this.project_id,
  260. this.doc_id
  261. )
  262. })
  263. it('should not call applyUpdate', function () {
  264. this.UpdateManager.promises.applyUpdate.called.should.equal(false)
  265. })
  266. })
  267. })
  268. describe('applyUpdate', function () {
  269. beforeEach(function () {
  270. this.updateMeta = { user_id: 'last-author-fake-id' }
  271. this.update = { op: [{ p: 42, i: 'foo' }], meta: this.updateMeta }
  272. this.updatedDocLines = ['updated', 'lines']
  273. this.version = 34
  274. this.lines = ['original', 'lines']
  275. this.ranges = { entries: 'mock', comments: 'mock' }
  276. this.updated_ranges = { entries: 'updated', comments: 'updated' }
  277. this.appliedOps = [
  278. { v: 42, op: 'mock-op-42' },
  279. { v: 45, op: 'mock-op-45' },
  280. ]
  281. this.historyUpdates = [
  282. 'history-update-1',
  283. 'history-update-2',
  284. 'history-update-3',
  285. ]
  286. this.project_ops_length = 123
  287. this.pathname = '/a/b/c.tex'
  288. this.DocumentManager.promises.getDoc.resolves({
  289. lines: this.lines,
  290. version: this.version,
  291. ranges: this.ranges,
  292. pathname: this.pathname,
  293. projectHistoryId: this.projectHistoryId,
  294. historyRangesSupport: false,
  295. })
  296. this.RangesManager.applyUpdate.returns({
  297. newRanges: this.updated_ranges,
  298. rangesWereCollapsed: false,
  299. historyUpdates: this.historyUpdates,
  300. })
  301. this.ShareJsUpdateManager.promises.applyUpdate = sinon.stub().resolves({
  302. updatedDocLines: this.updatedDocLines,
  303. version: this.version,
  304. appliedOps: this.appliedOps,
  305. })
  306. this.RedisManager.promises.updateDocument.resolves()
  307. this.UpdateManager.promises._addProjectHistoryMetadataToOps = sinon.stub()
  308. })
  309. describe('normally', function () {
  310. beforeEach(async function () {
  311. await this.UpdateManager.promises.applyUpdate(
  312. this.project_id,
  313. this.doc_id,
  314. this.update
  315. )
  316. })
  317. it('should apply the updates via ShareJS', function () {
  318. this.ShareJsUpdateManager.promises.applyUpdate
  319. .calledWith(
  320. this.project_id,
  321. this.doc_id,
  322. this.update,
  323. this.lines,
  324. this.version
  325. )
  326. .should.equal(true)
  327. })
  328. it('should update the ranges', function () {
  329. this.RangesManager.applyUpdate
  330. .calledWith(
  331. this.project_id,
  332. this.doc_id,
  333. this.ranges,
  334. this.appliedOps,
  335. this.updatedDocLines
  336. )
  337. .should.equal(true)
  338. })
  339. it('should save the document', function () {
  340. this.RedisManager.promises.updateDocument
  341. .calledWith(
  342. this.project_id,
  343. this.doc_id,
  344. this.updatedDocLines,
  345. this.version,
  346. this.appliedOps,
  347. this.updated_ranges,
  348. this.updateMeta
  349. )
  350. .should.equal(true)
  351. })
  352. it('should add metadata to the ops', function () {
  353. this.UpdateManager.promises._addProjectHistoryMetadataToOps.should.have.been.calledWith(
  354. this.appliedOps,
  355. this.pathname,
  356. this.projectHistoryId,
  357. this.lines
  358. )
  359. })
  360. it('should push the applied ops into the history queue', function () {
  361. this.ProjectHistoryRedisManager.promises.queueOps.should.have.been.calledWith(
  362. this.project_id,
  363. ...this.appliedOps.map(op => JSON.stringify(op))
  364. )
  365. this.HistoryManager.recordAndFlushHistoryOps.should.have.been.calledWith(
  366. this.project_id,
  367. this.appliedOps,
  368. this.appliedOps.length
  369. )
  370. })
  371. })
  372. describe('with UTF-16 surrogate pairs in the update', function () {
  373. beforeEach(async function () {
  374. this.update = { op: [{ p: 42, i: '\uD835\uDC00' }] }
  375. await this.UpdateManager.promises.applyUpdate(
  376. this.project_id,
  377. this.doc_id,
  378. this.update
  379. )
  380. })
  381. it('should apply the update but with surrogate pairs removed', function () {
  382. this.ShareJsUpdateManager.promises.applyUpdate
  383. .calledWith(this.project_id, this.doc_id, this.update)
  384. .should.equal(true)
  385. // \uFFFD is 'replacement character'
  386. this.update.op[0].i.should.equal('\uFFFD\uFFFD')
  387. })
  388. })
  389. describe('with an error', function () {
  390. beforeEach(async function () {
  391. this.error = new Error('something went wrong')
  392. this.ShareJsUpdateManager.promises.applyUpdate.rejects(this.error)
  393. await expect(
  394. this.UpdateManager.promises.applyUpdate(
  395. this.project_id,
  396. this.doc_id,
  397. this.update
  398. )
  399. ).to.be.rejectedWith(this.error)
  400. })
  401. it('should call RealTimeRedisManager.sendData with the error', function () {
  402. this.RealTimeRedisManager.sendData
  403. .calledWith({
  404. project_id: this.project_id,
  405. doc_id: this.doc_id,
  406. error: this.error.message,
  407. })
  408. .should.equal(true)
  409. })
  410. })
  411. describe('when ranges get collapsed', function () {
  412. beforeEach(async function () {
  413. this.RangesManager.applyUpdate.returns({
  414. newRanges: this.updated_ranges,
  415. rangesWereCollapsed: true,
  416. historyUpdates: this.historyUpdates,
  417. })
  418. await this.UpdateManager.promises.applyUpdate(
  419. this.project_id,
  420. this.doc_id,
  421. this.update
  422. )
  423. })
  424. it('should increment the doc-snapshot metric', function () {
  425. this.Metrics.inc.calledWith('doc-snapshot').should.equal(true)
  426. })
  427. it('should call SnapshotManager.recordSnapshot', function () {
  428. this.SnapshotManager.promises.recordSnapshot
  429. .calledWith(
  430. this.project_id,
  431. this.doc_id,
  432. this.version,
  433. this.pathname,
  434. this.lines,
  435. this.ranges
  436. )
  437. .should.equal(true)
  438. })
  439. })
  440. describe('when history ranges are supported', function () {
  441. beforeEach(async function () {
  442. this.DocumentManager.promises.getDoc.resolves({
  443. lines: this.lines,
  444. version: this.version,
  445. ranges: this.ranges,
  446. pathname: this.pathname,
  447. projectHistoryId: this.projectHistoryId,
  448. historyRangesSupport: true,
  449. })
  450. await this.UpdateManager.promises.applyUpdate(
  451. this.project_id,
  452. this.doc_id,
  453. this.update
  454. )
  455. })
  456. it('should push the history updates into the history queue', function () {
  457. this.ProjectHistoryRedisManager.promises.queueOps.should.have.been.calledWith(
  458. this.project_id,
  459. ...this.historyUpdates.map(op => JSON.stringify(op))
  460. )
  461. this.HistoryManager.recordAndFlushHistoryOps.should.have.been.calledWith(
  462. this.project_id,
  463. this.historyUpdates,
  464. this.historyUpdates.length
  465. )
  466. })
  467. })
  468. })
  469. describe('_addProjectHistoryMetadataToOps', function () {
  470. it('should add projectHistoryId, pathname and doc_length metadata to the ops', function () {
  471. const lines = ['some', 'test', 'data']
  472. const appliedOps = [
  473. {
  474. v: 42,
  475. op: [
  476. { i: 'foo', p: 4 },
  477. { i: 'bar', p: 6 },
  478. ],
  479. },
  480. {
  481. v: 45,
  482. op: [
  483. { d: 'qux', p: 4 },
  484. { i: 'bazbaz', p: 14 },
  485. ],
  486. },
  487. { v: 49, op: [{ i: 'penguin', p: 18 }] },
  488. ]
  489. this.UpdateManager._addProjectHistoryMetadataToOps(
  490. appliedOps,
  491. this.pathname,
  492. this.projectHistoryId,
  493. lines
  494. )
  495. appliedOps.should.deep.equal([
  496. {
  497. projectHistoryId: this.projectHistoryId,
  498. v: 42,
  499. op: [
  500. { i: 'foo', p: 4 },
  501. { i: 'bar', p: 6 },
  502. ],
  503. meta: {
  504. pathname: this.pathname,
  505. doc_length: 14,
  506. },
  507. },
  508. {
  509. projectHistoryId: this.projectHistoryId,
  510. v: 45,
  511. op: [
  512. { d: 'qux', p: 4 },
  513. { i: 'bazbaz', p: 14 },
  514. ],
  515. meta: {
  516. pathname: this.pathname,
  517. doc_length: 20,
  518. }, // 14 + 'foo' + 'bar'
  519. },
  520. {
  521. projectHistoryId: this.projectHistoryId,
  522. v: 49,
  523. op: [{ i: 'penguin', p: 18 }],
  524. meta: {
  525. pathname: this.pathname,
  526. doc_length: 23,
  527. }, // 14 - 'qux' + 'bazbaz'
  528. },
  529. ])
  530. })
  531. })
  532. describe('lockUpdatesAndDo', function () {
  533. beforeEach(function () {
  534. this.method = sinon
  535. .stub()
  536. .yields(null, this.response_arg1, this.response_arg2)
  537. this.arg1 = 'argument 1'
  538. this.response_arg1 = 'response argument 1'
  539. this.response_arg2 = 'response argument 2'
  540. })
  541. describe('successfully', function () {
  542. beforeEach(async function () {
  543. this.UpdateManager.promises.continueProcessingUpdatesWithLock = sinon
  544. .stub()
  545. .resolves()
  546. this.UpdateManager.promises.processOutstandingUpdates = sinon
  547. .stub()
  548. .resolves()
  549. this.response = await this.UpdateManager.promises.lockUpdatesAndDo(
  550. this.method,
  551. this.project_id,
  552. this.doc_id,
  553. this.arg1
  554. )
  555. })
  556. it('should lock the doc', function () {
  557. this.LockManager.promises.getLock
  558. .calledWith(this.doc_id)
  559. .should.equal(true)
  560. })
  561. it('should process any outstanding updates', function () {
  562. this.UpdateManager.promises.processOutstandingUpdates.should.have.been.calledWith(
  563. this.project_id,
  564. this.doc_id
  565. )
  566. })
  567. it('should call the method', function () {
  568. this.method
  569. .calledWith(this.project_id, this.doc_id, this.arg1)
  570. .should.equal(true)
  571. })
  572. it('should return the method response arguments', function () {
  573. expect(this.response).to.deep.equal([
  574. this.response_arg1,
  575. this.response_arg2,
  576. ])
  577. })
  578. it('should release the lock', function () {
  579. this.LockManager.promises.releaseLock
  580. .calledWith(this.doc_id, this.lockValue)
  581. .should.equal(true)
  582. })
  583. it('should continue processing updates', function () {
  584. this.UpdateManager.promises.continueProcessingUpdatesWithLock
  585. .calledWith(this.project_id, this.doc_id)
  586. .should.equal(true)
  587. })
  588. })
  589. describe('when processOutstandingUpdates returns an error', function () {
  590. beforeEach(async function () {
  591. this.error = new Error('Something went wrong')
  592. this.UpdateManager.promises.processOutstandingUpdates = sinon
  593. .stub()
  594. .rejects(this.error)
  595. await expect(
  596. this.UpdateManager.promises.lockUpdatesAndDo(
  597. this.method,
  598. this.project_id,
  599. this.doc_id,
  600. this.arg1
  601. )
  602. ).to.be.rejectedWith(this.error)
  603. })
  604. it('should free the lock', function () {
  605. this.LockManager.promises.releaseLock
  606. .calledWith(this.doc_id, this.lockValue)
  607. .should.equal(true)
  608. })
  609. })
  610. describe('when the method returns an error', function () {
  611. beforeEach(async function () {
  612. this.error = new Error('something went wrong')
  613. this.UpdateManager.promises.processOutstandingUpdates = sinon
  614. .stub()
  615. .resolves()
  616. this.method = sinon.stub().yields(this.error)
  617. await expect(
  618. this.UpdateManager.promises.lockUpdatesAndDo(
  619. this.method,
  620. this.project_id,
  621. this.doc_id,
  622. this.arg1
  623. )
  624. ).to.be.rejectedWith(this.error)
  625. })
  626. it('should free the lock', function () {
  627. this.LockManager.promises.releaseLock
  628. .calledWith(this.doc_id, this.lockValue)
  629. .should.equal(true)
  630. })
  631. })
  632. })
  633. })