UpdateManagerTests.coffee 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. "./WebRedisManager" : @WebRedisManager = {}
  15. "./ShareJsUpdateManager" : @ShareJsUpdateManager = {}
  16. "./HistoryManager" : @HistoryManager = {}
  17. "logger-sharelatex": @logger = { log: sinon.stub() }
  18. "./Metrics": @Metrics =
  19. Timer: class Timer
  20. done: sinon.stub()
  21. "settings-sharelatex": Settings = {}
  22. "./DocumentManager": @DocumentManager = {}
  23. "./TrackChangesManager": @TrackChangesManager = {}
  24. describe "processOutstandingUpdates", ->
  25. beforeEach ->
  26. @UpdateManager.fetchAndApplyUpdates = sinon.stub().callsArg(2)
  27. @UpdateManager.processOutstandingUpdates @project_id, @doc_id, @callback
  28. it "should apply the updates", ->
  29. @UpdateManager.fetchAndApplyUpdates.calledWith(@project_id, @doc_id).should.equal true
  30. it "should call the callback", ->
  31. @callback.called.should.equal true
  32. it "should time the execution", ->
  33. @Metrics.Timer::done.called.should.equal true
  34. describe "processOutstandingUpdatesWithLock", ->
  35. describe "when the lock is free", ->
  36. beforeEach ->
  37. @LockManager.tryLock = sinon.stub().callsArgWith(1, null, true, @lockValue = "mock-lock-value")
  38. @LockManager.releaseLock = sinon.stub().callsArg(2)
  39. @UpdateManager.continueProcessingUpdatesWithLock = sinon.stub().callsArg(2)
  40. @UpdateManager.processOutstandingUpdates = sinon.stub().callsArg(2)
  41. describe "successfully", ->
  42. beforeEach ->
  43. @UpdateManager.processOutstandingUpdatesWithLock @project_id, @doc_id, @callback
  44. it "should acquire the lock", ->
  45. @LockManager.tryLock.calledWith(@doc_id).should.equal true
  46. it "should free the lock", ->
  47. @LockManager.releaseLock.calledWith(@doc_id, @lockValue).should.equal true
  48. it "should process the outstanding updates", ->
  49. @UpdateManager.processOutstandingUpdates.calledWith(@project_id, @doc_id).should.equal true
  50. it "should do everything with the lock acquired", ->
  51. @UpdateManager.processOutstandingUpdates.calledAfter(@LockManager.tryLock).should.equal true
  52. @UpdateManager.processOutstandingUpdates.calledBefore(@LockManager.releaseLock).should.equal true
  53. it "should continue processing new updates that may have come in", ->
  54. @UpdateManager.continueProcessingUpdatesWithLock.calledWith(@project_id, @doc_id).should.equal true
  55. it "should return the callback", ->
  56. @callback.called.should.equal true
  57. describe "when processOutstandingUpdates returns an error", ->
  58. beforeEach ->
  59. @UpdateManager.processOutstandingUpdates = sinon.stub().callsArgWith(2, @error = new Error("Something went wrong"))
  60. @UpdateManager.processOutstandingUpdatesWithLock @project_id, @doc_id, @callback
  61. it "should free the lock", ->
  62. @LockManager.releaseLock.calledWith(@doc_id, @lockValue).should.equal true
  63. it "should return the error in the callback", ->
  64. @callback.calledWith(@error).should.equal true
  65. describe "when the lock is taken", ->
  66. beforeEach ->
  67. @LockManager.tryLock = sinon.stub().callsArgWith(1, null, false)
  68. @UpdateManager.processOutstandingUpdates = sinon.stub().callsArg(2)
  69. @UpdateManager.processOutstandingUpdatesWithLock @project_id, @doc_id, @callback
  70. it "should return the callback", ->
  71. @callback.called.should.equal true
  72. it "should not process the updates", ->
  73. @UpdateManager.processOutstandingUpdates.called.should.equal false
  74. describe "continueProcessingUpdatesWithLock", ->
  75. describe "when there are outstanding updates", ->
  76. beforeEach ->
  77. @WebRedisManager.getUpdatesLength = sinon.stub().callsArgWith(1, null, 3)
  78. @UpdateManager.processOutstandingUpdatesWithLock = sinon.stub().callsArg(2)
  79. @UpdateManager.continueProcessingUpdatesWithLock @project_id, @doc_id, @callback
  80. it "should process the outstanding updates", ->
  81. @UpdateManager.processOutstandingUpdatesWithLock.calledWith(@project_id, @doc_id).should.equal true
  82. it "should return the callback", ->
  83. @callback.called.should.equal true
  84. describe "when there are no outstanding updates", ->
  85. beforeEach ->
  86. @WebRedisManager.getUpdatesLength = sinon.stub().callsArgWith(1, null, 0)
  87. @UpdateManager.processOutstandingUpdatesWithLock = sinon.stub().callsArg(2)
  88. @UpdateManager.continueProcessingUpdatesWithLock @project_id, @doc_id, @callback
  89. it "should not try to process the outstanding updates", ->
  90. @UpdateManager.processOutstandingUpdatesWithLock.called.should.equal false
  91. it "should return the callback", ->
  92. @callback.called.should.equal true
  93. describe "fetchAndApplyUpdates", ->
  94. describe "with updates", ->
  95. beforeEach ->
  96. @updates = [{p: 1, t: "foo"}]
  97. @updatedDocLines = ["updated", "lines"]
  98. @version = 34
  99. @WebRedisManager.getPendingUpdatesForDoc = sinon.stub().callsArgWith(1, null, @updates)
  100. @UpdateManager.applyUpdate = sinon.stub().callsArgWith(3, null, @updatedDocLines, @version)
  101. @UpdateManager.fetchAndApplyUpdates @project_id, @doc_id, @callback
  102. it "should get the pending updates", ->
  103. @WebRedisManager.getPendingUpdatesForDoc.calledWith(@doc_id).should.equal true
  104. it "should apply the updates", ->
  105. for update in @updates
  106. @UpdateManager.applyUpdate
  107. .calledWith(@project_id, @doc_id, update)
  108. .should.equal true
  109. it "should call the callback", ->
  110. @callback.called.should.equal true
  111. describe "when there are no updates", ->
  112. beforeEach ->
  113. @updates = []
  114. @WebRedisManager.getPendingUpdatesForDoc = sinon.stub().callsArgWith(1, null, @updates)
  115. @UpdateManager.applyUpdate = sinon.stub()
  116. @RedisManager.setDocument = sinon.stub()
  117. @UpdateManager.fetchAndApplyUpdates @project_id, @doc_id, @callback
  118. it "should not call applyUpdate", ->
  119. @UpdateManager.applyUpdate.called.should.equal false
  120. it "should call the callback", ->
  121. @callback.called.should.equal true
  122. describe "applyUpdate", ->
  123. beforeEach ->
  124. @update = {op: [{p: 42, i: "foo"}]}
  125. @updatedDocLines = ["updated", "lines"]
  126. @version = 34
  127. @lines = ["original", "lines"]
  128. @track_changes_entries = { entries: "mock", comments: "mock" }
  129. @updated_track_changes_entries = { entries: "updated", comments: "updated" }
  130. @appliedOps = ["mock-applied-ops"]
  131. @DocumentManager.getDoc = sinon.stub().yields(null, @lines, @version, @track_changes_entries)
  132. @TrackChangesManager.applyUpdate = sinon.stub().yields(null, @updated_track_changes_entries)
  133. @ShareJsUpdateManager.applyUpdate = sinon.stub().yields(null, @updatedDocLines, @version, @appliedOps)
  134. @RedisManager.updateDocument = sinon.stub().yields()
  135. @HistoryManager.pushUncompressedHistoryOps = sinon.stub().callsArg(3)
  136. describe "normally", ->
  137. beforeEach ->
  138. @UpdateManager.applyUpdate @project_id, @doc_id, @update, @callback
  139. it "should apply the updates via ShareJS", ->
  140. @ShareJsUpdateManager.applyUpdate
  141. .calledWith(@project_id, @doc_id, @update, @lines, @version)
  142. .should.equal true
  143. it "should update the track changes entries", ->
  144. @TrackChangesManager.applyUpdate
  145. .calledWith(@project_id, @doc_id, @track_changes_entries, @appliedOps)
  146. .should.equal true
  147. it "should save the document", ->
  148. @RedisManager.updateDocument
  149. .calledWith(@doc_id, @updatedDocLines, @version, @appliedOps, @updated_track_changes_entries)
  150. .should.equal true
  151. it "should push the applied ops into the track changes queue", ->
  152. @HistoryManager.pushUncompressedHistoryOps
  153. .calledWith(@project_id, @doc_id, @appliedOps)
  154. .should.equal true
  155. it "should call the callback", ->
  156. @callback.called.should.equal true
  157. describe "with UTF-16 surrogate pairs in the update", ->
  158. beforeEach ->
  159. @update = {op: [{p: 42, i: "\uD835\uDC00"}]}
  160. @UpdateManager.applyUpdate @project_id, @doc_id, @update, @callback
  161. it "should apply the update but with surrogate pairs removed", ->
  162. @ShareJsUpdateManager.applyUpdate
  163. .calledWith(@project_id, @doc_id, @update)
  164. .should.equal true
  165. # \uFFFD is 'replacement character'
  166. @update.op[0].i.should.equal "\uFFFD\uFFFD"
  167. describe "lockUpdatesAndDo", ->
  168. beforeEach ->
  169. @method = sinon.stub().callsArgWith(3, null, @response_arg1)
  170. @callback = sinon.stub()
  171. @arg1 = "argument 1"
  172. @response_arg1 = "response argument 1"
  173. @lockValue = "mock-lock-value"
  174. @LockManager.getLock = sinon.stub().callsArgWith(1, null, @lockValue)
  175. @LockManager.releaseLock = sinon.stub().callsArg(2)
  176. describe "successfully", ->
  177. beforeEach ->
  178. @UpdateManager.continueProcessingUpdatesWithLock = sinon.stub()
  179. @UpdateManager.processOutstandingUpdates = sinon.stub().callsArg(2)
  180. @UpdateManager.lockUpdatesAndDo @method, @project_id, @doc_id, @arg1, @callback
  181. it "should lock the doc", ->
  182. @LockManager.getLock
  183. .calledWith(@doc_id)
  184. .should.equal true
  185. it "should process any outstanding updates", ->
  186. @UpdateManager.processOutstandingUpdates
  187. .calledWith(@project_id, @doc_id)
  188. .should.equal true
  189. it "should call the method", ->
  190. @method
  191. .calledWith(@project_id, @doc_id, @arg1)
  192. .should.equal true
  193. it "should return the method response to the callback", ->
  194. @callback
  195. .calledWith(null, @response_arg1)
  196. .should.equal true
  197. it "should release the lock", ->
  198. @LockManager.releaseLock
  199. .calledWith(@doc_id, @lockValue)
  200. .should.equal true
  201. it "should continue processing updates", ->
  202. @UpdateManager.continueProcessingUpdatesWithLock
  203. .calledWith(@project_id, @doc_id)
  204. .should.equal true
  205. describe "when processOutstandingUpdates returns an error", ->
  206. beforeEach ->
  207. @UpdateManager.processOutstandingUpdates = sinon.stub().callsArgWith(2, @error = new Error("Something went wrong"))
  208. @UpdateManager.lockUpdatesAndDo @method, @project_id, @doc_id, @arg1, @callback
  209. it "should free the lock", ->
  210. @LockManager.releaseLock.calledWith(@doc_id, @lockValue).should.equal true
  211. it "should return the error in the callback", ->
  212. @callback.calledWith(@error).should.equal true
  213. describe "when the method returns an error", ->
  214. beforeEach ->
  215. @UpdateManager.processOutstandingUpdates = sinon.stub().callsArg(2)
  216. @method = sinon.stub().callsArgWith(3, @error = new Error("something went wrong"), @response_arg1)
  217. @UpdateManager.lockUpdatesAndDo @method, @project_id, @doc_id, @arg1, @callback
  218. it "should free the lock", ->
  219. @LockManager.releaseLock.calledWith(@doc_id, @lockValue).should.equal true
  220. it "should return the error in the callback", ->
  221. @callback.calledWith(@error).should.equal true