2020-05-06 06:11:22 -04:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-05-06 06:10:51 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-05-06 06:11:36 -04:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const modulePath = '../../../../app/js/DispatchManager.js'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const Errors = require('../../../../app/js/Errors.js')
|
|
|
|
|
|
|
|
describe('DispatchManager', function () {
|
|
|
|
beforeEach(function () {
|
2020-04-28 06:17:53 -04:00
|
|
|
let Timer
|
2020-05-06 06:11:36 -04:00
|
|
|
this.timeout(3000)
|
|
|
|
this.DispatchManager = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
|
|
|
'./UpdateManager': (this.UpdateManager = {}),
|
2021-07-12 12:47:15 -04:00
|
|
|
'@overleaf/settings': (this.settings = {
|
2020-05-06 06:11:36 -04:00
|
|
|
redis: {
|
2021-07-13 07:04:42 -04:00
|
|
|
documentupdater: {},
|
|
|
|
},
|
2020-05-06 06:11:36 -04:00
|
|
|
}),
|
2020-11-10 06:32:04 -05:00
|
|
|
'@overleaf/redis-wrapper': (this.redis = {}),
|
2020-05-06 06:11:36 -04:00
|
|
|
'./RateLimitManager': {},
|
|
|
|
'./Errors': Errors,
|
2020-04-28 06:17:53 -04:00
|
|
|
'./Metrics': (this.Metrics = {
|
|
|
|
Timer: (Timer = (function () {
|
|
|
|
Timer = class Timer {
|
|
|
|
static initClass() {
|
|
|
|
this.prototype.done = sinon.stub()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Timer.initClass()
|
|
|
|
return Timer
|
2021-07-13 07:04:42 -04:00
|
|
|
})()),
|
|
|
|
}),
|
|
|
|
},
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
this.callback = sinon.stub()
|
|
|
|
return (this.RateLimiter = {
|
|
|
|
run(task, cb) {
|
|
|
|
return task(cb)
|
2021-07-13 07:04:42 -04:00
|
|
|
},
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
}) // run task without rate limit
|
|
|
|
|
|
|
|
return describe('each worker', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.client = { auth: sinon.stub() }
|
|
|
|
this.redis.createClient = sinon.stub().returns(this.client)
|
|
|
|
return (this.worker = this.DispatchManager.createDispatcher(
|
2021-02-02 10:10:04 -05:00
|
|
|
this.RateLimiter,
|
|
|
|
0
|
2020-05-06 06:11:36 -04:00
|
|
|
))
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should create a new redis client', function () {
|
|
|
|
return this.redis.createClient.called.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('_waitForUpdateThenDispatchWorker', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.project_id = 'project-id-123'
|
|
|
|
this.doc_id = 'doc-id-123'
|
|
|
|
this.doc_key = `${this.project_id}:${this.doc_id}`
|
|
|
|
return (this.client.blpop = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, null, ['pending-updates-list', this.doc_key]))
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('in the normal case', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.UpdateManager.processOutstandingUpdatesWithLock = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArg(2)
|
|
|
|
return this.worker._waitForUpdateThenDispatchWorker(this.callback)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should call redis with BLPOP', function () {
|
|
|
|
return this.client.blpop
|
|
|
|
.calledWith('pending-updates-list', 0)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should call processOutstandingUpdatesWithLock', function () {
|
|
|
|
return this.UpdateManager.processOutstandingUpdatesWithLock
|
|
|
|
.calledWith(this.project_id, this.doc_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not log any errors', function () {
|
|
|
|
this.logger.error.called.should.equal(false)
|
|
|
|
return this.logger.warn.called.should.equal(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should call the callback', function () {
|
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with an error', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.UpdateManager.processOutstandingUpdatesWithLock = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, new Error('a generic error'))
|
|
|
|
return this.worker._waitForUpdateThenDispatchWorker(this.callback)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should log an error', function () {
|
|
|
|
return this.logger.error.called.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should call the callback', function () {
|
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-02-02 10:10:04 -05:00
|
|
|
describe("with a 'Delete component' error", function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
beforeEach(function () {
|
|
|
|
this.UpdateManager.processOutstandingUpdatesWithLock = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, new Errors.DeleteMismatchError())
|
|
|
|
return this.worker._waitForUpdateThenDispatchWorker(this.callback)
|
|
|
|
})
|
|
|
|
|
2021-09-30 04:28:32 -04:00
|
|
|
it('should log a debug message', function () {
|
|
|
|
return this.logger.debug.called.should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
return it('should call the callback', function () {
|
|
|
|
return this.callback.called.should.equal(true)
|
2021-02-02 10:10:04 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('pending updates list with shard key', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
this.client = {
|
|
|
|
auth: sinon.stub(),
|
2021-07-13 07:04:42 -04:00
|
|
|
blpop: sinon.stub().callsArgWith(2),
|
2021-02-02 10:10:04 -05:00
|
|
|
}
|
|
|
|
this.redis.createClient = sinon.stub().returns(this.client)
|
|
|
|
this.queueShardNumber = 7
|
|
|
|
this.worker = this.DispatchManager.createDispatcher(
|
|
|
|
this.RateLimiter,
|
|
|
|
this.queueShardNumber
|
|
|
|
)
|
|
|
|
this.worker._waitForUpdateThenDispatchWorker(done)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should call redis with BLPOP with the correct key', function () {
|
|
|
|
this.client.blpop
|
|
|
|
.calledWith(`pending-updates-list-${this.queueShardNumber}`, 0)
|
|
|
|
.should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return describe('run', function () {
|
|
|
|
return it('should call _waitForUpdateThenDispatchWorker until shutting down', function (done) {
|
|
|
|
let callCount = 0
|
2021-07-13 07:04:42 -04:00
|
|
|
this.worker._waitForUpdateThenDispatchWorker = callback => {
|
2020-05-06 06:11:36 -04:00
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-05-06 06:11:36 -04:00
|
|
|
}
|
|
|
|
callCount++
|
|
|
|
if (callCount === 3) {
|
|
|
|
this.settings.shuttingDown = true
|
|
|
|
}
|
|
|
|
return setTimeout(() => callback(), 10)
|
|
|
|
}
|
|
|
|
sinon.spy(this.worker, '_waitForUpdateThenDispatchWorker')
|
|
|
|
|
|
|
|
this.worker.run()
|
|
|
|
|
2021-10-26 04:08:56 -04:00
|
|
|
const checkStatus = () => {
|
2020-05-06 06:11:36 -04:00
|
|
|
if (!this.settings.shuttingDown) {
|
|
|
|
// retry until shutdown
|
|
|
|
setTimeout(checkStatus, 100)
|
|
|
|
} else {
|
|
|
|
this.worker._waitForUpdateThenDispatchWorker.callCount.should.equal(
|
|
|
|
3
|
|
|
|
)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return checkStatus()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|