overleaf/services/real-time/test/unit/js/DrainManagerTests.js
Antoine Clausse 7f48c67512 Add prefer-node-protocol ESLint rule (#21532)
* Add `unicorn/prefer-node-protocol`

* Fix `unicorn/prefer-node-protocol` ESLint errors

* Run `npm run format:fix`

* Add sandboxed-module sourceTransformers in mocha setups

Fix `no such file or directory, open 'node:fs'` in `sandboxed-module`

* Remove `node:` in the SandboxedModule requires

* Fix new linting errors with `node:`

GitOrigin-RevId: 68f6e31e2191fcff4cb8058dd0a6914c14f59926
2024-11-11 09:04:51 +00:00

127 lines
3.7 KiB
JavaScript

/* eslint-disable
no-return-assign,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const sinon = require('sinon')
const SandboxedModule = require('sandboxed-module')
const path = require('node:path')
const modulePath = path.join(__dirname, '../../../app/js/DrainManager')
describe('DrainManager', function () {
beforeEach(function () {
this.DrainManager = SandboxedModule.require(modulePath, {})
return (this.io = {
sockets: {
clients: sinon.stub(),
},
})
})
describe('startDrainTimeWindow', function () {
beforeEach(function () {
this.clients = []
for (let i = 0; i <= 5399; i++) {
this.clients[i] = {
id: i,
emit: sinon.stub(),
}
}
this.io.sockets.clients.returns(this.clients)
return (this.DrainManager.startDrain = sinon.stub())
})
return it('should set a drain rate fast enough', function (done) {
this.DrainManager.startDrainTimeWindow(this.io, 9)
this.DrainManager.startDrain.calledWith(this.io, 10).should.equal(true)
return done()
})
})
return describe('reconnectNClients', function () {
beforeEach(function () {
this.clients = []
for (let i = 0; i <= 9; i++) {
this.clients[i] = {
id: i,
emit: sinon.stub(),
}
}
return this.io.sockets.clients.returns(this.clients)
})
return describe('after first pass', function () {
beforeEach(function () {
return this.DrainManager.reconnectNClients(this.io, 3)
})
it('should reconnect the first 3 clients', function () {
return [0, 1, 2].map(i =>
this.clients[i].emit
.calledWith('reconnectGracefully')
.should.equal(true)
)
})
it('should not reconnect any more clients', function () {
return [3, 4, 5, 6, 7, 8, 9].map(i =>
this.clients[i].emit
.calledWith('reconnectGracefully')
.should.equal(false)
)
})
return describe('after second pass', function () {
beforeEach(function () {
return this.DrainManager.reconnectNClients(this.io, 3)
})
it('should reconnect the next 3 clients', function () {
return [3, 4, 5].map(i =>
this.clients[i].emit
.calledWith('reconnectGracefully')
.should.equal(true)
)
})
it('should not reconnect any more clients', function () {
return [6, 7, 8, 9].map(i =>
this.clients[i].emit
.calledWith('reconnectGracefully')
.should.equal(false)
)
})
it('should not reconnect the first 3 clients again', function () {
return [0, 1, 2].map(i =>
this.clients[i].emit.calledOnce.should.equal(true)
)
})
return describe('after final pass', function () {
beforeEach(function () {
return this.DrainManager.reconnectNClients(this.io, 100)
})
it('should not reconnect the first 6 clients again', function () {
return [0, 1, 2, 3, 4, 5].map(i =>
this.clients[i].emit.calledOnce.should.equal(true)
)
})
return it('should log out that it reached the end', function () {
return this.logger.info
.calledWith('All clients have been told to reconnectGracefully')
.should.equal(true)
})
})
})
})
})
})