UpdateManagerTests.coffee 13 KB

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