ApplyingUpdates.coffee 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/UpdateManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. describe "UpdateManager", ->
  7. beforeEach ->
  8. @project_id = "project-id-123"
  9. @doc_id = "document-id-123"
  10. @callback = sinon.stub()
  11. @UpdateManager = SandboxedModule.require modulePath, requires:
  12. "./LockManager" : @LockManager = {}
  13. "./RedisManager" : @RedisManager = {}
  14. "./ShareJsUpdateManager" : @ShareJsUpdateManager = {}
  15. "logger-sharelatex": @logger = { log: sinon.stub() }
  16. "./Metrics": @Metrics =
  17. Timer: class Timer
  18. done: sinon.stub()
  19. describe "resumeProcessing", ->
  20. beforeEach (done) ->
  21. @docs = [{
  22. doc_id: "doc-1"
  23. project_id: "project-1"
  24. }, {
  25. doc_id: "doc-2"
  26. project_id: "project-2"
  27. }, {
  28. doc_id: "doc-3"
  29. project_id: "project-3"
  30. }]
  31. @RedisManager.getDocsWithPendingUpdates = sinon.stub().callsArgWith(0, null, @docs)
  32. @UpdateManager.processOutstandingUpdatesWithLock = sinon.stub().callsArg(2)
  33. @UpdateManager.resumeProcessing(done)
  34. it "should the docs that haven't been processed yet", ->
  35. @RedisManager.getDocsWithPendingUpdates
  36. .called.should.equal true
  37. it "should call processOutstandingUpdatesWithLock for each doc", ->
  38. for doc in @docs
  39. @UpdateManager.processOutstandingUpdatesWithLock
  40. .calledWith(doc.project_id, doc.doc_id)
  41. .should.equal true
  42. describe "processOutstandingUpdates", ->
  43. beforeEach ->
  44. @UpdateManager.fetchAndApplyUpdates = sinon.stub().callsArg(2)
  45. @RedisManager.clearDocFromPendingUpdatesSet = sinon.stub().callsArg(2)
  46. @UpdateManager.processOutstandingUpdates @project_id, @doc_id, @callback
  47. it "should apply the updates", ->
  48. @UpdateManager.fetchAndApplyUpdates.calledWith(@project_id, @doc_id).should.equal true
  49. it "should clear the doc from the process pending set", ->
  50. @RedisManager.clearDocFromPendingUpdatesSet
  51. .calledWith(@project_id, @doc_id)
  52. .should.equal true
  53. it "should call the callback", ->
  54. @callback.called.should.equal true
  55. it "should time the execution", ->
  56. @Metrics.Timer::done.called.should.equal true
  57. describe "processOutstandingUpdatesWithLock", ->
  58. describe "when the lock is free", ->
  59. beforeEach ->
  60. @LockManager.tryLock = sinon.stub().callsArgWith(1, null, true)
  61. @LockManager.releaseLock = sinon.stub().callsArg(1)
  62. @UpdateManager.continueProcessingUpdatesWithLock = sinon.stub().callsArg(2)
  63. @UpdateManager.processOutstandingUpdates = sinon.stub().callsArg(2)
  64. describe "successfully", ->
  65. beforeEach ->
  66. @UpdateManager.processOutstandingUpdatesWithLock @project_id, @doc_id, @callback
  67. it "should acquire the lock", ->
  68. @LockManager.tryLock.calledWith(@doc_id).should.equal true
  69. it "should free the lock", ->
  70. @LockManager.releaseLock.calledWith(@doc_id).should.equal true
  71. it "should process the outstanding updates", ->
  72. @UpdateManager.processOutstandingUpdates.calledWith(@project_id, @doc_id).should.equal true
  73. it "should do everything with the lock acquired", ->
  74. @UpdateManager.processOutstandingUpdates.calledAfter(@LockManager.tryLock).should.equal true
  75. @UpdateManager.processOutstandingUpdates.calledBefore(@LockManager.releaseLock).should.equal true
  76. it "should continue processing new updates that may have come in", ->
  77. @UpdateManager.continueProcessingUpdatesWithLock.calledWith(@project_id, @doc_id).should.equal true
  78. it "should return the callback", ->
  79. @callback.called.should.equal true
  80. describe "when processOutstandingUpdates returns an error", ->
  81. beforeEach ->
  82. @UpdateManager.processOutstandingUpdates = sinon.stub().callsArgWith(2, @error = new Error("Something went wrong"))
  83. @UpdateManager.processOutstandingUpdatesWithLock @project_id, @doc_id, @callback
  84. it "should free the lock", ->
  85. @LockManager.releaseLock.calledWith(@doc_id).should.equal true
  86. it "should return the error in the callback", ->
  87. @callback.calledWith(@error).should.equal true
  88. describe "when the lock is taken", ->
  89. beforeEach ->
  90. @LockManager.tryLock = sinon.stub().callsArgWith(1, null, false)
  91. @UpdateManager.processOutstandingUpdates = sinon.stub().callsArg(2)
  92. @UpdateManager.processOutstandingUpdatesWithLock @project_id, @doc_id, @callback
  93. it "should return the callback", ->
  94. @callback.called.should.equal true
  95. it "should not process the updates", ->
  96. @UpdateManager.processOutstandingUpdates.called.should.equal false
  97. describe "continueProcessingUpdatesWithLock", ->
  98. describe "when there are outstanding updates", ->
  99. beforeEach ->
  100. @RedisManager.getUpdatesLength = sinon.stub().callsArgWith(1, null, 3)
  101. @UpdateManager.processOutstandingUpdatesWithLock = sinon.stub().callsArg(2)
  102. @UpdateManager.continueProcessingUpdatesWithLock @project_id, @doc_id, @callback
  103. it "should process the outstanding updates", ->
  104. @UpdateManager.processOutstandingUpdatesWithLock.calledWith(@project_id, @doc_id).should.equal true
  105. it "should return the callback", ->
  106. @callback.called.should.equal true
  107. describe "when there are no outstanding updates", ->
  108. beforeEach ->
  109. @RedisManager.getUpdatesLength = sinon.stub().callsArgWith(1, null, 0)
  110. @UpdateManager.processOutstandingUpdatesWithLock = sinon.stub().callsArg(2)
  111. @UpdateManager.continueProcessingUpdatesWithLock @project_id, @doc_id, @callback
  112. it "should not try to process the outstanding updates", ->
  113. @UpdateManager.processOutstandingUpdatesWithLock.called.should.equal false
  114. it "should return the callback", ->
  115. @callback.called.should.equal true
  116. describe "fetchAndApplyUpdates", ->
  117. describe "with updates", ->
  118. beforeEach ->
  119. @updates = [{p: 1, t: "foo"}]
  120. @updatedDocLines = ["updated", "lines"]
  121. @version = 34
  122. @RedisManager.getPendingUpdatesForDoc = sinon.stub().callsArgWith(1, null, @updates)
  123. @UpdateManager.applyUpdates = sinon.stub().callsArgWith(3, null, @updatedDocLines, @version)
  124. @UpdateManager.fetchAndApplyUpdates @project_id, @doc_id, @callback
  125. it "should get the pending updates", ->
  126. @RedisManager.getPendingUpdatesForDoc.calledWith(@doc_id).should.equal true
  127. it "should apply the updates", ->
  128. @UpdateManager.applyUpdates
  129. .calledWith(@project_id, @doc_id, @updates)
  130. .should.equal true
  131. it "should call the callback", ->
  132. @callback.called.should.equal true
  133. describe "when there are no updates", ->
  134. beforeEach ->
  135. @updates = []
  136. @RedisManager.getPendingUpdatesForDoc = sinon.stub().callsArgWith(1, null, @updates)
  137. @UpdateManager.applyUpdates = sinon.stub()
  138. @RedisManager.setDocument = sinon.stub()
  139. @UpdateManager.fetchAndApplyUpdates @project_id, @doc_id, @callback
  140. it "should not call applyUpdates", ->
  141. @UpdateManager.applyUpdates.called.should.equal false
  142. it "should call the callback", ->
  143. @callback.called.should.equal true
  144. describe "applyUpdates", ->
  145. beforeEach ->
  146. @updates = [{op: [{p: 42, i: "foo"}]}]
  147. @updatedDocLines = ["updated", "lines"]
  148. @version = 34
  149. @ShareJsUpdateManager.applyUpdates = sinon.stub().callsArgWith(3, null, @updatedDocLines, @version)
  150. @RedisManager.setDocument = sinon.stub().callsArg(3)
  151. describe "normally", ->
  152. beforeEach ->
  153. @UpdateManager.applyUpdates @project_id, @doc_id, @updates, @callback
  154. it "should apply the updates via ShareJS", ->
  155. @ShareJsUpdateManager.applyUpdates
  156. .calledWith(@project_id, @doc_id, @updates)
  157. .should.equal true
  158. it "should save the document", ->
  159. @RedisManager.setDocument
  160. .calledWith(@doc_id, @updatedDocLines, @version)
  161. .should.equal true
  162. it "should call the callback", ->
  163. @callback.called.should.equal true
  164. describe "with UTF-16 surrogate pairs in the update", ->
  165. beforeEach ->
  166. @updates = [{op: [{p: 42, i: "\uD835\uDC00"}]}]
  167. @UpdateManager.applyUpdates @project_id, @doc_id, @updates, @callback
  168. it "should apply the update but with surrogate pairs removed", ->
  169. @ShareJsUpdateManager.applyUpdates
  170. .calledWith(@project_id, @doc_id, @updates)
  171. .should.equal true
  172. # \uFFFD is 'replacement character'
  173. @updates[0].op[0].i.should.equal "\uFFFD\uFFFD"