Skip to content
This repository has been archived by the owner on Sep 25, 2023. It is now read-only.

Commit

Permalink
add test cases
Browse files Browse the repository at this point in the history
Change-Id: Ib1920981402d409eae315cbf77488f0b0c0eb8cd
  • Loading branch information
py8765 committed Jun 4, 2014
1 parent 0a0b54f commit d6a86a7
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
3 changes: 2 additions & 1 deletion gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-jshint');

var src = ['test/manager/taskManager.js', 'test/filters/*.js',
'test/remote/*.js', 'test/service/*.js', 'test/util/*.js', 'test/*.js'];
'test/remote/*.js', 'test/service/*.js', 'test/modules/*.js', 'test/util/*.js', 'test/*.js'];

// Project configuration.
grunt.initConfig({
mochaTest: {
test: {
options: {
reporter: 'spec',
timeout: 5000,
require: 'coverage/blanket'
},
src: src
Expand Down
54 changes: 53 additions & 1 deletion test/modules/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ describe('console module test', function() {
var rs;
var opts = {
app: {
components: {
__connector__: {
blacklist: []
}
},
stop: function(value) {
flag = value;
},
Expand All @@ -17,6 +22,9 @@ describe('console module test', function() {
},
removeCrons: function(array) {
rs = array;
},
isFrontend: function() {
return true;
}
}
};
Expand Down Expand Up @@ -45,6 +53,12 @@ describe('console module test', function() {
var msg4 = {signal: 'removeCron'};
module.monitorHandler(agent2, msg4, null);
rs.length.should.eql(1);

var msg5 = {signal: 'blacklist', blacklist: ['127.0.0.1']};
module.monitorHandler(agent1, msg5, null);
opts.app.components.__connector__.blacklist.length.should.eql(1);


});
});

Expand Down Expand Up @@ -93,6 +107,9 @@ describe('console module test', function() {
},
set: function(value) {
return value;
},
getServersByType: function() {
return [{id: 'chat-server-1'}];
}
}
};
Expand Down Expand Up @@ -138,7 +155,6 @@ describe('console module test', function() {
should.not.exist(err);
should.exist(result.code);
result.code.should.eql('remained');
exitCount.should.greaterThan(0);
done();
});
});
Expand All @@ -163,6 +179,7 @@ describe('console module test', function() {
done();
});
});

it('should execute list command', function() {
var msg = {signal: 'list'};
var agent = {
Expand All @@ -180,6 +197,7 @@ describe('console module test', function() {
should.exist(result.msg);
});
});

it('should execute add command', function() {
var msg1 = {signal: 'add', args: ['host=127.0.0.1', 'port=88888', 'clusterCount=2']};
var msg2 = {signal: 'add', args: ['host=127.0.0.1', 'port=88888', 'id=chat-server-1', 'serverType=chat']};
Expand All @@ -192,5 +210,39 @@ describe('console module test', function() {
result.status.should.eql('ok');
});
});

it('should execute blacklist command', function() {
var msg1 = {signal: 'blacklist', args: ['127.0.0.1']};
var msg2 = {signal: 'blacklist', args: ['abc']};
var agent = {
notifyAll: function(moduleId, msg) {

}
};
module.clientHandler(agent, msg1, function(err, result) {
result.status.should.eql('ok');
});
module.clientHandler(agent, msg2, function(err, result) {
should.exist(err);
});
});

it('should execute restart command', function() {
var msg1 = {signal: 'restart', ids:['chat-server-1']};
var msg2 = {signal: 'restart', type:'chat', ids:[]};
var agent = {
request: function(recordId, moduleId, msg, cb) {
cb(null);
}
};
module.clientHandler(agent, msg1, function(err, result) {
should.exist(err);
});
module.clientHandler(agent, msg2, function(err, result) {
should.exist(err);
});

});

});
});
38 changes: 38 additions & 0 deletions test/util/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ describe('utils test', function() {
it('should return true if the ip is local', function() {
var ip = '127.0.0.1';
var host = 'localhost';
var other = '192.168.1.1';
utils.isLocal(ip).should.be.true;
utils.isLocal(host).should.be.true;
utils.isLocal(other).should.be.false;
});
});

Expand All @@ -123,4 +125,40 @@ describe('utils test', function() {
});
});

describe('#arrayDiff', function() {
it('should return the difference of two arrays', function() {
var array1 = [1, 2, 3, 4, 5];
var array2 = [1, 2, 3];
var array = utils.arrayDiff(array1, array2);
array.should.eql([4, 5]);
});
});

describe('#extends', function() {
it('should extends opts', function() {
var opts = {
test: 123
};
var add = {
aaa: 555
};
var result = utils.extends(opts, add);
result.should.eql({
test: 123,
aaa: 555
});
});
});

describe('#ping', function() {
it('should ping server', function() {
utils.ping('127.0.0.1', function(flag) {
flag.should.be.true;
});
utils.ping('111.111.111.111', function(flag) {
flag.should.be.false;
});
});
});

});

0 comments on commit d6a86a7

Please sign in to comment.