Skip to content

Commit

Permalink
[minor] Adding missing end event.
Browse files Browse the repository at this point in the history
[minor] Adding missing join and leave events.
[fix] The leader should be string by default so types will match.
[test] Adding test for node joins
  • Loading branch information
3rd-Eden committed Oct 31, 2014
1 parent 8e2e296 commit 932a5ee
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function Node(options) {
// this state until receive a message from a Leader or Candidate.
//
this.state = Node.FOLLOWER; // Our current state.
this.leader = null; // Leader in our cluster.
this.leader = ''; // Leader in our cluster.
this.term = 0; // Our current term.

this.initialize(options);
Expand Down Expand Up @@ -507,6 +507,7 @@ Node.prototype.join = function join(name, write, fn) {

var node = this.clone({ name: name });
this.nodes.push(node);
this.emit('join', node);

return node;
};
Expand All @@ -532,6 +533,7 @@ Node.prototype.leave = function leave(name) {
if (~index && node) {
if (node.end) node.end();
this.nodes.splice(index, 1);
this.emit('leave', node);
}

return node;
Expand All @@ -550,6 +552,7 @@ Node.prototype.end = function end() {
this.leave(this.nodes[i]);
}

this.emit('end');
this.timers.end();
this.removeAllListeners();

Expand Down
76 changes: 73 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,30 +152,50 @@ describe('liferaft', function () {
assume(raft.end()).to.equal(true);
assume(listeners(raft)).equals(0);
});

it('emits an end event', function (next) {
raft.on('end', next);

//
// Double end is here to check if the event is not executed multiple
// times.
//
raft.end();
raft.end();
});
});

describe('#change', function () {
it('updates the term and emits a change', function (next) {
raft.once('term change', function () {
raft.once('term change', function (currently, previously) {
assume(currently).equals(raft.term);
assume(previously).equals(0);
assume(raft.term).equals(3);

next();
});

raft.change({ term: 3 });
});

it('updates the leader and emits a change', function (next) {
raft.once('leader change', function () {
raft.once('leader change', function (currently, previously) {
assume(currently).equals(raft.leader);
assume(raft.leader).equals('foo');
assume(previously).equals('');

next();
});

raft.change({ leader: 'foo' });
});

it('updates the state and emits a change', function (next) {
raft.once('state change', function () {
raft.once('state change', function (currently, previously) {
assume(previously).equals(Raft.FOLLOWER);
assume(raft.state).equals(Raft.LEADER);
assume(currently).equals(raft.state);

next();
});

Expand Down Expand Up @@ -357,6 +377,56 @@ describe('liferaft', function () {
punk.end();
draft.end();
});

it('allows overriding of config through options', function () {
raft.end();

var Draft = Raft.extend({ write: function () { } })
, draft = new Draft({ threshold: 99 })
, punk = draft.clone({ threshold: 9 });

assume(punk.threshold).equals(9);

punk.end();
draft.end();
});
});

describe('#join', function () {
it('returns the node we added', function () {
var node = raft.join();

assume(node.name).does.not.equal(raft.name);
assume(node).does.not.equal(raft);
assume(node).is.instanceOf(Raft);

node.end();
});

it('returns the same instance as the node', function () {
raft.end();

var Draft = Raft.extend({ write: function () { } })
, draft = new Draft({ threshold: 99 })
, punk = draft.join('foo');

assume(punk).is.instanceOf(Draft);
assume(punk).is.instanceOf(Raft);
assume(punk).does.not.equal(draft);

punk.end();
draft.end();
});

it('allows setting of node with a custom name', function () {
var node = raft.join('foo');

assume(node).does.not.equal(raft);
assume(node).is.instanceOf(Raft);
assume(node.name).equals('foo');

node.end();
});
});

describe('event', function () {
Expand Down

0 comments on commit 932a5ee

Please sign in to comment.