2020-06-23 13:30:03 -04: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 13:29:59 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
const should = require('chai').should();
|
|
|
|
const sinon = require("sinon");
|
|
|
|
const SandboxedModule = require('sandboxed-module');
|
|
|
|
const path = require("path");
|
|
|
|
const modulePath = path.join(__dirname, "../../../app/js/DrainManager");
|
2016-10-24 11:36:09 -04:00
|
|
|
|
2020-06-23 13:29:59 -04:00
|
|
|
describe("DrainManager", function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.DrainManager = SandboxedModule.require(modulePath, { requires: {
|
|
|
|
"logger-sharelatex": (this.logger = {log: sinon.stub()})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return this.io = {
|
|
|
|
sockets: {
|
2016-10-24 11:36:09 -04:00
|
|
|
clients: sinon.stub()
|
2020-06-23 13:29:59 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
2016-10-24 11:36:09 -04:00
|
|
|
|
2020-06-23 13:29:59 -04:00
|
|
|
describe("startDrainTimeWindow", function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.clients = [];
|
|
|
|
for (let i = 0; i <= 5399; i++) {
|
|
|
|
this.clients[i] = {
|
|
|
|
id: i,
|
2019-08-13 06:12:04 -04:00
|
|
|
emit: sinon.stub()
|
2020-06-23 13:29:59 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
this.io.sockets.clients.returns(this.clients);
|
|
|
|
return this.DrainManager.startDrain = sinon.stub();
|
|
|
|
});
|
2019-08-13 06:12:04 -04:00
|
|
|
|
2020-06-23 13:29:59 -04: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 06:12:04 -04:00
|
|
|
|
|
|
|
|
2020-06-23 13:29:59 -04:00
|
|
|
return describe("reconnectNClients", function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.clients = [];
|
|
|
|
for (let i = 0; i <= 9; i++) {
|
|
|
|
this.clients[i] = {
|
|
|
|
id: i,
|
2016-10-24 11:36:09 -04:00
|
|
|
emit: sinon.stub()
|
2020-06-23 13:29:59 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return this.io.sockets.clients.returns(this.clients);
|
|
|
|
});
|
2016-10-24 11:36:09 -04:00
|
|
|
|
2020-06-23 13:29:59 -04:00
|
|
|
return describe("after first pass", function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
return this.DrainManager.reconnectNClients(this.io, 3);
|
|
|
|
});
|
2016-10-24 11:36:09 -04:00
|
|
|
|
2020-06-23 13:29:59 -04:00
|
|
|
it("should reconnect the first 3 clients", function() {
|
|
|
|
return [0, 1, 2].map((i) =>
|
|
|
|
this.clients[i].emit.calledWith("reconnectGracefully").should.equal(true));
|
|
|
|
});
|
2016-10-24 11:36:09 -04:00
|
|
|
|
2020-06-23 13:29:59 -04:00
|
|
|
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));
|
|
|
|
});
|
2016-10-24 11:36:09 -04:00
|
|
|
|
2020-06-23 13:29:59 -04:00
|
|
|
return describe("after second pass", function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
return this.DrainManager.reconnectNClients(this.io, 3);
|
|
|
|
});
|
2016-10-24 11:36:09 -04:00
|
|
|
|
2020-06-23 13:29:59 -04:00
|
|
|
it("should reconnect the next 3 clients", function() {
|
|
|
|
return [3, 4, 5].map((i) =>
|
|
|
|
this.clients[i].emit.calledWith("reconnectGracefully").should.equal(true));
|
|
|
|
});
|
2016-10-24 11:36:09 -04:00
|
|
|
|
2020-06-23 13:29:59 -04:00
|
|
|
it("should not reconnect any more clients", function() {
|
|
|
|
return [6, 7, 8, 9].map((i) =>
|
|
|
|
this.clients[i].emit.calledWith("reconnectGracefully").should.equal(false));
|
|
|
|
});
|
2016-10-24 11:36:09 -04:00
|
|
|
|
2020-06-23 13:29:59 -04:00
|
|
|
it("should not reconnect the first 3 clients again", function() {
|
|
|
|
return [0, 1, 2].map((i) =>
|
|
|
|
this.clients[i].emit.calledOnce.should.equal(true));
|
|
|
|
});
|
2016-10-24 11:36:09 -04:00
|
|
|
|
2020-06-23 13:29:59 -04:00
|
|
|
return describe("after final pass", function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
return this.DrainManager.reconnectNClients(this.io, 100);
|
|
|
|
});
|
2016-10-24 11:36:09 -04:00
|
|
|
|
2020-06-23 13:29:59 -04:00
|
|
|
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));
|
|
|
|
});
|
2016-10-24 11:36:09 -04:00
|
|
|
|
2020-06-23 13:29:59 -04:00
|
|
|
return it("should log out that it reached the end", function() {
|
|
|
|
return this.logger.log
|
2016-10-24 11:36:09 -04:00
|
|
|
.calledWith("All clients have been told to reconnectGracefully")
|
2020-06-23 13:29:59 -04:00
|
|
|
.should.equal(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|