/
test.js
434 lines (274 loc) · 14.9 KB
/
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
var assert = require('assert'),
supe = require('../index');
describe('Supe Test Suite', function(){
describe('About Supe', function(){
it('is a function', function(){
assert.equal(typeof supe, 'function', 'is not a function');
});
});
describe('Supervisor (Instantiated Supe) Properties', function(){
var supervisor = supe(),
expected_properties = [ 'start', 'get', 'noticeboard' ];
it('has its own "start" function', function(){
assert.equal( supervisor.hasOwnProperty('start') && typeof supervisor.start === 'function', true, 'didn\'t instantiate with its own "start" function');
});
it('has its own "get" function', function(){
assert.equal( supervisor.hasOwnProperty('get') && typeof supervisor.start === 'function', true, 'didn\'t instantiate with its own "get" function');
});
it('has its own "noticeboard" object', function(){
assert.equal( supervisor.hasOwnProperty('noticeboard') && Object.prototype.toString.call( supervisor.noticeboard ) === '[object Object]', true, 'didn\'t instantiate with its own "noticeboard" object');
});
it('has no unexpected properties', function(){
for( var prop in supervisor ){
assert.equal( expected_properties.indexOf( prop ) > -1, true, 'has unexpected property "' + prop + '"' );
}
});
it('"noticeboard" is an instance of cjs-noticeboard', function(){
assert.equal( supervisor.noticeboard instanceof require('cjs-noticeboard'), true, 'not an instance of cjs-noticeboard');
});
});
describe('Supervisor Functions Behavior', function(){
describe('Supervisor.start', function(){
var supervisor,
new_process;
beforeEach( function(){
supervisor = supe();
new_process = supervisor.start( 'logger', './test/citizen/interval-logger' );
});
it('returns an object (citizen)', function( done ){
assert.equal( Object.prototype.toString.call( new_process ) === '[object Object]', true, 'did not return an object' );
setTimeout( done, 0 );
});
it('citizen has a process reference ("ref" property)', function(){
assert.equal( new_process.hasOwnProperty( 'ref' ), true, 'citizen does not have ref property' );
});
it('citizen\'s process reference ("ref" property) is a node child_process instance', function(){
assert.equal( new_process.ref instanceof require('events'), true, 'citizen "ref" property is not an event emitter' );
assert.equal( new_process.ref.hasOwnProperty( 'stdout' ), true, 'citizen.ref does not have expected property "stdout"' );
assert.equal( new_process.ref.stdout instanceof require('events'), true, 'new process reference property "stdout" is not an event emitter' );
assert.equal( new_process.ref.hasOwnProperty( 'stderr' ), true, 'new process reference does not have expected property "stderr"' );
assert.equal( new_process.ref.stderr instanceof require('events'), true, 'new process reference property "stderr" is not an event emitter' );
});
it('will not create a new citizen without name parameter', function(){
var crashed = false;
try{
supervisor.start( null, './test/citizen/interval-logger' );
}
catch( e ){ crashed = true; }
assert.equal( crashed === true , true, 'citizen was created without name parameter' );
});
it('will not create a new citizen without file parameter', function(){
var fileless_citizen = supervisor.start( 'fileless-citizen' );
assert.equal( !fileless_citizen, true, 'citizen was created without file parameter' );
});
it('will not create a new citizen if file parameter is not a string', function(){
var nonstrings = [ 1, [], {}, function(){} ],
nonstring_file_citizen;
nonstrings.forEach( function( nonstring ){
nonstring_file_citizen = supervisor.start( 'nonstring-file-citizen' );
assert.equal( !nonstring_file_citizen, true, 'citizen was created with a non-string file parameter' );
});
});
it('will not create a new citizen if name is associated with a different citizen', function(){
var overwrite_error = false;
supervisor.start( 'common-citizen', './test/citizen/one-time-logger' );
try{
common_citizen_2 = supervisor.start( 'common-citizen', './test/citizen/interval-logger' );
}
catch( e ){ overwrite_error = true; }
assert.equal( overwrite_error === true, true, 'new citizen was registered under the name of an existing citizen' );
});
it('will restart a previously-started citizen', function( done ){
this.timeout( 10000 );
supervisor.noticeboard.watch( 'citizen-shutdown', 'start-tests', function( msg ){
var second_start = supervisor.start( 'one-time-logger' );
assert.equal( Object.prototype.toString.call( second_start ) === '[object Object]', true, 'second start attempt did not return an object as expected' );
assert.equal( second_start.hasOwnProperty( 'ref' ), true, 'second start attempt did not return with a process reference (property "ref")' );
assert.equal( first_pid !== second_start.ref.pid || first_start.ref !== second_start.ref, true, 'first and second start are the same instance' );
supervisor.noticeboard.ignore( 'citizen-shutdown', 'start-tests' );
done();
});
var first_start = supervisor.start( 'one-time-logger', './test/citizen/one-time-logger' ),
first_pid = first_start.ref.pid;
});
it('will not restart a currently-running citizen', function(){
var new_logger = supervisor.start( 'logger' );
assert.equal( Object.prototype.toString.call( new_logger) !== ['object Object'], true, 'restarted currently-running process' );
});
it('will pass parameters to citizen\'s config', function(){
var supervisor = supe(),
params = { retries: 5, duration: 5, happy: true },
citizen = supervisor.start( 'custom-logger', './test/citizen/one-time-logger', params );
for( var prop in params ){
if( !params.hasOwnProperty( prop ) ) continue;
assert.equal( citizen.config.hasOwnProperty( prop ), true, 'citizen config does not have expected property "' + prop + '"' );
assert.equal( citizen.config[ prop ] === params[ prop ], true, 'parameter property "' + prop + '" does not match citizen config\'s "' + prop + '"' );
}
});
it('will override default citizen\'s config', function(){
var supervisor = supe(),
default_logger = supervisor.start( 'default-logger', './test/citizen/one-time-logger' ),
custom_logger = supervisor.start( 'custom-logger', './test/citizen/one-time-logger', { retries: 5, duration: 5, happy: true });
assert.equal( default_logger.config !== custom_logger.config, true, 'config for default and custom loggers are identical' );
});
});
describe('Supervisor.get', function(){
supervisor = supe();
it('returns citizen with given name if it exists', function(){
var citizen = supervisor.start( 'logger', './test/citizen/interval-logger' ),
get_val = supervisor.get( 'logger' );
assert.equal( citizen === get_val, true, 'get return value does not match created citizen' );
});
it('returns false if citizen with given name does not exist', function(){
var get_val = supervisor.get( 'non-existent-logger' );
assert.equal( Object.prototype.toString.call( get_val ) !== ['object Object'], true, 'returned an object, not false' );
});
it('returns false if given name is not a string', function(){
var non_strings = [ 1, {}, [], null, false, NaN ];
non_strings.forEach( function( non_string ){
var get_val = supervisor.get( non_string );
assert.equal( Object.prototype.toString.call( get_val ) !== ['object Object'], true, 'returned an object, not false' );
});
});
});
});
describe('Supervisor Noticeboard Integration', function(){
var supervisor;
beforeEach( function(){
supervisor = supe({ retries: 0 });
});
it('sends notice "(name)-shutdown" when a citizen shuts down', function( done ){
this.timeout( 15000 );
var detected_shutdown = false;
supervisor.noticeboard.watch( 'one-time-logger-shutdown', 'do-assertions', function( msg ){
detected_shutdown = true;
assert.equal( detected_shutdown === true, true, 'did not detect specific citizen shutdown' );
done();
});
supervisor.start( 'one-time-logger', './test/citizen/one-time-logger' );
});
it('sends notice "citizen-shutdown" when any citizen shuts down', function( done ){
this.timeout( 15000 );
var first_citizen_name = 'first-logger',
second_citizen_name = 'second-logger',
shutdowns_detected = 0;
supervisor.noticeboard.watch( 'citizen-shutdown', 'do-assertions', function( msg ){
shutdowns_detected += 1;
var details = msg.notice;
if( details.name !== second_citizen_name ) return;
assert.equal( shutdowns_detected === 2, true, 'did not detect all citizen shutdowns' );
assert.equal( details.name === second_citizen_name, true, 'did not detect expected citizen shutdown' );
done();
});
supervisor.start( first_citizen_name, './test/citizen/one-time-logger' );
setTimeout( function(){
supervisor.start( second_citizen_name, './test/citizen/one-time-logger' );
}, 2000 );
});
it('sends notice "(name)-crashed" on crash', function( done ){
this.timeout( 10000 );
var detected_crash = false;
supervisor.noticeboard.once( 'one-time-crasher-crashed', 'do-assertions', function( msg ){
detected_crash = true;
assert.equal( detected_crash, true, 'did not detect specific citizen crash' );
done();
});
supervisor.start( 'one-time-crasher', './test/citizen/one-time-crasher', { retries: 0 } );
});
it('sends notice "citizen-crashed" when any citizen crashes', function( done ){
this.timeout( 15000 );
var first_citizen_name = 'first-crasher',
second_citizen_name = 'second-crasher',
crashes_detected = 0;
supervisor.noticeboard.watch( 'citizen-crashed', 'do-assertions', function( msg ){
crashes_detected += 1;
var details = msg.notice;
if( details.name !== second_citizen_name ) return;
assert.equal( crashes_detected === 2, true, 'did not detect all citizen crashes' );
assert.equal( details.name === second_citizen_name, true, 'did not detect expected citizen crash' );
done();
});
supervisor.start( first_citizen_name, './test/citizen/one-time-crasher' );
setTimeout( function(){
supervisor.start( second_citizen_name, './test/citizen/one-time-crasher' );
}, 2000 );
});
it('sends notice "(name)-auto-restarted" when crashed citizen is restarted', function( done ){
this.timeout( 10000 );
var name = 'one-time-crasher',
detected_crash = false;
supervisor.noticeboard.once( name + '-auto-restarted', 'do-assertions', function( msg ){
detected_crash = true;
assert.equal( detected_crash, true, 'did not detect specific citizen auto-restart' );
done();
});
supervisor.start( name, './test/citizen/one-time-crasher', { retries: 1 } );
});
it('sends notice "citizen-auto-restarted" when any crashed citizen is restarted', function( done ){
this.timeout( 15000 );
var first_citizen_name = 'first-crasher',
second_citizen_name = 'second-crasher',
crashes_detected = 0;
supervisor.noticeboard.watch( 'citizen-auto-restarted', 'do-assertions', function( msg ){
crashes_detected += 1;
var details = msg.notice;
if( details.name !== second_citizen_name ) return;
assert.equal( crashes_detected === 2, true, 'did not detect all citizen restarts' );
assert.equal( details.name === second_citizen_name, true, 'did not detect expected citizen crash' );
done();
});
supervisor.start( first_citizen_name, './test/citizen/one-time-crasher', { retries: 1 });
setTimeout( function(){
supervisor.start( second_citizen_name, './test/citizen/one-time-crasher', { retries: 1 });
}, 1000 );
});
});
describe('Supervisor Behavior', function(){
this.timeout( 10000 );
var supervisor;
beforeEach( function(){
supervisor = supe();
});
it('will automatically restart a crashed citizen', function( done ){
this.timeout( 10000 );
var citizen_name = 'crasher';
supervisor.noticeboard.once( citizen_name + '-auto-restarted', 'do-assertions', function( msg ){
var details = msg.notice,
citizen = supervisor.get( citizen_name );
assert.equal( citizen.ref && citizen.ref.stdout && citizen.ref instanceof require('events'), true, 'restarted citizen does not have reference to valid child process' );
done();
});
supervisor.start( citizen_name, './test/citizen/one-time-crasher', { retries: 1 });
});
it('will not automatically restart citizen that crashed excessively', function( done ){
this.timeout( 0 );
var citizen_name = 'crasher',
max_restarts = 3,
restarts = 0;
supervisor.noticeboard.watch( citizen_name + '-auto-restarted', 'count-auto-restarts', function(){
restarts += 1;
if( restarts > max_restarts ) done( new Error( 'restarted citizen more than permitted amount of times' ) );
});
supervisor.noticeboard.watch( citizen_name + '-excessive-crash', 'do-assertions', function(){
// wait two seconds before doing assertions, just in case
setTimeout( function(){
assert.equal( restarts === max_restarts, true, 'current restarts does not match max allowed restarts' );
done();
}, 2000 );
});
supervisor.start( citizen_name, './test/citizen/one-time-crasher', { retries: max_restarts });
});
it('will route mail to addressed citizen', function( done ){
this.timeout( 10000 );
supervisor.noticeboard.watch( 'supervisor-message', 'do-assertions', function( msg ){
var envelope = msg.notice;
if( envelope.type !== 'mail' ) return;
if( envelope.from !== 'routed-mail-receiver' ) return;
if( envelope.msg.received_mail_from !== 'routed-mail-sender' ) return;
done();
});
supervisor.start( 'routed-mail-receiver', './test/citizen/routed-mail-receiver' );
supervisor.start( 'routed-mail-sender', './test/citizen/routed-mail-sender' );
});
});
});