overleaf/services/document-updater/test/unit/coffee/DispatchManager/DispatchManagerTests.coffee
2019-02-07 15:27:51 +00:00

81 lines
2.4 KiB
CoffeeScript

sinon = require('sinon')
chai = require('chai')
should = chai.should()
modulePath = "../../../../app/js/DispatchManager.js"
SandboxedModule = require('sandboxed-module')
describe "DispatchManager", ->
beforeEach ->
@timeout(3000)
@DispatchManager = SandboxedModule.require modulePath, requires:
"./UpdateManager" : @UpdateManager = {}
"logger-sharelatex": @logger = { log: sinon.stub() }
"settings-sharelatex": @settings =
redis:
realtime: {}
"redis-sharelatex": @redis = {}
"./RateLimitManager": {}
"./Metrics":
Timer: ->
done: ->
@callback = sinon.stub()
@RateLimiter = { run: (task,cb) -> task(cb) } # run task without rate limit
describe "each worker", ->
beforeEach ->
@client =
auth: sinon.stub()
@redis.createClient = sinon.stub().returns @client
@worker = @DispatchManager.createDispatcher(@RateLimiter)
it "should create a new redis client", ->
@redis.createClient.called.should.equal true
describe "_waitForUpdateThenDispatchWorker", ->
beforeEach ->
@project_id = "project-id-123"
@doc_id = "doc-id-123"
@doc_key = "#{@project_id}:#{@doc_id}"
@client.blpop = sinon.stub().callsArgWith(2, null, ["pending-updates-list", @doc_key])
@UpdateManager.processOutstandingUpdatesWithLock = sinon.stub().callsArg(2)
@worker._waitForUpdateThenDispatchWorker @callback
it "should call redis with BLPOP", ->
@client.blpop
.calledWith("pending-updates-list", 0)
.should.equal true
it "should call processOutstandingUpdatesWithLock", ->
@UpdateManager.processOutstandingUpdatesWithLock
.calledWith(@project_id, @doc_id)
.should.equal true
it "should call the callback", ->
@callback.called.should.equal true
describe "run", ->
it "should call _waitForUpdateThenDispatchWorker until shutting down", (done) ->
callCount = 0
@worker._waitForUpdateThenDispatchWorker = (callback = (error) ->) =>
callCount++
if callCount == 3
@settings.shuttingDown = true
setTimeout () ->
callback()
, 10
sinon.spy @worker, "_waitForUpdateThenDispatchWorker"
@worker.run()
checkStatus = () =>
if not @settings.shuttingDown # retry until shutdown
setTimeout checkStatus, 100
return
else
@worker._waitForUpdateThenDispatchWorker.callCount.should.equal 3
done()
checkStatus()