DispatchManagerTests.coffee 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/DispatchManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. describe "DispatchManager", ->
  7. beforeEach ->
  8. @timeout(3000)
  9. @DispatchManager = SandboxedModule.require modulePath, requires:
  10. "./UpdateManager" : @UpdateManager = {}
  11. "logger-sharelatex": @logger = { log: sinon.stub() }
  12. "settings-sharelatex": @settings =
  13. redis:
  14. documentupdater: {}
  15. "redis-sharelatex": @redis = {}
  16. "./RateLimitManager": {}
  17. "./Metrics":
  18. Timer: ->
  19. done: ->
  20. @callback = sinon.stub()
  21. @RateLimiter = { run: (task,cb) -> task(cb) } # run task without rate limit
  22. describe "each worker", ->
  23. beforeEach ->
  24. @client =
  25. auth: sinon.stub()
  26. @redis.createClient = sinon.stub().returns @client
  27. @worker = @DispatchManager.createDispatcher(@RateLimiter)
  28. it "should create a new redis client", ->
  29. @redis.createClient.called.should.equal true
  30. describe "_waitForUpdateThenDispatchWorker", ->
  31. beforeEach ->
  32. @project_id = "project-id-123"
  33. @doc_id = "doc-id-123"
  34. @doc_key = "#{@project_id}:#{@doc_id}"
  35. @client.blpop = sinon.stub().callsArgWith(2, null, ["pending-updates-list", @doc_key])
  36. @UpdateManager.processOutstandingUpdatesWithLock = sinon.stub().callsArg(2)
  37. @worker._waitForUpdateThenDispatchWorker @callback
  38. it "should call redis with BLPOP", ->
  39. @client.blpop
  40. .calledWith("pending-updates-list", 0)
  41. .should.equal true
  42. it "should call processOutstandingUpdatesWithLock", ->
  43. @UpdateManager.processOutstandingUpdatesWithLock
  44. .calledWith(@project_id, @doc_id)
  45. .should.equal true
  46. it "should call the callback", ->
  47. @callback.called.should.equal true
  48. describe "run", ->
  49. it "should call _waitForUpdateThenDispatchWorker until shutting down", (done) ->
  50. callCount = 0
  51. @worker._waitForUpdateThenDispatchWorker = (callback = (error) ->) =>
  52. callCount++
  53. if callCount == 3
  54. @settings.shuttingDown = true
  55. setTimeout () ->
  56. callback()
  57. , 10
  58. sinon.spy @worker, "_waitForUpdateThenDispatchWorker"
  59. @worker.run()
  60. checkStatus = () =>
  61. if not @settings.shuttingDown # retry until shutdown
  62. setTimeout checkStatus, 100
  63. return
  64. else
  65. @worker._waitForUpdateThenDispatchWorker.callCount.should.equal 3
  66. done()
  67. checkStatus()