RateLimitManager.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 {
  17. expect
  18. } = chai;
  19. const modulePath = "../../../../app/js/RateLimitManager.js";
  20. const SandboxedModule = require('sandboxed-module');
  21. describe("RateLimitManager", function() {
  22. beforeEach(function() {
  23. let Timer;
  24. this.RateLimitManager = SandboxedModule.require(modulePath, { requires: {
  25. "logger-sharelatex": (this.logger = { log: sinon.stub() }),
  26. "settings-sharelatex": (this.settings = {}),
  27. "./Metrics": (this.Metrics = {
  28. Timer: (Timer = (function() {
  29. Timer = class Timer {
  30. static initClass() {
  31. this.prototype.done = sinon.stub();
  32. }
  33. };
  34. Timer.initClass();
  35. return Timer;
  36. })()),
  37. gauge: sinon.stub()
  38. })
  39. }
  40. }
  41. );
  42. this.callback = sinon.stub();
  43. return this.RateLimiter = new this.RateLimitManager(1);
  44. });
  45. describe("for a single task", function() {
  46. beforeEach(function() {
  47. this.task = sinon.stub();
  48. return this.RateLimiter.run(this.task, this.callback);
  49. });
  50. it("should execute the task in the background", function() {
  51. return this.task.called.should.equal(true);
  52. });
  53. it("should call the callback", function() {
  54. return this.callback.called.should.equal(true);
  55. });
  56. return it("should finish with a worker count of one", function() {
  57. // because it's in the background
  58. return expect(this.RateLimiter.ActiveWorkerCount).to.equal(1);
  59. });
  60. });
  61. describe("for multiple tasks", function() {
  62. beforeEach(function(done) {
  63. this.task = sinon.stub();
  64. this.finalTask = sinon.stub();
  65. const task = cb => {
  66. this.task();
  67. return setTimeout(cb, 100);
  68. };
  69. const finalTask = cb => {
  70. this.finalTask();
  71. return setTimeout(cb, 100);
  72. };
  73. this.RateLimiter.run(task, this.callback);
  74. this.RateLimiter.run(task, this.callback);
  75. this.RateLimiter.run(task, this.callback);
  76. return this.RateLimiter.run(finalTask, err => {
  77. this.callback(err);
  78. return done();
  79. });
  80. });
  81. it("should execute the first three tasks", function() {
  82. return this.task.calledThrice.should.equal(true);
  83. });
  84. it("should execute the final task", function() {
  85. return this.finalTask.called.should.equal(true);
  86. });
  87. it("should call the callback", function() {
  88. return this.callback.called.should.equal(true);
  89. });
  90. return it("should finish with worker count of zero", function() {
  91. return expect(this.RateLimiter.ActiveWorkerCount).to.equal(0);
  92. });
  93. });
  94. return describe("for a mixture of long-running tasks", function() {
  95. beforeEach(function(done) {
  96. this.task = sinon.stub();
  97. this.finalTask = sinon.stub();
  98. const finalTask = cb => {
  99. this.finalTask();
  100. return setTimeout(cb, 100);
  101. };
  102. this.RateLimiter.run(this.task, this.callback);
  103. this.RateLimiter.run(this.task, this.callback);
  104. this.RateLimiter.run(this.task, this.callback);
  105. return this.RateLimiter.run(finalTask, err => {
  106. this.callback(err);
  107. return done();
  108. });
  109. });
  110. it("should execute the first three tasks", function() {
  111. return this.task.calledThrice.should.equal(true);
  112. });
  113. it("should execute the final task", function() {
  114. return this.finalTask.called.should.equal(true);
  115. });
  116. it("should call the callback", function() {
  117. return this.callback.called.should.equal(true);
  118. });
  119. return it("should finish with worker count of three", function() {
  120. return expect(this.RateLimiter.ActiveWorkerCount).to.equal(3);
  121. });
  122. });
  123. });