UpdateManagerTests.coffee 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. "./RangesManager": @RangesManager = {}
  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. @ranges = { entries: "mock", comments: "mock" }
  129. @updated_ranges = { entries: "updated", comments: "updated" }
  130. @appliedOps = ["mock-applied-ops"]
  131. @DocumentManager.getDoc = sinon.stub().yields(null, @lines, @version, @ranges)
  132. @RangesManager.applyUpdate = sinon.stub().yields(null, @updated_ranges)
  133. @ShareJsUpdateManager.applyUpdate = sinon.stub().yields(null, @updatedDocLines, @version, @appliedOps)
  134. @RedisManager.updateDocument = sinon.stub().yields()
  135. @WebRedisManager.sendData = sinon.stub()
  136. @HistoryManager.pushUncompressedHistoryOps = sinon.stub().callsArg(3)
  137. describe "normally", ->
  138. beforeEach ->
  139. @UpdateManager.applyUpdate @project_id, @doc_id, @update, @callback
  140. it "should apply the updates via ShareJS", ->
  141. @ShareJsUpdateManager.applyUpdate
  142. .calledWith(@project_id, @doc_id, @update, @lines, @version)
  143. .should.equal true
  144. it "should update the ranges", ->
  145. @RangesManager.applyUpdate
  146. .calledWith(@project_id, @doc_id, @ranges, @appliedOps)
  147. .should.equal true
  148. it "should save the document", ->
  149. @RedisManager.updateDocument
  150. .calledWith(@doc_id, @updatedDocLines, @version, @appliedOps, @updated_ranges)
  151. .should.equal true
  152. it "should push the applied ops into the history queue", ->
  153. @HistoryManager.pushUncompressedHistoryOps
  154. .calledWith(@project_id, @doc_id, @appliedOps)
  155. .should.equal true
  156. it "should call the callback", ->
  157. @callback.called.should.equal true
  158. describe "with UTF-16 surrogate pairs in the update", ->
  159. beforeEach ->
  160. @update = {op: [{p: 42, i: "\uD835\uDC00"}]}
  161. @UpdateManager.applyUpdate @project_id, @doc_id, @update, @callback
  162. it "should apply the update but with surrogate pairs removed", ->
  163. @ShareJsUpdateManager.applyUpdate
  164. .calledWith(@project_id, @doc_id, @update)
  165. .should.equal true
  166. # \uFFFD is 'replacement character'
  167. @update.op[0].i.should.equal "\uFFFD\uFFFD"
  168. describe "with an error", ->
  169. beforeEach ->
  170. @error = new Error("something went wrong")
  171. @ShareJsUpdateManager.applyUpdate = sinon.stub().yields(@error)
  172. @UpdateManager.applyUpdate @project_id, @doc_id, @update, @callback
  173. it "should call WebRedisManager.sendData with the error", ->
  174. @WebRedisManager.sendData
  175. .calledWith({
  176. project_id: @project_id,
  177. doc_id: @doc_id,
  178. error: @error.message
  179. })
  180. .should.equal true
  181. it "should call the callback with the error", ->
  182. @callback.calledWith(@error).should.equal true
  183. describe "lockUpdatesAndDo", ->
  184. beforeEach ->
  185. @method = sinon.stub().callsArgWith(3, null, @response_arg1)
  186. @callback = sinon.stub()
  187. @arg1 = "argument 1"
  188. @response_arg1 = "response argument 1"
  189. @lockValue = "mock-lock-value"
  190. @LockManager.getLock = sinon.stub().callsArgWith(1, null, @lockValue)
  191. @LockManager.releaseLock = sinon.stub().callsArg(2)
  192. describe "successfully", ->
  193. beforeEach ->
  194. @UpdateManager.continueProcessingUpdatesWithLock = sinon.stub()
  195. @UpdateManager.processOutstandingUpdates = sinon.stub().callsArg(2)
  196. @UpdateManager.lockUpdatesAndDo @method, @project_id, @doc_id, @arg1, @callback
  197. it "should lock the doc", ->
  198. @LockManager.getLock
  199. .calledWith(@doc_id)
  200. .should.equal true
  201. it "should process any outstanding updates", ->
  202. @UpdateManager.processOutstandingUpdates
  203. .calledWith(@project_id, @doc_id)
  204. .should.equal true
  205. it "should call the method", ->
  206. @method
  207. .calledWith(@project_id, @doc_id, @arg1)
  208. .should.equal true
  209. it "should return the method response to the callback", ->
  210. @callback
  211. .calledWith(null, @response_arg1)
  212. .should.equal true
  213. it "should release the lock", ->
  214. @LockManager.releaseLock
  215. .calledWith(@doc_id, @lockValue)
  216. .should.equal true
  217. it "should continue processing updates", ->
  218. @UpdateManager.continueProcessingUpdatesWithLock
  219. .calledWith(@project_id, @doc_id)
  220. .should.equal true
  221. describe "when processOutstandingUpdates returns an error", ->
  222. beforeEach ->
  223. @UpdateManager.processOutstandingUpdates = sinon.stub().callsArgWith(2, @error = new Error("Something went wrong"))
  224. @UpdateManager.lockUpdatesAndDo @method, @project_id, @doc_id, @arg1, @callback
  225. it "should free the lock", ->
  226. @LockManager.releaseLock.calledWith(@doc_id, @lockValue).should.equal true
  227. it "should return the error in the callback", ->
  228. @callback.calledWith(@error).should.equal true
  229. describe "when the method returns an error", ->
  230. beforeEach ->
  231. @UpdateManager.processOutstandingUpdates = sinon.stub().callsArg(2)
  232. @method = sinon.stub().callsArgWith(3, @error = new Error("something went wrong"), @response_arg1)
  233. @UpdateManager.lockUpdatesAndDo @method, @project_id, @doc_id, @arg1, @callback
  234. it "should free the lock", ->
  235. @LockManager.releaseLock.calledWith(@doc_id, @lockValue).should.equal true
  236. it "should return the error in the callback", ->
  237. @callback.calledWith(@error).should.equal true