forked from NetEase/pomelo-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole-service-test.js
executable file
·302 lines (259 loc) · 6.89 KB
/
console-service-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
var should = require('should');
var flow = require('flow');
var ConsoleService = require('..');
var WAIT_TIME = 100;
var masterHost = '127.0.0.1';
var masterPort = 3333;
describe('console service', function() {
it('should forward message from master to the monitorHandler method of the module of the right monitor, and get the response by masterAgent.request', function(done) {
var monitorId1 = 'connector-server-1';
var monitorId2 = 'area-server-1';
var monitorType1 = 'connector';
var monitorType2 = 'area';
var moduleId1 = 'testModuleId1';
var moduleId2 = 'testModuleId2';
var msg1 = {msg: 'message to monitor1'};
var msg2 = {msg: 'message to monitor2'};
var req1Count = 0;
var req2Count = 0;
var resp1Count = 0;
var resp2Count = 0;
var masterConsole = ConsoleService.createMasterConsole({
port: masterPort
});
var monitorConsole1 = ConsoleService.createMonitorConsole({
host: masterHost,
port: masterPort,
id: monitorId1,
type: monitorType1
});
monitorConsole1.register(moduleId1, {
monitorHandler: function(agent, msg, cb) {
req1Count++;
should.exist(msg);
msg.should.eql(msg1);
cb(null, msg);
}
});
var monitorConsole2 = ConsoleService.createMonitorConsole({
host: masterHost,
port: masterPort,
id: monitorId2,
type: monitorType2
});
monitorConsole2.register(moduleId2, {
monitorHandler: function(agent, msg, cb) {
req2Count++;
should.exist(msg);
msg.should.eql(msg2);
cb(null, msg);
}
});
flow.exec(function() {
masterConsole.start(this);
},
function(err) {
should.not.exist(err);
monitorConsole1.start(this);
},
function(err) {
should.not.exist(err);
monitorConsole2.start(this);
},
function(err) {
should.not.exist(err);
masterConsole.agent.request(monitorId1, moduleId1, msg1, function(err, resp) {
resp1Count++;
should.not.exist(err);
should.exist(resp);
resp.should.eql(msg1);
});
masterConsole.agent.request(monitorId2, moduleId2, msg2, function(err, resp) {
resp2Count++;
should.not.exist(err);
should.exist(resp);
resp.should.eql(msg2);
});
}); // end of flow.exec
setTimeout(function() {
req1Count.should.equal(1);
req2Count.should.equal(1);
resp1Count.should.equal(1);
resp2Count.should.equal(1);
monitorConsole1.stop();
monitorConsole2.stop();
masterConsole.stop();
done();
}, WAIT_TIME);
});
it('should forward message from monitor to the masterHandler of the right module of the master by monitor.notify', function(done) {
var monitorId = 'connector-server-1';
var monitorType = 'connector';
var moduleId = 'testModuleId';
var orgMsg = {msg: 'message to master'};
var reqCount = 0;
var masterConsole = ConsoleService.createMasterConsole({
port: masterPort
});
masterConsole.register(moduleId, {
masterHandler: function(agent, msg, cb) {
reqCount++;
should.exist(msg);
msg.should.eql(orgMsg);
}
});
var monitorConsole =ConsoleService.createMonitorConsole({
host: masterHost,
port: masterPort,
id: monitorId,
type: monitorType
});
flow.exec(function() {
masterConsole.start(this);
},
function(err) {
should.not.exist(err);
monitorConsole.start(this);
},
function(err) {
should.not.exist(err);
monitorConsole.agent.notify(moduleId, orgMsg);
}); // end of flow.exec
setTimeout(function() {
reqCount.should.equal(1);
monitorConsole.stop();
masterConsole.stop();
done();
}, WAIT_TIME);
});
it('should fail if the module is disable', function(done) {
var monitorId = 'connector-server-1';
var monitorType = 'connector';
var moduleId = 'testModuleId';
var orgMsg = {msg: 'message to someone'};
var masterConsole = ConsoleService.createMasterConsole({
port: masterPort
});
masterConsole.register(moduleId, {
masterHandler: function(agent, msg, cb) {
// should not come here
true.should.not.be.ok();
}
});
var monitorConsole = ConsoleService.createMonitorConsole({
host: masterHost,
port: masterPort,
id: monitorId,
type: monitorType
});
monitorConsole.register(moduleId, {
monitorHandler: function(agent, msg, cb) {
// should not come here
true.should.not.be.ok();
}
});
flow.exec(function() {
masterConsole.start(this);
},
function(err) {
should.not.exist(err);
masterConsole.disable(moduleId);
monitorConsole.start(this);
},
function(err) {
should.not.exist(err);
monitorConsole.disable(moduleId);
monitorConsole.agent.notify(moduleId, orgMsg);
masterConsole.agent.notifyById(monitorId, moduleId, orgMsg);
}); // end of flow.exec
setTimeout(function() {
monitorConsole.stop();
masterConsole.stop();
done();
}, WAIT_TIME);
});
it('should fail if the monitor not exists', function(done) {
var monitorId = 'connector-server-1';
var moduleId = 'testModuleId';
var orgMsg = {msg: 'message to someone'};
var masterConsole = ConsoleService.createMasterConsole({
port: masterPort
});
flow.exec(function() {
masterConsole.start(this);
},
function(err) {
should.not.exist(err);
masterConsole.agent.request(monitorId, moduleId, orgMsg, function(err, resp) {
should.exist(err);
should.not.exist(resp);
});
}); // end of flow.exec
setTimeout(function() {
masterConsole.stop();
done();
}, WAIT_TIME);
});
it('should invoke masterHandler periodically in pull mode', function(done) {
var moduleId = 'testModuleId';
var intervalSec = 1;
var invokeCount = 0;
var turn = 2;
var masterConsole = ConsoleService.createMasterConsole({
port: masterPort
});
masterConsole.register(moduleId, {
type: 'pull',
interval: intervalSec,
masterHandler: function(agent, msg, cb) {
invokeCount++;
}
});
masterConsole.start();
setTimeout(function() {
invokeCount.should.equal(turn);
masterConsole.stop();
done();
}, intervalSec * (turn - 0.5) * 1000);
});
it('should invoke monitorHandler periodically in push mode', function(done) {
var monitorId = 'connector-server-1';
var monitorType = 'connector';
var moduleId = 'testModuleId';
var intervalSec = 1;
var invokeCount = 0;
var turn = 2;
var masterConsole = ConsoleService.createMasterConsole({
port: masterPort
});
var monitorConsole = ConsoleService.createMonitorConsole({
host: masterHost,
port: masterPort,
id: monitorId,
type: monitorType
});
monitorConsole.register(moduleId, {
type: 'push',
interval: intervalSec,
monitorHandler: function(agent, msg, cb) {
invokeCount++;
}
});
flow.exec(function() {
masterConsole.start(this);
},
function(err) {
should.not.exist(err);
monitorConsole.start(this);
},
function(err) {
should.not.exist(err);
});
setTimeout(function() {
invokeCount.should.equal(turn);
monitorConsole.stop();
masterConsole.stop();
done();
}, intervalSec * (turn - 0.5) * 1000);
});
});