lockUpdatesAndDoTests.coffee 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. @LockManager.getLock = sinon.stub().callsArgWith(1, null, true)
  20. @LockManager.releaseLock = sinon.stub().callsArg(1)
  21. describe "successfully", ->
  22. beforeEach ->
  23. @UpdateManager.continueProcessingUpdatesWithLock = sinon.stub()
  24. @UpdateManager.processOutstandingUpdates = sinon.stub().callsArg(2)
  25. @UpdateManager.lockUpdatesAndDo @method, @project_id, @doc_id, @arg1, @callback
  26. it "should lock the doc", ->
  27. @LockManager.getLock
  28. .calledWith(@doc_id)
  29. .should.equal true
  30. it "should process any outstanding updates", ->
  31. @UpdateManager.processOutstandingUpdates
  32. .calledWith(@project_id, @doc_id)
  33. .should.equal true
  34. it "should call the method", ->
  35. @method
  36. .calledWith(@project_id, @doc_id, @arg1)
  37. .should.equal true
  38. it "should return the method response to the callback", ->
  39. @callback
  40. .calledWith(null, @response_arg1)
  41. .should.equal true
  42. it "should release the lock", ->
  43. @LockManager.releaseLock
  44. .calledWith(@doc_id)
  45. .should.equal true
  46. it "should continue processing updates", ->
  47. @UpdateManager.continueProcessingUpdatesWithLock
  48. .calledWith(@project_id, @doc_id)
  49. .should.equal true
  50. describe "when processOutstandingUpdates returns an error", ->
  51. beforeEach ->
  52. @UpdateManager.processOutstandingUpdates = sinon.stub().callsArgWith(2, @error = new Error("Something went wrong"))
  53. @UpdateManager.lockUpdatesAndDo @method, @project_id, @doc_id, @arg1, @callback
  54. it "should free the lock", ->
  55. @LockManager.releaseLock.calledWith(@doc_id).should.equal true
  56. it "should return the error in the callback", ->
  57. @callback.calledWith(@error).should.equal true
  58. describe "when the method returns an error", ->
  59. beforeEach ->
  60. @UpdateManager.processOutstandingUpdates = sinon.stub().callsArg(2)
  61. @method = sinon.stub().callsArgWith(3, @error = new Error("something went wrong"), @response_arg1)
  62. @UpdateManager.lockUpdatesAndDo @method, @project_id, @doc_id, @arg1, @callback
  63. it "should free the lock", ->
  64. @LockManager.releaseLock.calledWith(@doc_id).should.equal true
  65. it "should return the error in the callback", ->
  66. @callback.calledWith(@error).should.equal true