Skip to content

Commit

Permalink
Replace nodeunit with mocha
Browse files Browse the repository at this point in the history
  • Loading branch information
dakatsuka committed Jun 18, 2012
1 parent 408441c commit ab8f742
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 42 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "Dai Akatsuka <d.akatsuka@gmail.com>", "author": "Dai Akatsuka <d.akatsuka@gmail.com>",
"main": "./index.js", "main": "./index.js",
"scripts": { "scripts": {
"test": "nodeunit ./test/consistent_hashing.test.js" "test": "mocha -R spec"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
Expand All @@ -22,6 +22,7 @@
} }
], ],
"devDependencies": { "devDependencies": {
"nodeunit": "0.5.3" "mocha": "1.1.0",
"should": "0.6.3"
} }
} }
80 changes: 40 additions & 40 deletions test/consistent_hashing.test.js
Original file line number Original file line Diff line number Diff line change
@@ -1,10 +1,10 @@
var testCase = require('nodeunit').testCase, var should = require('should');
ConsistentHashing = require('..'); var ConsistentHashing = require('../');


var nodes = ["node1", "node2", "node3"]; describe('ConsistentHashing', function() {
var nodes = ["node1", "node2", "node3"];


module.exports = testCase({ before(function(done) {
setUp: function(callback) {
Array.prototype.contains = function(value) { Array.prototype.contains = function(value) {
for (var i in this) { for (var i in this) {
if (this.hasOwnProperty(i) && this[i] == value) { if (this.hasOwnProperty(i) && this[i] == value) {
Expand All @@ -13,55 +13,55 @@ module.exports = testCase({
} }
return false; return false;
}; };
callback(); done();
}, });


'should be set default values': function(test) { it('should be set default values', function(done) {
var cons = new ConsistentHashing(nodes); var cons = new ConsistentHashing(nodes);
test.equal(cons.replicas, 160); cons.replicas.should.equal(160);
test.equal(cons.algorithm, 'md5'); cons.algorithm.should.equal('md5');
test.done(); done();
}, });


'should be able to change value of replicas': function(test) { it('should be able to change value of replicas', function(done) {
var cons = new ConsistentHashing(nodes, { replicas: 300 }); var cons = new ConsistentHashing(nodes, { replicas: 300 });
test.equal(cons.replicas, 300); cons.replicas.should.equal(300);
test.done(); done();
}, });


'should be able to change algorithm': function(test) { it('should be able to change algorithm', function(done) {
var cons = new ConsistentHashing(nodes, { algorithm: 'sha1' }); var cons = new ConsistentHashing(nodes, { algorithm: 'sha1' });
test.equal(cons.algorithm, 'sha1'); cons.algorithm.should.equal('sha1');
test.done(); done();
}, });


'should add nodes': function(test) { it('should add nodes', function(done) {
var cons = new ConsistentHashing(nodes); var cons = new ConsistentHashing(nodes);
cons.addNode("node4"); cons.addNode("node4");
test.equal(cons.nodes.contains("node4"), true); cons.nodes.contains("node4").should.be.true;
test.equal(cons.keys.length, cons.replicas * 4); cons.keys.length.should.equal(cons.replicas * 4);
test.done(); done();
}, });


'should remove node': function(test) { it('should remove node', function(done) {
var cons = new ConsistentHashing(nodes); var cons = new ConsistentHashing(nodes);
cons.removeNode("node1"); cons.removeNode("node1");
test.equal(cons.nodes.contains("node1"), false); cons.nodes.contains("node1").should.be.false;
test.equal(cons.keys.length, cons.replicas * 2); cons.keys.length.should.equal(cons.replicas * 2);
test.done(); done();
}, });


'should create an array for sort': function(test) { it('should create an array for sort', function(done) {
var cons = new ConsistentHashing(nodes); var cons = new ConsistentHashing(nodes);
test.equal(cons.keys.length, cons.replicas * nodes.length); cons.keys.length.should.equal(cons.replicas * nodes.length);
test.done(); done();
}, });


'should get a node from key': function(test) { it('should get a node from key', function(done) {
var cons = new ConsistentHashing(nodes); var cons = new ConsistentHashing(nodes);
test.equal(cons.getNode('A'), "node3"); cons.getNode('A').should.equal("node3");
test.equal(cons.getNode('B'), "node1"); cons.getNode('B').should.equal("node1");
test.equal(cons.getNode('C'), "node1"); cons.getNode('C').should.equal("node1");
test.done(); done();
} });
}); });

0 comments on commit ab8f742

Please sign in to comment.