UpdateManagerTests.coffee 14 KB

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