UpdateManagerTests.coffee 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. "./RealTimeRedisManager" : @RealTimeRedisManager = {}
  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. "./Profiler": class Profiler
  25. log: sinon.stub().returns { end: sinon.stub() }
  26. end: sinon.stub()
  27. describe "processOutstandingUpdates", ->
  28. beforeEach ->
  29. @UpdateManager.fetchAndApplyUpdates = sinon.stub().callsArg(2)
  30. @UpdateManager.processOutstandingUpdates @project_id, @doc_id, @callback
  31. it "should apply the updates", ->
  32. @UpdateManager.fetchAndApplyUpdates.calledWith(@project_id, @doc_id).should.equal true
  33. it "should call the callback", ->
  34. @callback.called.should.equal true
  35. it "should time the execution", ->
  36. @Metrics.Timer::done.called.should.equal true
  37. describe "processOutstandingUpdatesWithLock", ->
  38. describe "when the lock is free", ->
  39. beforeEach ->
  40. @LockManager.tryLock = sinon.stub().callsArgWith(1, null, true, @lockValue = "mock-lock-value")
  41. @LockManager.releaseLock = sinon.stub().callsArg(2)
  42. @UpdateManager.continueProcessingUpdatesWithLock = sinon.stub().callsArg(2)
  43. @UpdateManager.processOutstandingUpdates = sinon.stub().callsArg(2)
  44. describe "successfully", ->
  45. beforeEach ->
  46. @UpdateManager.processOutstandingUpdatesWithLock @project_id, @doc_id, @callback
  47. it "should acquire the lock", ->
  48. @LockManager.tryLock.calledWith(@doc_id).should.equal true
  49. it "should free the lock", ->
  50. @LockManager.releaseLock.calledWith(@doc_id, @lockValue).should.equal true
  51. it "should process the outstanding updates", ->
  52. @UpdateManager.processOutstandingUpdates.calledWith(@project_id, @doc_id).should.equal true
  53. it "should do everything with the lock acquired", ->
  54. @UpdateManager.processOutstandingUpdates.calledAfter(@LockManager.tryLock).should.equal true
  55. @UpdateManager.processOutstandingUpdates.calledBefore(@LockManager.releaseLock).should.equal true
  56. it "should continue processing new updates that may have come in", ->
  57. @UpdateManager.continueProcessingUpdatesWithLock.calledWith(@project_id, @doc_id).should.equal true
  58. it "should return the callback", ->
  59. @callback.called.should.equal true
  60. describe "when processOutstandingUpdates returns an error", ->
  61. beforeEach ->
  62. @UpdateManager.processOutstandingUpdates = sinon.stub().callsArgWith(2, @error = new Error("Something went wrong"))
  63. @UpdateManager.processOutstandingUpdatesWithLock @project_id, @doc_id, @callback
  64. it "should free the lock", ->
  65. @LockManager.releaseLock.calledWith(@doc_id, @lockValue).should.equal true
  66. it "should return the error in the callback", ->
  67. @callback.calledWith(@error).should.equal true
  68. describe "when the lock is taken", ->
  69. beforeEach ->
  70. @LockManager.tryLock = sinon.stub().callsArgWith(1, null, false)
  71. @UpdateManager.processOutstandingUpdates = sinon.stub().callsArg(2)
  72. @UpdateManager.processOutstandingUpdatesWithLock @project_id, @doc_id, @callback
  73. it "should return the callback", ->
  74. @callback.called.should.equal true
  75. it "should not process the updates", ->
  76. @UpdateManager.processOutstandingUpdates.called.should.equal false
  77. describe "continueProcessingUpdatesWithLock", ->
  78. describe "when there are outstanding updates", ->
  79. beforeEach ->
  80. @RealTimeRedisManager.getUpdatesLength = sinon.stub().callsArgWith(1, null, 3)
  81. @UpdateManager.processOutstandingUpdatesWithLock = sinon.stub().callsArg(2)
  82. @UpdateManager.continueProcessingUpdatesWithLock @project_id, @doc_id, @callback
  83. it "should process the outstanding updates", ->
  84. @UpdateManager.processOutstandingUpdatesWithLock.calledWith(@project_id, @doc_id).should.equal true
  85. it "should return the callback", ->
  86. @callback.called.should.equal true
  87. describe "when there are no outstanding updates", ->
  88. beforeEach ->
  89. @RealTimeRedisManager.getUpdatesLength = sinon.stub().callsArgWith(1, null, 0)
  90. @UpdateManager.processOutstandingUpdatesWithLock = sinon.stub().callsArg(2)
  91. @UpdateManager.continueProcessingUpdatesWithLock @project_id, @doc_id, @callback
  92. it "should not try to process the outstanding updates", ->
  93. @UpdateManager.processOutstandingUpdatesWithLock.called.should.equal false
  94. it "should return the callback", ->
  95. @callback.called.should.equal true
  96. describe "fetchAndApplyUpdates", ->
  97. describe "with updates", ->
  98. beforeEach ->
  99. @updates = [{p: 1, t: "foo"}]
  100. @updatedDocLines = ["updated", "lines"]
  101. @version = 34
  102. @RealTimeRedisManager.getPendingUpdatesForDoc = sinon.stub().callsArgWith(1, null, @updates)
  103. @UpdateManager.applyUpdate = sinon.stub().callsArgWith(3, null, @updatedDocLines, @version)
  104. @UpdateManager.fetchAndApplyUpdates @project_id, @doc_id, @callback
  105. it "should get the pending updates", ->
  106. @RealTimeRedisManager.getPendingUpdatesForDoc.calledWith(@doc_id).should.equal true
  107. it "should apply the updates", ->
  108. for update in @updates
  109. @UpdateManager.applyUpdate
  110. .calledWith(@project_id, @doc_id, update)
  111. .should.equal true
  112. it "should call the callback", ->
  113. @callback.called.should.equal true
  114. describe "when there are no updates", ->
  115. beforeEach ->
  116. @updates = []
  117. @RealTimeRedisManager.getPendingUpdatesForDoc = sinon.stub().callsArgWith(1, null, @updates)
  118. @UpdateManager.applyUpdate = sinon.stub()
  119. @RedisManager.setDocument = sinon.stub()
  120. @UpdateManager.fetchAndApplyUpdates @project_id, @doc_id, @callback
  121. it "should not call applyUpdate", ->
  122. @UpdateManager.applyUpdate.called.should.equal false
  123. it "should call the callback", ->
  124. @callback.called.should.equal true
  125. describe "applyUpdate", ->
  126. beforeEach ->
  127. @update = {op: [{p: 42, i: "foo"}]}
  128. @updatedDocLines = ["updated", "lines"]
  129. @version = 34
  130. @lines = ["original", "lines"]
  131. @ranges = { entries: "mock", comments: "mock" }
  132. @updated_ranges = { entries: "updated", comments: "updated" }
  133. @appliedOps = [ {v: 42, op: "mock-op-42"}, { v: 45, op: "mock-op-45" }]
  134. @doc_ops_length = sinon.stub()
  135. @project_ops_length = sinon.stub()
  136. @pathname = '/a/b/c.tex'
  137. @DocumentManager.getDoc = sinon.stub().yields(null, @lines, @version, @ranges, @pathname)
  138. @RangesManager.applyUpdate = sinon.stub().yields(null, @updated_ranges)
  139. @ShareJsUpdateManager.applyUpdate = sinon.stub().yields(null, @updatedDocLines, @version, @appliedOps)
  140. @RedisManager.updateDocument = sinon.stub().yields(null, @doc_ops_length, @project_ops_length)
  141. @RealTimeRedisManager.sendData = sinon.stub()
  142. @UpdateManager._addProjectHistoryMetadataToOps = sinon.stub()
  143. @HistoryManager.recordAndFlushHistoryOps = sinon.stub().callsArg(5)
  144. describe "normally", ->
  145. beforeEach ->
  146. @UpdateManager.applyUpdate @project_id, @doc_id, @update, @callback
  147. it "should apply the updates via ShareJS", ->
  148. @ShareJsUpdateManager.applyUpdate
  149. .calledWith(@project_id, @doc_id, @update, @lines, @version)
  150. .should.equal true
  151. it "should update the ranges", ->
  152. @RangesManager.applyUpdate
  153. .calledWith(@project_id, @doc_id, @ranges, @appliedOps, @updatedDocLines)
  154. .should.equal true
  155. it "should save the document", ->
  156. @RedisManager.updateDocument
  157. .calledWith(@project_id, @doc_id, @updatedDocLines, @version, @appliedOps, @updated_ranges)
  158. .should.equal true
  159. it "should add metadata to the ops" , ->
  160. @UpdateManager._addProjectHistoryMetadataToOps
  161. .calledWith(@appliedOps, @pathname, @lines)
  162. .should.equal true
  163. it "should push the applied ops into the history queue", ->
  164. @HistoryManager.recordAndFlushHistoryOps
  165. .calledWith(@project_id, @doc_id, @appliedOps, @doc_ops_length, @project_ops_length)
  166. .should.equal true
  167. it "should call the callback", ->
  168. @callback.called.should.equal true
  169. describe "with UTF-16 surrogate pairs in the update", ->
  170. beforeEach ->
  171. @update = {op: [{p: 42, i: "\uD835\uDC00"}]}
  172. @UpdateManager.applyUpdate @project_id, @doc_id, @update, @callback
  173. it "should apply the update but with surrogate pairs removed", ->
  174. @ShareJsUpdateManager.applyUpdate
  175. .calledWith(@project_id, @doc_id, @update)
  176. .should.equal true
  177. # \uFFFD is 'replacement character'
  178. @update.op[0].i.should.equal "\uFFFD\uFFFD"
  179. describe "with an error", ->
  180. beforeEach ->
  181. @error = new Error("something went wrong")
  182. @ShareJsUpdateManager.applyUpdate = sinon.stub().yields(@error)
  183. @UpdateManager.applyUpdate @project_id, @doc_id, @update, @callback
  184. it "should call RealTimeRedisManager.sendData with the error", ->
  185. @RealTimeRedisManager.sendData
  186. .calledWith({
  187. project_id: @project_id,
  188. doc_id: @doc_id,
  189. error: @error.message
  190. })
  191. .should.equal true
  192. it "should call the callback with the error", ->
  193. @callback.calledWith(@error).should.equal true
  194. describe "_addProjectHistoryMetadataToOps", ->
  195. it "should add pathname and doc_length metadata to the ops", ->
  196. lines = [
  197. 'some'
  198. 'test'
  199. 'data'
  200. ]
  201. appliedOps = [
  202. { v: 42, op: [{i: "foo", p: 4}, { i: "bar", p: 6 }] },
  203. { v: 45, op: [{d: "qux", p: 4}, { i: "bazbaz", p: 14 }] },
  204. { v: 49, op: [{i: "penguin", p: 18}] }
  205. ]
  206. @UpdateManager._addProjectHistoryMetadataToOps(appliedOps, @pathname, lines)
  207. appliedOps.should.deep.equal [{
  208. v: 42
  209. op: [{i: "foo", p: 4}, { i: "bar", p: 6 }]
  210. meta:
  211. pathname: @pathname
  212. doc_length: 14
  213. }, {
  214. v: 45
  215. op: [{d: "qux", p: 4}, { i: "bazbaz", p: 14 }]
  216. meta:
  217. pathname: @pathname
  218. doc_length: 20 # 14 + 'foo' + 'bar'
  219. }, {
  220. v: 49
  221. op: [{i: "penguin", p: 18}]
  222. meta:
  223. pathname: @pathname
  224. doc_length: 23 # 14 - 'qux' + 'bazbaz'
  225. }]
  226. describe "lockUpdatesAndDo", ->
  227. beforeEach ->
  228. @method = sinon.stub().callsArgWith(3, null, @response_arg1)
  229. @callback = sinon.stub()
  230. @arg1 = "argument 1"
  231. @response_arg1 = "response argument 1"
  232. @lockValue = "mock-lock-value"
  233. @LockManager.getLock = sinon.stub().callsArgWith(1, null, @lockValue)
  234. @LockManager.releaseLock = sinon.stub().callsArg(2)
  235. describe "successfully", ->
  236. beforeEach ->
  237. @UpdateManager.continueProcessingUpdatesWithLock = sinon.stub()
  238. @UpdateManager.processOutstandingUpdates = sinon.stub().callsArg(2)
  239. @UpdateManager.lockUpdatesAndDo @method, @project_id, @doc_id, @arg1, @callback
  240. it "should lock the doc", ->
  241. @LockManager.getLock
  242. .calledWith(@doc_id)
  243. .should.equal true
  244. it "should process any outstanding updates", ->
  245. @UpdateManager.processOutstandingUpdates
  246. .calledWith(@project_id, @doc_id)
  247. .should.equal true
  248. it "should call the method", ->
  249. @method
  250. .calledWith(@project_id, @doc_id, @arg1)
  251. .should.equal true
  252. it "should return the method response to the callback", ->
  253. @callback
  254. .calledWith(null, @response_arg1)
  255. .should.equal true
  256. it "should release the lock", ->
  257. @LockManager.releaseLock
  258. .calledWith(@doc_id, @lockValue)
  259. .should.equal true
  260. it "should continue processing updates", ->
  261. @UpdateManager.continueProcessingUpdatesWithLock
  262. .calledWith(@project_id, @doc_id)
  263. .should.equal true
  264. describe "when processOutstandingUpdates returns an error", ->
  265. beforeEach ->
  266. @UpdateManager.processOutstandingUpdates = sinon.stub().callsArgWith(2, @error = new Error("Something went wrong"))
  267. @UpdateManager.lockUpdatesAndDo @method, @project_id, @doc_id, @arg1, @callback
  268. it "should free the lock", ->
  269. @LockManager.releaseLock.calledWith(@doc_id, @lockValue).should.equal true
  270. it "should return the error in the callback", ->
  271. @callback.calledWith(@error).should.equal true
  272. describe "when the method returns an error", ->
  273. beforeEach ->
  274. @UpdateManager.processOutstandingUpdates = sinon.stub().callsArg(2)
  275. @method = sinon.stub().callsArgWith(3, @error = new Error("something went wrong"), @response_arg1)
  276. @UpdateManager.lockUpdatesAndDo @method, @project_id, @doc_id, @arg1, @callback
  277. it "should free the lock", ->
  278. @LockManager.releaseLock.calledWith(@doc_id, @lockValue).should.equal true
  279. it "should return the error in the callback", ->
  280. @callback.calledWith(@error).should.equal true