2020-06-23 17:30:03 +00:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-06-23 17:29:59 +00:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-06-23 17:30:16 +00:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const path = require('path')
|
|
|
|
const modulePath = path.join(__dirname, '../../../app/js/DrainManager')
|
2016-10-24 15:36:09 +00:00
|
|
|
|
2020-06-23 17:30:16 +00:00
|
|
|
describe('DrainManager', function () {
|
|
|
|
beforeEach(function () {
|
2021-03-18 20:19:31 +00:00
|
|
|
this.DrainManager = SandboxedModule.require(modulePath, {})
|
2020-06-23 17:30:16 +00:00
|
|
|
return (this.io = {
|
|
|
|
sockets: {
|
2021-07-13 11:04:45 +00:00
|
|
|
clients: sinon.stub(),
|
|
|
|
},
|
2020-06-23 17:30:16 +00:00
|
|
|
})
|
|
|
|
})
|
2016-10-24 15:36:09 +00:00
|
|
|
|
2020-06-23 17:30:16 +00:00
|
|
|
describe('startDrainTimeWindow', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.clients = []
|
|
|
|
for (let i = 0; i <= 5399; i++) {
|
|
|
|
this.clients[i] = {
|
|
|
|
id: i,
|
2021-07-13 11:04:45 +00:00
|
|
|
emit: sinon.stub(),
|
2020-06-23 17:30:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this.io.sockets.clients.returns(this.clients)
|
|
|
|
return (this.DrainManager.startDrain = sinon.stub())
|
|
|
|
})
|
2019-08-13 10:12:04 +00:00
|
|
|
|
2020-06-23 17:30:16 +00:00
|
|
|
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()
|
|
|
|
})
|
|
|
|
})
|
2019-08-13 10:12:04 +00:00
|
|
|
|
2020-06-23 17:30:16 +00:00
|
|
|
return describe('reconnectNClients', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.clients = []
|
|
|
|
for (let i = 0; i <= 9; i++) {
|
|
|
|
this.clients[i] = {
|
|
|
|
id: i,
|
2021-07-13 11:04:45 +00:00
|
|
|
emit: sinon.stub(),
|
2020-06-23 17:30:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.io.sockets.clients.returns(this.clients)
|
|
|
|
})
|
2019-08-13 10:12:04 +00:00
|
|
|
|
2020-06-23 17:30:16 +00:00
|
|
|
return describe('after first pass', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
return this.DrainManager.reconnectNClients(this.io, 3)
|
|
|
|
})
|
2016-10-24 15:36:09 +00:00
|
|
|
|
2020-06-23 17:30:16 +00:00
|
|
|
it('should reconnect the first 3 clients', function () {
|
2021-07-13 11:04:45 +00:00
|
|
|
return [0, 1, 2].map(i =>
|
2020-06-23 17:30:16 +00:00
|
|
|
this.clients[i].emit
|
|
|
|
.calledWith('reconnectGracefully')
|
|
|
|
.should.equal(true)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not reconnect any more clients', function () {
|
2021-07-13 11:04:45 +00:00
|
|
|
return [3, 4, 5, 6, 7, 8, 9].map(i =>
|
2020-06-23 17:30:16 +00:00
|
|
|
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 () {
|
2021-07-13 11:04:45 +00:00
|
|
|
return [3, 4, 5].map(i =>
|
2020-06-23 17:30:16 +00:00
|
|
|
this.clients[i].emit
|
|
|
|
.calledWith('reconnectGracefully')
|
|
|
|
.should.equal(true)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not reconnect any more clients', function () {
|
2021-07-13 11:04:45 +00:00
|
|
|
return [6, 7, 8, 9].map(i =>
|
2020-06-23 17:30:16 +00:00
|
|
|
this.clients[i].emit
|
|
|
|
.calledWith('reconnectGracefully')
|
|
|
|
.should.equal(false)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not reconnect the first 3 clients again', function () {
|
2021-07-13 11:04:45 +00:00
|
|
|
return [0, 1, 2].map(i =>
|
2020-06-23 17:30:16 +00:00
|
|
|
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 () {
|
2021-07-13 11:04:45 +00:00
|
|
|
return [0, 1, 2, 3, 4, 5].map(i =>
|
2020-06-23 17:30:16 +00:00
|
|
|
this.clients[i].emit.calledOnce.should.equal(true)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should log out that it reached the end', function () {
|
2021-09-14 08:36:24 +00:00
|
|
|
return this.logger.info
|
2020-06-23 17:30:16 +00:00
|
|
|
.calledWith('All clients have been told to reconnectGracefully')
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|