ApplyingUpdates.coffee 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. "./ShareJsUpdateManager" : @ShareJsUpdateManager = {}
  15. "logger-sharelatex": @logger = { log: sinon.stub() }
  16. "./Metrics": @Metrics =
  17. Timer: class Timer
  18. done: sinon.stub()
  19. describe "processOutstandingUpdates", ->
  20. beforeEach ->
  21. @UpdateManager.fetchAndApplyUpdates = sinon.stub().callsArg(2)
  22. @UpdateManager.processOutstandingUpdates @project_id, @doc_id, @callback
  23. it "should apply the updates", ->
  24. @UpdateManager.fetchAndApplyUpdates.calledWith(@project_id, @doc_id).should.equal true
  25. it "should call the callback", ->
  26. @callback.called.should.equal true
  27. it "should time the execution", ->
  28. @Metrics.Timer::done.called.should.equal true
  29. describe "processOutstandingUpdatesWithLock", ->
  30. describe "when the lock is free", ->
  31. beforeEach ->
  32. @LockManager.tryLock = sinon.stub().callsArgWith(1, null, true, @lockValue = "mock-lock-value")
  33. @LockManager.releaseLock = sinon.stub().callsArg(2)
  34. @UpdateManager.continueProcessingUpdatesWithLock = sinon.stub().callsArg(2)
  35. @UpdateManager.processOutstandingUpdates = sinon.stub().callsArg(2)
  36. describe "successfully", ->
  37. beforeEach ->
  38. @UpdateManager.processOutstandingUpdatesWithLock @project_id, @doc_id, @callback
  39. it "should acquire the lock", ->
  40. @LockManager.tryLock.calledWith(@doc_id).should.equal true
  41. it "should free the lock", ->
  42. @LockManager.releaseLock.calledWith(@doc_id, @lockValue).should.equal true
  43. it "should process the outstanding updates", ->
  44. @UpdateManager.processOutstandingUpdates.calledWith(@project_id, @doc_id).should.equal true
  45. it "should do everything with the lock acquired", ->
  46. @UpdateManager.processOutstandingUpdates.calledAfter(@LockManager.tryLock).should.equal true
  47. @UpdateManager.processOutstandingUpdates.calledBefore(@LockManager.releaseLock).should.equal true
  48. it "should continue processing new updates that may have come in", ->
  49. @UpdateManager.continueProcessingUpdatesWithLock.calledWith(@project_id, @doc_id).should.equal true
  50. it "should return the callback", ->
  51. @callback.called.should.equal true
  52. describe "when processOutstandingUpdates returns an error", ->
  53. beforeEach ->
  54. @UpdateManager.processOutstandingUpdates = sinon.stub().callsArgWith(2, @error = new Error("Something went wrong"))
  55. @UpdateManager.processOutstandingUpdatesWithLock @project_id, @doc_id, @callback
  56. it "should free the lock", ->
  57. @LockManager.releaseLock.calledWith(@doc_id, @lockValue).should.equal true
  58. it "should return the error in the callback", ->
  59. @callback.calledWith(@error).should.equal true
  60. describe "when the lock is taken", ->
  61. beforeEach ->
  62. @LockManager.tryLock = sinon.stub().callsArgWith(1, null, false)
  63. @UpdateManager.processOutstandingUpdates = sinon.stub().callsArg(2)
  64. @UpdateManager.processOutstandingUpdatesWithLock @project_id, @doc_id, @callback
  65. it "should return the callback", ->
  66. @callback.called.should.equal true
  67. it "should not process the updates", ->
  68. @UpdateManager.processOutstandingUpdates.called.should.equal false
  69. describe "continueProcessingUpdatesWithLock", ->
  70. describe "when there are outstanding updates", ->
  71. beforeEach ->
  72. @RedisManager.getUpdatesLength = sinon.stub().callsArgWith(1, null, 3)
  73. @UpdateManager.processOutstandingUpdatesWithLock = sinon.stub().callsArg(2)
  74. @UpdateManager.continueProcessingUpdatesWithLock @project_id, @doc_id, @callback
  75. it "should process the outstanding updates", ->
  76. @UpdateManager.processOutstandingUpdatesWithLock.calledWith(@project_id, @doc_id).should.equal true
  77. it "should return the callback", ->
  78. @callback.called.should.equal true
  79. describe "when there are no outstanding updates", ->
  80. beforeEach ->
  81. @RedisManager.getUpdatesLength = sinon.stub().callsArgWith(1, null, 0)
  82. @UpdateManager.processOutstandingUpdatesWithLock = sinon.stub().callsArg(2)
  83. @UpdateManager.continueProcessingUpdatesWithLock @project_id, @doc_id, @callback
  84. it "should not try to process the outstanding updates", ->
  85. @UpdateManager.processOutstandingUpdatesWithLock.called.should.equal false
  86. it "should return the callback", ->
  87. @callback.called.should.equal true
  88. describe "fetchAndApplyUpdates", ->
  89. describe "with updates", ->
  90. beforeEach ->
  91. @updates = [{p: 1, t: "foo"}]
  92. @updatedDocLines = ["updated", "lines"]
  93. @version = 34
  94. @RedisManager.getPendingUpdatesForDoc = sinon.stub().callsArgWith(1, null, @updates)
  95. @UpdateManager.applyUpdates = sinon.stub().callsArgWith(3, null, @updatedDocLines, @version)
  96. @UpdateManager.fetchAndApplyUpdates @project_id, @doc_id, @callback
  97. it "should get the pending updates", ->
  98. @RedisManager.getPendingUpdatesForDoc.calledWith(@doc_id).should.equal true
  99. it "should apply the updates", ->
  100. @UpdateManager.applyUpdates
  101. .calledWith(@project_id, @doc_id, @updates)
  102. .should.equal true
  103. it "should call the callback", ->
  104. @callback.called.should.equal true
  105. describe "when there are no updates", ->
  106. beforeEach ->
  107. @updates = []
  108. @RedisManager.getPendingUpdatesForDoc = sinon.stub().callsArgWith(1, null, @updates)
  109. @UpdateManager.applyUpdates = sinon.stub()
  110. @RedisManager.setDocument = sinon.stub()
  111. @UpdateManager.fetchAndApplyUpdates @project_id, @doc_id, @callback
  112. it "should not call applyUpdates", ->
  113. @UpdateManager.applyUpdates.called.should.equal false
  114. it "should call the callback", ->
  115. @callback.called.should.equal true
  116. describe "applyUpdates", ->
  117. beforeEach ->
  118. @updates = [{op: [{p: 42, i: "foo"}]}]
  119. @updatedDocLines = ["updated", "lines"]
  120. @version = 34
  121. @ShareJsUpdateManager.applyUpdates = sinon.stub().callsArgWith(3, null, @updatedDocLines, @version)
  122. @RedisManager.setDocument = sinon.stub().callsArg(3)
  123. describe "normally", ->
  124. beforeEach ->
  125. @UpdateManager.applyUpdates @project_id, @doc_id, @updates, @callback
  126. it "should apply the updates via ShareJS", ->
  127. @ShareJsUpdateManager.applyUpdates
  128. .calledWith(@project_id, @doc_id, @updates)
  129. .should.equal true
  130. it "should save the document", ->
  131. @RedisManager.setDocument
  132. .calledWith(@doc_id, @updatedDocLines, @version)
  133. .should.equal true
  134. it "should call the callback", ->
  135. @callback.called.should.equal true
  136. describe "with UTF-16 surrogate pairs in the update", ->
  137. beforeEach ->
  138. @updates = [{op: [{p: 42, i: "\uD835\uDC00"}]}]
  139. @UpdateManager.applyUpdates @project_id, @doc_id, @updates, @callback
  140. it "should apply the update but with surrogate pairs removed", ->
  141. @ShareJsUpdateManager.applyUpdates
  142. .calledWith(@project_id, @doc_id, @updates)
  143. .should.equal true
  144. # \uFFFD is 'replacement character'
  145. @updates[0].op[0].i.should.equal "\uFFFD\uFFFD"