lockUpdatesAndDoTests.coffee 2.9 KB

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