DrainManagerTests.js 3.7 KB

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