RateLimitManager.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS206: Consider reworking classes to avoid initClass
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const sinon = require('sinon')
  14. const chai = require('chai')
  15. const should = chai.should()
  16. const { expect } = chai
  17. const modulePath = '../../../../app/js/RateLimitManager.js'
  18. const SandboxedModule = require('sandboxed-module')
  19. describe('RateLimitManager', function () {
  20. beforeEach(function () {
  21. let Timer
  22. this.RateLimitManager = SandboxedModule.require(modulePath, {
  23. requires: {
  24. 'logger-sharelatex': (this.logger = { log: sinon.stub() }),
  25. 'settings-sharelatex': (this.settings = {}),
  26. './Metrics': (this.Metrics = {
  27. Timer: (Timer = (function () {
  28. Timer = class Timer {
  29. static initClass() {
  30. this.prototype.done = sinon.stub()
  31. }
  32. }
  33. Timer.initClass()
  34. return Timer
  35. })()),
  36. gauge: sinon.stub()
  37. })
  38. }
  39. })
  40. this.callback = sinon.stub()
  41. return (this.RateLimiter = new this.RateLimitManager(1))
  42. })
  43. describe('for a single task', function () {
  44. beforeEach(function () {
  45. this.task = sinon.stub()
  46. return this.RateLimiter.run(this.task, this.callback)
  47. })
  48. it('should execute the task in the background', function () {
  49. return this.task.called.should.equal(true)
  50. })
  51. it('should call the callback', function () {
  52. return this.callback.called.should.equal(true)
  53. })
  54. return it('should finish with a worker count of one', function () {
  55. // because it's in the background
  56. return expect(this.RateLimiter.ActiveWorkerCount).to.equal(1)
  57. })
  58. })
  59. describe('for multiple tasks', function () {
  60. beforeEach(function (done) {
  61. this.task = sinon.stub()
  62. this.finalTask = sinon.stub()
  63. const task = (cb) => {
  64. this.task()
  65. return setTimeout(cb, 100)
  66. }
  67. const finalTask = (cb) => {
  68. this.finalTask()
  69. return setTimeout(cb, 100)
  70. }
  71. this.RateLimiter.run(task, this.callback)
  72. this.RateLimiter.run(task, this.callback)
  73. this.RateLimiter.run(task, this.callback)
  74. return this.RateLimiter.run(finalTask, (err) => {
  75. this.callback(err)
  76. return done()
  77. })
  78. })
  79. it('should execute the first three tasks', function () {
  80. return this.task.calledThrice.should.equal(true)
  81. })
  82. it('should execute the final task', function () {
  83. return this.finalTask.called.should.equal(true)
  84. })
  85. it('should call the callback', function () {
  86. return this.callback.called.should.equal(true)
  87. })
  88. return it('should finish with worker count of zero', function () {
  89. return expect(this.RateLimiter.ActiveWorkerCount).to.equal(0)
  90. })
  91. })
  92. return describe('for a mixture of long-running tasks', function () {
  93. beforeEach(function (done) {
  94. this.task = sinon.stub()
  95. this.finalTask = sinon.stub()
  96. const finalTask = (cb) => {
  97. this.finalTask()
  98. return setTimeout(cb, 100)
  99. }
  100. this.RateLimiter.run(this.task, this.callback)
  101. this.RateLimiter.run(this.task, this.callback)
  102. this.RateLimiter.run(this.task, this.callback)
  103. return this.RateLimiter.run(finalTask, (err) => {
  104. this.callback(err)
  105. return done()
  106. })
  107. })
  108. it('should execute the first three tasks', function () {
  109. return this.task.calledThrice.should.equal(true)
  110. })
  111. it('should execute the final task', function () {
  112. return this.finalTask.called.should.equal(true)
  113. })
  114. it('should call the callback', function () {
  115. return this.callback.called.should.equal(true)
  116. })
  117. return it('should finish with worker count of three', function () {
  118. return expect(this.RateLimiter.ActiveWorkerCount).to.equal(3)
  119. })
  120. })
  121. })