DispatchManagerTests.coffee 2.2 KB

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