DrainManager.test.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Fix any style issues and re-enable lint.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS102: Remove unnecessary code created because of implicit returns
  6. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  7. */
  8. import sinon from 'sinon'
  9. import { describe, beforeEach, it } from 'vitest'
  10. import path from 'node:path'
  11. const modulePath = path.join(
  12. import.meta.dirname,
  13. '../../../app/js/DrainManager'
  14. )
  15. describe('DrainManager', function () {
  16. beforeEach(async function (ctx) {
  17. ctx.DrainManager = (await import(modulePath)).default
  18. ctx.io = {
  19. sockets: {
  20. clients: sinon.stub(),
  21. },
  22. }
  23. })
  24. describe('startDrainTimeWindow', function () {
  25. beforeEach(function (ctx) {
  26. ctx.clients = []
  27. for (let i = 0; i <= 5399; i++) {
  28. ctx.clients[i] = {
  29. id: i,
  30. emit: sinon.stub(),
  31. }
  32. }
  33. ctx.io.sockets.clients.returns(ctx.clients)
  34. ctx.DrainManager.startDrain = sinon.stub()
  35. })
  36. it('should set a drain rate fast enough', async function (ctx) {
  37. await new Promise((resolve, reject) => {
  38. ctx.DrainManager.startDrainTimeWindow(ctx.io, 9)
  39. ctx.DrainManager.startDrain.calledWith(ctx.io, 10).should.equal(true)
  40. resolve()
  41. })
  42. })
  43. })
  44. describe('reconnectNClients', function () {
  45. beforeEach(function (ctx) {
  46. ctx.clients = []
  47. for (let i = 0; i <= 9; i++) {
  48. ctx.clients[i] = {
  49. id: i,
  50. emit: sinon.stub(),
  51. }
  52. }
  53. ctx.io.sockets.clients.returns(ctx.clients)
  54. })
  55. describe('after first pass', function () {
  56. beforeEach(function (ctx) {
  57. ctx.DrainManager.reconnectNClients(ctx.io, 3)
  58. })
  59. it('should reconnect the first 3 clients', function (ctx) {
  60. ;[0, 1, 2].map(i =>
  61. ctx.clients[i].emit
  62. .calledWith('reconnectGracefully')
  63. .should.equal(true)
  64. )
  65. })
  66. it('should not reconnect any more clients', function (ctx) {
  67. ;[3, 4, 5, 6, 7, 8, 9].map(i =>
  68. ctx.clients[i].emit
  69. .calledWith('reconnectGracefully')
  70. .should.equal(false)
  71. )
  72. })
  73. describe('after second pass', function () {
  74. beforeEach(function (ctx) {
  75. ctx.DrainManager.reconnectNClients(ctx.io, 3)
  76. })
  77. it('should reconnect the next 3 clients', function (ctx) {
  78. ;[3, 4, 5].map(i =>
  79. ctx.clients[i].emit
  80. .calledWith('reconnectGracefully')
  81. .should.equal(true)
  82. )
  83. })
  84. it('should not reconnect any more clients', function (ctx) {
  85. ;[6, 7, 8, 9].map(i =>
  86. ctx.clients[i].emit
  87. .calledWith('reconnectGracefully')
  88. .should.equal(false)
  89. )
  90. })
  91. it('should not reconnect the first 3 clients again', function (ctx) {
  92. ;[0, 1, 2].map(i => ctx.clients[i].emit.calledOnce.should.equal(true))
  93. })
  94. describe('after final pass', function () {
  95. beforeEach(function (ctx) {
  96. ctx.DrainManager.reconnectNClients(ctx.io, 100)
  97. })
  98. it('should not reconnect the first 6 clients again', function (ctx) {
  99. ;[0, 1, 2, 3, 4, 5].map(i =>
  100. ctx.clients[i].emit.calledOnce.should.equal(true)
  101. )
  102. })
  103. it('should log out that it reached the end', function (ctx) {
  104. ctx.logger.info
  105. .calledWith('All clients have been told to reconnectGracefully')
  106. .should.equal(true)
  107. })
  108. })
  109. })
  110. })
  111. })
  112. })