DispatchManagerTests.coffee 2.3 KB

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