DrainManagerTests.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const should = require('chai').should()
  13. const sinon = require('sinon')
  14. const SandboxedModule = require('sandboxed-module')
  15. const path = require('path')
  16. const modulePath = path.join(__dirname, '../../../app/js/DrainManager')
  17. describe('DrainManager', function () {
  18. beforeEach(function () {
  19. this.DrainManager = SandboxedModule.require(modulePath, {
  20. requires: {
  21. 'logger-sharelatex': (this.logger = { log: sinon.stub() })
  22. }
  23. })
  24. return (this.io = {
  25. sockets: {
  26. clients: sinon.stub()
  27. }
  28. })
  29. })
  30. describe('startDrainTimeWindow', function () {
  31. beforeEach(function () {
  32. this.clients = []
  33. for (let i = 0; i <= 5399; i++) {
  34. this.clients[i] = {
  35. id: i,
  36. emit: sinon.stub()
  37. }
  38. }
  39. this.io.sockets.clients.returns(this.clients)
  40. return (this.DrainManager.startDrain = sinon.stub())
  41. })
  42. return it('should set a drain rate fast enough', function (done) {
  43. this.DrainManager.startDrainTimeWindow(this.io, 9)
  44. this.DrainManager.startDrain.calledWith(this.io, 10).should.equal(true)
  45. return done()
  46. })
  47. })
  48. return describe('reconnectNClients', function () {
  49. beforeEach(function () {
  50. this.clients = []
  51. for (let i = 0; i <= 9; i++) {
  52. this.clients[i] = {
  53. id: i,
  54. emit: sinon.stub()
  55. }
  56. }
  57. return this.io.sockets.clients.returns(this.clients)
  58. })
  59. return describe('after first pass', function () {
  60. beforeEach(function () {
  61. return this.DrainManager.reconnectNClients(this.io, 3)
  62. })
  63. it('should reconnect the first 3 clients', function () {
  64. return [0, 1, 2].map((i) =>
  65. this.clients[i].emit
  66. .calledWith('reconnectGracefully')
  67. .should.equal(true)
  68. )
  69. })
  70. it('should not reconnect any more clients', function () {
  71. return [3, 4, 5, 6, 7, 8, 9].map((i) =>
  72. this.clients[i].emit
  73. .calledWith('reconnectGracefully')
  74. .should.equal(false)
  75. )
  76. })
  77. return describe('after second pass', function () {
  78. beforeEach(function () {
  79. return this.DrainManager.reconnectNClients(this.io, 3)
  80. })
  81. it('should reconnect the next 3 clients', function () {
  82. return [3, 4, 5].map((i) =>
  83. this.clients[i].emit
  84. .calledWith('reconnectGracefully')
  85. .should.equal(true)
  86. )
  87. })
  88. it('should not reconnect any more clients', function () {
  89. return [6, 7, 8, 9].map((i) =>
  90. this.clients[i].emit
  91. .calledWith('reconnectGracefully')
  92. .should.equal(false)
  93. )
  94. })
  95. it('should not reconnect the first 3 clients again', function () {
  96. return [0, 1, 2].map((i) =>
  97. this.clients[i].emit.calledOnce.should.equal(true)
  98. )
  99. })
  100. return describe('after final pass', function () {
  101. beforeEach(function () {
  102. return this.DrainManager.reconnectNClients(this.io, 100)
  103. })
  104. it('should not reconnect the first 6 clients again', function () {
  105. return [0, 1, 2, 3, 4, 5].map((i) =>
  106. this.clients[i].emit.calledOnce.should.equal(true)
  107. )
  108. })
  109. return it('should log out that it reached the end', function () {
  110. return this.logger.log
  111. .calledWith('All clients have been told to reconnectGracefully')
  112. .should.equal(true)
  113. })
  114. })
  115. })
  116. })
  117. })
  118. })