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
|
|
|
|
* DS206: Consider reworking classes to avoid initClass
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-05-06 06:11:36 -04:00
|
|
|
const sinon = require('sinon')
|
2021-04-01 15:51:00 -04:00
|
|
|
const { expect } = require('chai')
|
2020-05-06 06:11:36 -04:00
|
|
|
const modulePath = '../../../../app/js/RateLimitManager.js'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
2017-05-26 07:10:57 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
describe('RateLimitManager', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
let Timer
|
|
|
|
this.RateLimitManager = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
2021-07-12 12:47:15 -04:00
|
|
|
'@overleaf/settings': (this.settings = {}),
|
2020-05-06 06:11:36 -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
|
|
|
gauge: sinon.stub(),
|
|
|
|
}),
|
|
|
|
},
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
this.callback = sinon.stub()
|
|
|
|
return (this.RateLimiter = new this.RateLimitManager(1))
|
|
|
|
})
|
2017-05-26 07:10:57 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
describe('for a single task', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.task = sinon.stub()
|
|
|
|
return this.RateLimiter.run(this.task, this.callback)
|
|
|
|
})
|
2017-05-26 07:10:57 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
it('should execute the task in the background', function () {
|
|
|
|
return this.task.called.should.equal(true)
|
|
|
|
})
|
2017-05-26 07:10:57 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
it('should call the callback', function () {
|
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
2017-05-26 07:10:57 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
return it('should finish with a worker count of one', function () {
|
|
|
|
// because it's in the background
|
|
|
|
return expect(this.RateLimiter.ActiveWorkerCount).to.equal(1)
|
|
|
|
})
|
|
|
|
})
|
2017-05-26 07:10:57 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
describe('for multiple tasks', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
this.task = sinon.stub()
|
|
|
|
this.finalTask = sinon.stub()
|
2021-07-13 07:04:42 -04:00
|
|
|
const task = cb => {
|
2020-05-06 06:11:36 -04:00
|
|
|
this.task()
|
|
|
|
return setTimeout(cb, 100)
|
|
|
|
}
|
2021-07-13 07:04:42 -04:00
|
|
|
const finalTask = cb => {
|
2020-05-06 06:11:36 -04:00
|
|
|
this.finalTask()
|
|
|
|
return setTimeout(cb, 100)
|
|
|
|
}
|
|
|
|
this.RateLimiter.run(task, this.callback)
|
|
|
|
this.RateLimiter.run(task, this.callback)
|
|
|
|
this.RateLimiter.run(task, this.callback)
|
2021-07-13 07:04:42 -04:00
|
|
|
return this.RateLimiter.run(finalTask, err => {
|
2020-05-06 06:11:36 -04:00
|
|
|
this.callback(err)
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
})
|
2017-05-26 07:10:57 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
it('should execute the first three tasks', function () {
|
|
|
|
return this.task.calledThrice.should.equal(true)
|
|
|
|
})
|
2017-05-26 07:10:57 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
it('should execute the final task', function () {
|
|
|
|
return this.finalTask.called.should.equal(true)
|
|
|
|
})
|
2017-05-26 07:10:57 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
it('should call the callback', function () {
|
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
2017-05-26 07:10:57 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
return it('should finish with worker count of zero', function () {
|
|
|
|
return expect(this.RateLimiter.ActiveWorkerCount).to.equal(0)
|
|
|
|
})
|
|
|
|
})
|
2017-05-26 07:10:57 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
return describe('for a mixture of long-running tasks', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
this.task = sinon.stub()
|
|
|
|
this.finalTask = sinon.stub()
|
2021-07-13 07:04:42 -04:00
|
|
|
const finalTask = cb => {
|
2020-05-06 06:11:36 -04:00
|
|
|
this.finalTask()
|
|
|
|
return setTimeout(cb, 100)
|
|
|
|
}
|
|
|
|
this.RateLimiter.run(this.task, this.callback)
|
|
|
|
this.RateLimiter.run(this.task, this.callback)
|
|
|
|
this.RateLimiter.run(this.task, this.callback)
|
2021-07-13 07:04:42 -04:00
|
|
|
return this.RateLimiter.run(finalTask, err => {
|
2020-05-06 06:11:36 -04:00
|
|
|
this.callback(err)
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
})
|
2017-05-26 07:10:57 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
it('should execute the first three tasks', function () {
|
|
|
|
return this.task.calledThrice.should.equal(true)
|
|
|
|
})
|
2017-05-26 07:10:57 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
it('should execute the final task', function () {
|
|
|
|
return this.finalTask.called.should.equal(true)
|
|
|
|
})
|
2017-05-26 07:10:57 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
it('should call the callback', function () {
|
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
2017-05-26 07:10:57 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
return it('should finish with worker count of three', function () {
|
|
|
|
return expect(this.RateLimiter.ActiveWorkerCount).to.equal(3)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|