lockUpdatesAndDoTests.coffee 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 - lockUpdatesAndDo', ->
  7. beforeEach ->
  8. @UpdateManager = SandboxedModule.require modulePath, requires:
  9. "./LockManager" : @LockManager = {}
  10. "./RedisManager" : @RedisManager = {}
  11. "./ShareJsUpdateManager" : @ShareJsUpdateManager = {}
  12. "logger-sharelatex": @logger = { log: sinon.stub() }
  13. @project_id = "project-id-123"
  14. @doc_id = "doc-id-123"
  15. @method = sinon.stub().callsArgWith(3, null, @response_arg1)
  16. @callback = sinon.stub()
  17. @arg1 = "argument 1"
  18. @response_arg1 = "response argument 1"
  19. @lockValue = "mock-lock-value"
  20. @LockManager.getLock = sinon.stub().callsArgWith(1, null, @lockValue)
  21. @LockManager.releaseLock = sinon.stub().callsArg(2)
  22. describe "successfully", ->
  23. beforeEach ->
  24. @UpdateManager.continueProcessingUpdatesWithLock = sinon.stub()
  25. @UpdateManager.processOutstandingUpdates = sinon.stub().callsArg(2)
  26. @UpdateManager.lockUpdatesAndDo @method, @project_id, @doc_id, @arg1, @callback
  27. it "should lock the doc", ->
  28. @LockManager.getLock
  29. .calledWith(@doc_id)
  30. .should.equal true
  31. it "should process any outstanding updates", ->
  32. @UpdateManager.processOutstandingUpdates
  33. .calledWith(@project_id, @doc_id)
  34. .should.equal true
  35. it "should call the method", ->
  36. @method
  37. .calledWith(@project_id, @doc_id, @arg1)
  38. .should.equal true
  39. it "should return the method response to the callback", ->
  40. @callback
  41. .calledWith(null, @response_arg1)
  42. .should.equal true
  43. it "should release the lock", ->
  44. @LockManager.releaseLock
  45. .calledWith(@doc_id, @lockValue)
  46. .should.equal true
  47. it "should continue processing updates", ->
  48. @UpdateManager.continueProcessingUpdatesWithLock
  49. .calledWith(@project_id, @doc_id)
  50. .should.equal true
  51. describe "when processOutstandingUpdates returns an error", ->
  52. beforeEach ->
  53. @UpdateManager.processOutstandingUpdates = sinon.stub().callsArgWith(2, @error = new Error("Something went wrong"))
  54. @UpdateManager.lockUpdatesAndDo @method, @project_id, @doc_id, @arg1, @callback
  55. it "should free the lock", ->
  56. @LockManager.releaseLock.calledWith(@doc_id, @lockValue).should.equal true
  57. it "should return the error in the callback", ->
  58. @callback.calledWith(@error).should.equal true
  59. describe "when the method returns an error", ->
  60. beforeEach ->
  61. @UpdateManager.processOutstandingUpdates = sinon.stub().callsArg(2)
  62. @method = sinon.stub().callsArgWith(3, @error = new Error("something went wrong"), @response_arg1)
  63. @UpdateManager.lockUpdatesAndDo @method, @project_id, @doc_id, @arg1, @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