Added timeout to leadership election

This commit is contained in:
Chris Blanchard 2015-07-31 16:06:21 +01:00
parent 6f5e927f70
commit cbc486de77
3 changed files with 16 additions and 9 deletions

View file

@ -25,8 +25,8 @@ var Gather = require("./gather");
var gather = new Gather();
// ***** Generate Test Users *****
// var helper = require("./helper");
// helper.createTestUsers({ gather: gather });
var helper = require("./helper");
helper.createTestUsers({ gather: gather });
module.exports = function (namespace) {
var refreshGather = function () {

View file

@ -14,10 +14,15 @@
var Gatherer = require("./gatherer");
var StateMachine = require("javascript-state-machine");
function Gather () {
function Gather (options) {
if (!(this instanceof Gather)) {
return new Gather();
return new Gather(options);
}
if (typeof options.onElectionTimeout === 'function') {
this.onElectionTimeout = options.onElectionTimeout;
}
this.TEAM_SIZE = 6;
this.gatherers = [];
this.ELECTION_INTERVAL = 10000; // 10 Seconds (temporarily)

View file

@ -45,6 +45,13 @@ describe("Gather Model:", function () {
describe("Election Tmimeout", function () {
it ("starts a timer and transitions to next state when timer runs out", function (done) {
gather = new Gather({
onElectionTimeout: function () {
assert.equal(gather.current, "selection");
assert.isNull(gather.electionStartTime);
done();
}
});
gather.ELECTION_INTERVAL = 100; // 1 second
assert.isNull(gather.electionStartTime);
gatherers.forEach(function (gatherer) {
@ -52,11 +59,6 @@ describe("Gather Model:", function () {
});
assert.equal(gather.current, "election");
assert.isNotNull(gather.electionStartTime);
setTimeout(function () {
assert.equal(gather.current, "selection");
assert.isNull(gather.electionStartTime);
done();
}, 200);
});
});