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

Commit d6a86a7

Browse files
committed
add test cases
Change-Id: Ib1920981402d409eae315cbf77488f0b0c0eb8cd
1 parent 0a0b54f commit d6a86a7

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

gruntfile.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ module.exports = function(grunt) {
77
grunt.loadNpmTasks('grunt-contrib-jshint');
88

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

1212
// Project configuration.
1313
grunt.initConfig({
1414
mochaTest: {
1515
test: {
1616
options: {
1717
reporter: 'spec',
18+
timeout: 5000,
1819
require: 'coverage/blanket'
1920
},
2021
src: src

test/modules/console.js

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ describe('console module test', function() {
99
var rs;
1010
var opts = {
1111
app: {
12+
components: {
13+
__connector__: {
14+
blacklist: []
15+
}
16+
},
1217
stop: function(value) {
1318
flag = value;
1419
},
@@ -17,6 +22,9 @@ describe('console module test', function() {
1722
},
1823
removeCrons: function(array) {
1924
rs = array;
25+
},
26+
isFrontend: function() {
27+
return true;
2028
}
2129
}
2230
};
@@ -45,6 +53,12 @@ describe('console module test', function() {
4553
var msg4 = {signal: 'removeCron'};
4654
module.monitorHandler(agent2, msg4, null);
4755
rs.length.should.eql(1);
56+
57+
var msg5 = {signal: 'blacklist', blacklist: ['127.0.0.1']};
58+
module.monitorHandler(agent1, msg5, null);
59+
opts.app.components.__connector__.blacklist.length.should.eql(1);
60+
61+
4862
});
4963
});
5064

@@ -93,6 +107,9 @@ describe('console module test', function() {
93107
},
94108
set: function(value) {
95109
return value;
110+
},
111+
getServersByType: function() {
112+
return [{id: 'chat-server-1'}];
96113
}
97114
}
98115
};
@@ -138,7 +155,6 @@ describe('console module test', function() {
138155
should.not.exist(err);
139156
should.exist(result.code);
140157
result.code.should.eql('remained');
141-
exitCount.should.greaterThan(0);
142158
done();
143159
});
144160
});
@@ -163,6 +179,7 @@ describe('console module test', function() {
163179
done();
164180
});
165181
});
182+
166183
it('should execute list command', function() {
167184
var msg = {signal: 'list'};
168185
var agent = {
@@ -180,6 +197,7 @@ describe('console module test', function() {
180197
should.exist(result.msg);
181198
});
182199
});
200+
183201
it('should execute add command', function() {
184202
var msg1 = {signal: 'add', args: ['host=127.0.0.1', 'port=88888', 'clusterCount=2']};
185203
var msg2 = {signal: 'add', args: ['host=127.0.0.1', 'port=88888', 'id=chat-server-1', 'serverType=chat']};
@@ -192,5 +210,39 @@ describe('console module test', function() {
192210
result.status.should.eql('ok');
193211
});
194212
});
213+
214+
it('should execute blacklist command', function() {
215+
var msg1 = {signal: 'blacklist', args: ['127.0.0.1']};
216+
var msg2 = {signal: 'blacklist', args: ['abc']};
217+
var agent = {
218+
notifyAll: function(moduleId, msg) {
219+
220+
}
221+
};
222+
module.clientHandler(agent, msg1, function(err, result) {
223+
result.status.should.eql('ok');
224+
});
225+
module.clientHandler(agent, msg2, function(err, result) {
226+
should.exist(err);
227+
});
228+
});
229+
230+
it('should execute restart command', function() {
231+
var msg1 = {signal: 'restart', ids:['chat-server-1']};
232+
var msg2 = {signal: 'restart', type:'chat', ids:[]};
233+
var agent = {
234+
request: function(recordId, moduleId, msg, cb) {
235+
cb(null);
236+
}
237+
};
238+
module.clientHandler(agent, msg1, function(err, result) {
239+
should.exist(err);
240+
});
241+
module.clientHandler(agent, msg2, function(err, result) {
242+
should.exist(err);
243+
});
244+
245+
});
246+
195247
});
196248
});

test/util/utils.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,10 @@ describe('utils test', function() {
108108
it('should return true if the ip is local', function() {
109109
var ip = '127.0.0.1';
110110
var host = 'localhost';
111+
var other = '192.168.1.1';
111112
utils.isLocal(ip).should.be.true;
112113
utils.isLocal(host).should.be.true;
114+
utils.isLocal(other).should.be.false;
113115
});
114116
});
115117

@@ -123,4 +125,40 @@ describe('utils test', function() {
123125
});
124126
});
125127

128+
describe('#arrayDiff', function() {
129+
it('should return the difference of two arrays', function() {
130+
var array1 = [1, 2, 3, 4, 5];
131+
var array2 = [1, 2, 3];
132+
var array = utils.arrayDiff(array1, array2);
133+
array.should.eql([4, 5]);
134+
});
135+
});
136+
137+
describe('#extends', function() {
138+
it('should extends opts', function() {
139+
var opts = {
140+
test: 123
141+
};
142+
var add = {
143+
aaa: 555
144+
};
145+
var result = utils.extends(opts, add);
146+
result.should.eql({
147+
test: 123,
148+
aaa: 555
149+
});
150+
});
151+
});
152+
153+
describe('#ping', function() {
154+
it('should ping server', function() {
155+
utils.ping('127.0.0.1', function(flag) {
156+
flag.should.be.true;
157+
});
158+
utils.ping('111.111.111.111', function(flag) {
159+
flag.should.be.false;
160+
});
161+
});
162+
});
163+
126164
});

0 commit comments

Comments
 (0)