ApplyingUpdates.coffee 7.2 KB

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