UNPKG

16.5 kBJavaScriptView Raw
1var app = require('../lib/application');
2var pomelo = require('../');
3var should = require('should');
4
5var WAIT_TIME = 1000;
6var mockBase = process.cwd() + '/test';
7
8describe('application test', function(){
9 afterEach(function() {
10 app.state = 0;
11 app.settings = {};
12 });
13
14 describe('#init', function() {
15 it('should init the app instance', function() {
16 app.init({base: mockBase});
17 app.state.should.equal(1); // magic number from application.js
18 });
19 });
20
21 describe('#set and get', function() {
22 it('should play the role of normal set and get', function() {
23 should.not.exist(app.get('some undefined key'));
24
25 var key = 'some defined key', value = 'some value';
26 app.set(key, value);
27 value.should.equal(app.get(key));
28 });
29
30 it('should return the value if pass just one parameter to the set method', function() {
31 var key = 'some defined key', value = 'some value';
32 should.not.exist(app.set(key));
33 app.set(key, value);
34 value.should.equal(app.set(key));
35 });
36 });
37
38 describe("#enable and disable", function() {
39 it('should play the role of enable and disable', function() {
40 var key = 'some enable key';
41 app.enabled(key).should.be.false;
42 app.disabled(key).should.be.true;
43
44 app.enable(key);
45 app.enabled(key).should.be.true;
46 app.disabled(key).should.be.false;
47
48 app.disable(key);
49 app.enabled(key).should.be.false;
50 app.disabled(key).should.be.true;
51 });
52 });
53
54 describe("#compoent", function() {
55 it('should load the component and fire their lifecircle callback by app.start, app.afterStart, app.stop', function(done) {
56 var startCount = 0, afterStartCount = 0, stopCount = 0;
57
58 var mockComponent = {
59 start: function(cb) {
60 console.log('start invoked');
61 startCount++;
62 cb();
63 },
64
65 afterStart: function(cb) {
66 console.log('afterStart invoked');
67 afterStartCount++;
68 cb();
69 },
70
71 stop: function(force, cb) {
72 console.log('stop invoked');
73 stopCount++;
74 cb();
75 }
76 };
77
78 app.init({base: mockBase});
79 app.load(mockComponent);
80 app.start(function(err) {
81 should.not.exist(err);
82 });
83
84 setTimeout(function() {
85 // wait for after start
86 app.stop(false);
87
88 setTimeout(function() {
89 // wait for stop
90 startCount.should.equal(1);
91 afterStartCount.should.equal(1);
92 stopCount.should.equal(1);
93 done();
94 }, WAIT_TIME);
95 }, WAIT_TIME);
96 });
97
98 it('should access the component with a name by app.components.name after loaded', function() {
99 var key1 = 'key1', comp1 = {content: 'some thing in comp1'};
100 var comp2 = {name: 'key2', content: 'some thing in comp2'};
101 var key3 = 'key3';
102 var comp3 = function() {
103 return {content: 'some thing in comp3', name: key3};
104 };
105
106 app.init({base: mockBase});
107 app.load(key1, comp1);
108 app.load(comp2);
109 app.load(comp3);
110
111 app.components.key1.should.eql(comp1);
112 app.components.key2.should.eql(comp2);
113 app.components.key3.should.eql(comp3());
114 });
115
116 it('should ignore duplicated components', function() {
117 var key = 'key';
118 var comp1 = {content: 'some thing in comp1'};
119 var comp2 = {content: 'some thing in comp2'};
120
121 app.init({base: mockBase});
122 app.load(key, comp1);
123 app.load(key, comp2);
124
125 app.components[key].should.eql(comp1);
126 app.components[key].should.not.eql(comp2);
127 });
128 });
129
130 describe('#filter', function() {
131 it('should add before filter and could fetch it later', function() {
132 var filters = [
133 function() {console.error('filter1');},
134 function() {}
135 ];
136
137 app.init({base: mockBase});
138
139 var i, l;
140 for(i=0, l=filters.length; i<l; i++) {
141 app.before(filters[i]);
142 }
143
144 var filters2 = app.get('__befores__');
145 should.exist(filters2);
146 filters2.length.should.equal(filters.length);
147 for(i=0, l=filters2.length; i<l; i++) {
148 filters2[i].should.equal(filters[i]);
149 }
150 });
151
152 it('should add after filter and could fetch it later', function() {
153 var filters = [
154 function() {console.error('filter1');},
155 function() {}
156 ];
157
158 app.init({base: mockBase});
159
160 var i, l;
161 for(i=0, l=filters.length; i<l; i++) {
162 app.after(filters[i]);
163 }
164
165 var filters2 = app.get('__afters__');
166 should.exist(filters2);
167 filters2.length.should.equal(filters.length);
168 for(i=0, l=filters2.length; i<l; i++) {
169 filters2[i].should.equal(filters[i]);
170 }
171 });
172
173 it('should add filter and could fetch it from before and after filter later', function() {
174 var filters = [
175 function() {console.error('filter1');},
176 function() {}
177 ];
178
179 app.init({base: mockBase});
180
181 var i, l;
182 for(i=0, l=filters.length; i<l; i++) {
183 app.filter(filters[i]);
184 }
185
186 var filters2 = app.get('__befores__');
187 should.exist(filters2);
188 filters2.length.should.equal(filters.length);
189 for(i=0, l=filters2.length; i<l; i++) {
190 filters2[i].should.equal(filters[i]);
191 }
192
193 var filters3 = app.get('__afters__');
194 should.exist(filters3);
195 filters3.length.should.equal(filters.length);
196 for(i=0, l=filters3.length; i<l; i++) {
197 filters2[i].should.equal(filters[i]);
198 }
199 });
200 });
201
202 describe('#globalFilter', function() {
203 it('should add before global filter and could fetch it later', function() {
204 var filters = [
205 function() {console.error('global filter1');},
206 function() {}
207 ];
208
209 app.init({base: mockBase});
210
211 var i, l;
212 for(i=0, l=filters.length; i<l; i++) {
213 app.globalBefore(filters[i]);
214 }
215
216 var filters2 = app.get('__globalBefores__');
217 should.exist(filters2);
218 filters2.length.should.equal(filters.length);
219 for(i=0, l=filters2.length; i<l; i++) {
220 filters2[i].should.equal(filters[i]);
221 }
222 });
223
224 it('should add after global filter and could fetch it later', function() {
225 var filters = [
226 function() {console.error('filter1');},
227 function() {}
228 ];
229
230 app.init({base: mockBase});
231
232 var i, l;
233 for(i=0, l=filters.length; i<l; i++) {
234 app.globalAfter(filters[i]);
235 }
236
237 var filters2 = app.get('__globalAfters__');
238 should.exist(filters2);
239 filters2.length.should.equal(filters.length);
240 for(i=0, l=filters2.length; i<l; i++) {
241 filters2[i].should.equal(filters[i]);
242 }
243 });
244
245 it('should add filter and could fetch it from before and after filter later', function() {
246 var filters = [
247 function() {console.error('filter1');},
248 function() {}
249 ];
250
251 app.init({base: mockBase});
252
253 var i, l;
254 for(i=0, l=filters.length; i<l; i++) {
255 app.globalFilter(filters[i]);
256 }
257
258 var filters2 = app.get('__globalBefores__');
259 should.exist(filters2);
260 filters2.length.should.equal(filters.length);
261 for(i=0, l=filters2.length; i<l; i++) {
262 filters2[i].should.equal(filters[i]);
263 }
264
265 var filters3 = app.get('__globalAfters__');
266 should.exist(filters3);
267 filters3.length.should.equal(filters.length);
268 for(i=0, l=filters3.length; i<l; i++) {
269 filters2[i].should.equal(filters[i]);
270 }
271 });
272 });
273
274 describe('#configure', function() {
275 it('should execute the code block wtih the right environment', function() {
276 var proCount = 0, devCount = 0;
277 var proEnv = 'production', devEnv = 'development', serverType = 'server';
278
279 app.init({base: mockBase});
280 app.set('serverType', serverType);
281 app.set('env', proEnv);
282
283 app.configure(proEnv, serverType, function() {
284 proCount++;
285 });
286
287 app.configure(devEnv, serverType, function() {
288 devCount++;
289 });
290
291 app.set('env', devEnv);
292
293 app.configure(proEnv, serverType, function() {
294 proCount++;
295 });
296
297 app.configure(devEnv, serverType, function() {
298 devCount++;
299 });
300
301 proCount.should.equal(1);
302 devCount.should.equal(1);
303 });
304
305 it('should execute the code block wtih the right server', function() {
306 var server1Count = 0, server2Count = 0;
307 var proEnv = 'production', serverType1 = 'server1', serverType2 = 'server2';
308
309 app.init({base: mockBase});
310 app.set('serverType', serverType1);
311 app.set('env', proEnv);
312
313 app.configure(proEnv, serverType1, function() {
314 server1Count++;
315 });
316
317 app.configure(proEnv, serverType2, function() {
318 server2Count++;
319 });
320
321 app.set('serverType', serverType2);
322
323 app.configure(proEnv, serverType1, function() {
324 server1Count++;
325 });
326
327 app.configure(proEnv, serverType2, function() {
328 server2Count++;
329 });
330
331 server1Count.should.equal(1);
332 server2Count.should.equal(1);
333 });
334 });
335
336 describe('#route', function() {
337 it('should add route record and could fetch it later', function() {
338 var type1 = 'area', type2 = 'connector';
339 var func1 = function() {console.log('func1');};
340 var func2 = function() {console.log('func2');};
341
342 app.init({base: mockBase});
343
344 app.route(type1, func1);
345 app.route(type2, func2);
346
347 var routes = app.get('__routes__');
348 should.exist(routes);
349 func1.should.equal(routes[type1]);
350 func2.should.equal(routes[type2]);
351 });
352 });
353
354 describe('#transaction', function() {
355 it('should execute all conditions and handlers', function() {
356 var conditions = {
357 test1: function(cb) {
358 console.log('condition1');
359 cb();
360 },
361 test2: function(cb) {
362 console.log('condition2');
363 cb();
364 }
365 };
366 var flag = 1;
367 var handlers = {
368 do1: function(cb) {
369 console.log('handler1');
370 cb();
371 },
372 do2: function(cb) {
373 console.log('handler2');
374 if(flag < 3){
375 flag ++;
376 cb(new Error('error'));
377 } else {
378 cb();
379 }
380 }
381 };
382 app.transaction('test', conditions, handlers, 5);
383 });
384
385 it('shoud execute conditions with error and do not execute handlers', function() {
386 var conditions = {
387 test1: function(cb) {
388 console.log('condition1');
389 cb();
390 },
391 test2: function(cb) {
392 console.log('condition2');
393 cb(new Error('error'));
394 },
395 test3: function(cb) {
396 console.log('condition3');
397 cb();
398 }
399 };
400 var handlers = {
401 do1: function(cb) {
402 console.log('handler1');
403 cb();
404 },
405 do2: function(cb) {
406 console.log('handler2');
407 cb();
408 }
409 };
410 app.transaction('test', conditions, handlers);
411 });
412 });
413
414 describe('#add and remove servers', function() {
415 it('should add servers and emit event and fetch the new server info by get methods', function(done) {
416 var newServers = [
417 {id: 'connector-server-1', serverType: 'connecctor', host: '127.0.0.1', port: 1234, clientPort: 3000, frontend: true},
418 {id: 'area-server-1', serverType: 'area', host: '127.0.0.1', port: 2234}
419 ];
420 app.init({base: mockBase});
421 app.event.on(pomelo.events.ADD_SERVERS, function(servers) {
422 // check event args
423 newServers.should.eql(servers);
424
425 // check servers
426 var curServers = app.getServers();
427 should.exist(curServers);
428 var item, i, l;
429 for(i=0, l=newServers.length; i<l; i++) {
430 item = newServers[i];
431 item.should.eql(curServers[item.id]);
432 }
433
434 // check get server by id
435 for(i=0, l=newServers.length; i<l; i++) {
436 item = newServers[i];
437 item.should.eql(app.getServerById(item.id));
438 }
439
440 // check server types
441 var types = [];
442 for(i=0, l=newServers.length; i<l; i++) {
443 item = newServers[i];
444 if(types.indexOf(item.serverType) < 0) {
445 types.push(item.serverType);
446 }
447 }
448 var types2 = app.getServerTypes();
449 types.length.should.equal(types2.length);
450 for(i=0, l=types.length; i<l; i++) {
451 types2.should.include(types[i]);
452 }
453
454 // check server type list
455 var slist;
456 for(i=0, l=newServers.length; i<l; i++) {
457 item = newServers[i];
458 slist = app.getServersByType(item.serverType);
459 should.exist(slist);
460 contains(slist, item).should.be.true;
461 }
462
463 done();
464 });
465
466 app.addServers(newServers);
467 });
468
469 it('should remove server info and emit event', function(done) {
470 var newServers = [
471 {id: 'connector-server-1', serverType: 'connecctor', host: '127.0.0.1', port: 1234, clientPort: 3000, frontend: true},
472 {id: 'area-server-1', serverType: 'area', host: '127.0.0.1', port: 2234},
473 {id: 'path-server-1', serverType: 'path', host: '127.0.0.1', port: 2235}
474 ];
475 var destServers = [
476 {id: 'connector-server-1', serverType: 'connecctor', host: '127.0.0.1', port: 1234, clientPort: 3000, frontend: true},
477 {id: 'path-server-1', serverType: 'path', host: '127.0.0.1', port: 2235}
478 ];
479 var delIds = ['area-server-1'];
480 var addCount = 0;
481 var delCount = 0;
482
483 app.init({base: mockBase});
484 app.event.on(pomelo.events.ADD_SERVERS, function(servers) {
485 // check event args
486 newServers.should.eql(servers);
487 addCount++;
488 });
489
490 app.event.on(pomelo.events.REMOVE_SERVERS, function(ids) {
491 delIds.should.eql(ids);
492
493 // check servers
494 var curServers = app.getServers();
495 should.exist(curServers);
496 var item, i, l;
497 for(i=0, l=destServers.length; i<l; i++) {
498 item = destServers[i];
499 item.should.eql(curServers[item.id]);
500 }
501
502 // check get server by id
503 for(i=0, l=destServers.length; i<l; i++) {
504 item = destServers[i];
505 item.should.eql(app.getServerById(item.id));
506 }
507
508 // check server types
509 // NOTICE: server types would not clear when remove server from app
510 var types = [];
511 for(i=0, l=newServers.length; i<l; i++) {
512 item = newServers[i];
513 if(types.indexOf(item.serverType) < 0) {
514 types.push(item.serverType);
515 }
516 }
517 var types2 = app.getServerTypes();
518 types.length.should.equal(types2.length);
519 for(i=0, l=types.length; i<l; i++) {
520 types2.should.include(types[i]);
521 }
522
523 // check server type list
524 var slist;
525 for(i=0, l=destServers.length; i<l; i++) {
526 item = destServers[i];
527 slist = app.getServersByType(item.serverType);
528 should.exist(slist);
529 contains(slist, item).should.be.true;
530 }
531
532 done();
533 });
534
535 app.addServers(newServers);
536 app.removeServers(delIds);
537 });
538 });
539
540 describe('#beforeStopHook', function() {
541 it('should be called before application stopped.', function(done) {
542 var count = 0;
543 app.init({base: mockBase});
544 app.beforeStopHook(function() {
545 count++;
546 });
547 app.start(function(err) {
548 should.not.exist(err);
549 });
550
551 setTimeout(function() {
552 // wait for after start
553 app.stop(false);
554
555 setTimeout(function() {
556 // wait for stop
557 count.should.equal(1);
558 done();
559 }, WAIT_TIME);
560 }, WAIT_TIME);
561 });
562 });
563 describe('#use', function() {
564 it('should exist plugin component and event', function(done) {
565 var plugin = {
566 components: mockBase + '/mock-plugin/components/',
567 events: mockBase + '/mock-plugin/events/'
568 };
569 var opts = {};
570 app.use(plugin, opts);
571 should.exist(app.event.listeners('bind_session'));
572 should.exist(app.components.mockPlugin);
573 done();
574 });
575 });
576});
577
578var contains = function(slist, sinfo) {
579 for(var i=0, l=slist.length; i<l; i++) {
580 if(slist[i].id === sinfo.id) {
581 return true;
582 }
583 }
584 return false;
585};