-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathmaster.test.js
1006 lines (859 loc) · 33.1 KB
/
master.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const path = require('path');
const assert = require('assert');
const fs = require('fs');
const { rm } = require('fs/promises');
const cp = require('child_process');
const pedding = require('pedding');
const mm = require('egg-mock');
const request = require('supertest');
const awaitEvent = require('await-event');
const utils = require('./utils');
const { sleep } = require('../lib/utils/timer');
describe('test/master.test.js', () => {
let app;
afterEach(mm.restore);
describe('start master', () => {
afterEach(() => app && app.close());
it('start success in local env', done => {
mm.env('local');
app = utils.cluster('apps/master-worker-started');
app.expect('stdout', /egg start/)
.expect('stdout', /egg started/)
.notExpect('stdout', /\[master\] agent_worker#1:\d+ start with clusterPort:\d+/)
.expect('code', 0)
.end(done);
});
it('start success with reusePort=true', done => {
mm.env('local');
app = utils.cluster('apps/master-worker-started', { reusePort: true });
app.expect('stdout', /egg start/)
.expect('stdout', /egg started/)
.notExpect('stdout', /\[master\] agent_worker#1:\d+ start with clusterPort:\d+/)
.expect('code', 0)
.end(done);
});
it('start success in prod env', done => {
mm.env('prod');
app = utils.cluster('apps/mock-production-app').debug(false);
app.expect('stdout', /egg start/)
.expect('stdout', /egg started/)
.expect('code', 0)
.end(err => {
assert.ifError(err);
console.log(app.stdout);
console.log(app.stderr);
done();
});
});
it('should print process.on.HOST while egg started', done => {
mm.env('prod');
mm(process.env, 'HOST', 'xxx.com');
app = utils.cluster('apps/mock-production-app').debug(false);
app.expect('stdout', /egg start/)
.expect('stdout', /egg started on http:\/\/xxx\.com:/)
.expect('code', 0)
.end(err => {
assert.ifError(err);
console.log(app.stdout);
console.log(app.stderr);
done();
});
});
it('should not print process.on.HOST if it equals 0.0.0.0', done => {
mm.env('prod');
mm(process.env, 'HOST', '0.0.0.0');
app = utils.cluster('apps/mock-production-app').debug(false);
app.expect('stdout', /egg start/)
.expect('stdout', /egg started on http:\/\/127\.0\.0\.1:/)
.expect('code', 0)
.end(err => {
assert.ifError(err);
console.log(app.stdout);
console.log(app.stderr);
done();
});
});
});
describe('close master', () => {
afterEach(() => app.close());
it('master will close agent and app worker', async () => {
mm.env('local');
mm(process.env, 'EGG_APP_WORKER_LOGGER_LEVEL', 'INFO');
mm(process.env, 'EGG_AGENT_WORKER_LOGGER_LEVEL', 'INFO');
mm(process.env, 'EGG_MASTER_LOGGER_LEVEL', 'DEBUG');
app = utils.cluster('apps/master-worker-started');
// app.debug();
await app.expect('stdout', /egg start/)
.expect('stdout', /egg started/)
.expect('code', 0)
.end();
// 2017-05-27 21:24:38,064 INFO 59065 [master] master is killed by signal SIGTERM, closing
// 2017-05-27 21:24:38,065 INFO 59065 [master] close done, exiting with code:0
// 2017-05-27 21:24:38,065 INFO 59065 [master] exit with code:0
// 2017-05-27 21:24:38,067 INFO 59067 [app_worker] receive signal SIGTERM, exiting with code:0
// 2017-05-27 21:24:38,068 INFO 59067 [app_worker] exit with code:0
// 2017-05-27 21:24:38,106 INFO 59066 [agent_worker] receive signal SIGTERM, exiting with code:0
// 2017-05-27 21:24:38,107 INFO 59066 [agent_worker] exit with code:0
app.proc.kill('SIGTERM');
await sleep(6000);
assert(app.proc.killed === true);
app.expect('stdout', /INFO \d+ \[master\] master is killed by signal SIGTERM, closing/);
app.expect('stdout', /\[master\] system memory: total \d+, free \d+/);
app.expect('stdout', /\[master\] process info: heap_limit \d+, heap_used \d+/);
app.expect('stdout', /DEBUG \d+ \[master\] close done, exiting with code:0/);
app.expect('stdout', /INFO \d+ \[master\] exit with code:0/);
app.expect('stdout', /INFO \d+ \[app_worker\] receive signal SIGTERM, exiting with code:0/);
app.expect('stdout', /INFO \d+ \[agent_worker\] receive signal SIGTERM, exiting with code:0/);
app.notExpect('stderr', /\[app_worker\] receive disconnect event in cluster fork mode/);
app.notExpect('stderr', /\[agent_worker\] receive disconnect event /);
app.expect('stdout', /INFO \d+ \[app_worker\] exit with code:0/);
app.expect('stdout', /INFO \d+ \[agent_worker\] exit with code:0/);
app.expect('stdout', /INFO \d+ \[master\] wait 5000ms/);
});
it('master kill by SIGKILL and agent, app worker exit too', async () => {
mm.env('local');
app = utils.cluster('apps/master-worker-started');
// app.debug();
await app.expect('stdout', /egg start/)
.expect('stdout', /egg started/)
.expect('code', 0)
.end();
// 2017-05-28 00:08:19,047 INFO 59500 [master] egg started on http://127.0.0.1:17001 (2364ms)
// 2017-05-28 00:08:19,058 ERROR 59502 [app_worker] receive disconnect event in cluster fork mode, exitedAfterDisconnect:false
// 2017-05-28 00:08:19,108 ERROR 59501 [agent_worker] receive disconnect event on child_process fork mode, exiting with code:110
// 2017-05-28 00:08:19,109 ERROR 59501 [agent_worker] exit with code:110
app.proc.kill('SIGKILL');
await sleep(6000);
assert(app.proc.killed === true);
app.notExpect('stdout', /\[master\] master is killed by signal SIGTERM, closing/);
app.notExpect('stdout', /\[master\] close done, exiting with code:0/);
app.notExpect('stdout', /\[master\] exit with code:0/);
app.expect('stderr', /\[app_worker\] receive disconnect event /);
app.expect('stderr', /\[agent_worker\] receive disconnect event /);
app.expect('stderr', /\[agent_worker\] exit with code:110/);
});
it('master kill by SIGKILL and exit multi workers', async () => {
mm.env('local');
app = utils.cluster('apps/master-worker-started', { workers: 4 });
// app.debug();
await app.expect('stdout', /egg start/)
.expect('stdout', /egg started/)
.expect('code', 0)
.end();
// 2017-05-28 00:08:19,047 INFO 59500 [master] egg started on http://127.0.0.1:17001 (2364ms)
// 2017-05-28 00:08:19,058 ERROR 59502 [app_worker] receive disconnect event in cluster fork mode, exitedAfterDisconnect:false
// 2017-05-28 00:08:19,108 ERROR 59501 [agent_worker] receive disconnect event on child_process fork mode, exiting with code:110
// 2017-05-28 00:08:19,109 ERROR 59501 [agent_worker] exit with code:110
app.proc.kill('SIGKILL');
await sleep(6000);
assert(app.proc.killed === true);
app.notExpect('stdout', /\[master\] master is killed by signal SIGTERM, closing/);
app.notExpect('stdout', /\[master\] close done, exiting with code:0/);
app.notExpect('stdout', /\[master\] exit with code:0/);
app.expect('stderr', /\[app_worker\] receive disconnect event /);
app.expect('stderr', /\[agent_worker\] receive disconnect event /);
app.expect('stderr', /\[agent_worker\] exit with code:110/);
});
it('use SIGTERM close master', async () => {
mm.env('local');
app = utils.cluster('apps/master-worker-started');
// app.debug();
await app.expect('stdout', /egg start/)
.expect('stdout', /egg started/)
.expect('code', 0)
.end();
// 2017-05-28 00:14:32,982 INFO 59714 [master] egg started on http://127.0.0.1:17001 (1606ms)
// 2017-05-28 00:14:32,987 INFO 59714 [master] master is killed by signal SIGTERM, closing
// 2017-05-28 00:14:32,988 INFO 59714 [master] close done, exiting with code:0
// 2017-05-28 00:14:32,988 INFO 59714 [master] exit with code:0
// 2017-05-28 00:14:32,996 INFO 59716 [app_worker] receive signal SIGTERM, exiting with code:0
// 2017-05-28 00:14:32,997 INFO 59716 [app_worker] exit with code:0
// 2017-05-28 00:14:33,047 INFO 59715 [agent_worker] receive signal SIGTERM, exiting with code:0
// 2017-05-28 00:14:33,048 INFO 59715 [agent_worker] exit with code:0
app.proc.kill('SIGTERM');
await sleep(6000);
assert(app.proc.killed === true);
app.expect('stdout', /\[master\] master is killed by signal SIGTERM, closing/);
app.expect('stdout', /\[master\] system memory: total \d+, free \d+/);
app.expect('stdout', /\[master\] process info: heap_limit \d+, heap_used \d+/);
app.expect('stdout', /\[master\] exit with code:0/);
});
it('use SIGQUIT close master', async () => {
mm.env('local');
app = utils.cluster('apps/master-worker-started');
// app.debug();
await app.expect('stdout', /egg start/)
.expect('stdout', /egg started/)
.expect('code', 0)
.end();
app.proc.kill('SIGQUIT');
await sleep(6000);
assert(app.proc.killed === true);
app.expect('stdout', /\[master\] master is killed by signal SIGQUIT, closing/);
app.expect('stdout', /\[master\] system memory: total \d+, free \d+/);
app.expect('stdout', /\[master\] process info: heap_limit \d+, heap_used \d+/);
app.expect('stdout', /\[master\] exit with code:0/);
});
it('use SIGINT close master', async () => {
mm.env('local');
app = utils.cluster('apps/master-worker-started');
// app.debug();
await app
.expect('stdout', /egg start/)
.expect('stdout', /egg started/)
.expect('code', 0)
.end();
app.proc.kill('SIGINT');
await sleep(6000);
assert(app.proc.killed === true);
app.expect('stdout', /\[master\] master is killed by signal SIGINT, closing/);
app.expect('stdout', /\[master\] system memory: total \d+, free \d+/);
app.expect('stdout', /\[master\] process info: heap_limit \d+, heap_used \d+/);
app.expect('stdout', /\[master\] exit with code:0/);
});
it('should close when set EGG_MASTER_CLOSE_TIMEOUT', async () => {
mm.env('local');
mm(process.env, 'EGG_APP_WORKER_LOGGER_LEVEL', 'INFO');
mm(process.env, 'EGG_AGENT_WORKER_LOGGER_LEVEL', 'INFO');
mm(process.env, 'EGG_MASTER_LOGGER_LEVEL', 'DEBUG');
mm(process.env, 'EGG_MASTER_CLOSE_TIMEOUT', 1000);
app = utils.cluster('apps/master-worker-started');
// app.debug();
await app.expect('stdout', /egg start/)
.expect('stdout', /egg started/)
.expect('code', 0)
.end();
app.proc.kill('SIGTERM');
await sleep(2000);
assert(app.proc.killed === true);
app.expect('stdout', /INFO \d+ \[master\] exit with code:0/);
app.expect('stdout', /INFO \d+ \[master\] wait 1000ms/);
});
it('kill order', async () => {
mm.env('local');
mm(process.env, 'EGG_APP_WORKER_LOGGER_LEVEL', 'INFO');
mm(process.env, 'EGG_AGENT_WORKER_LOGGER_LEVEL', 'INFO');
mm(process.env, 'EGG_MASTER_LOGGER_LEVEL', 'DEBUG');
mm(process.env, 'EGG_APP_CLOSE_TIMEOUT', 1000);
mm(process.env, 'EGG_AGENT_CLOSE_TIMEOUT', 1000);
app = utils.cluster('apps/worker-close-timeout');
await app.expect('stdout', /egg start/)
.expect('stdout', /egg started/)
.expect('code', 0)
.end();
app.proc.kill('SIGTERM');
await awaitEvent(app.proc, 'exit');
app.expect('stdout', /INFO \d+ \[master\] exit with code:0/);
app.expect('stdout', /INFO \d+ \[master\] wait 1000ms/);
const appTimeoutMatch = app.stdout.match(/app worker start close: (\d+)/);
const agentTimeoutMatch = app.stdout.match(/agent worker start close: (\d+)/);
const appTimeout = Number(appTimeoutMatch && appTimeoutMatch[1]);
const agentTimeout = Number(agentTimeoutMatch && agentTimeoutMatch[1]);
assert(!Number.isNaN(appTimeout));
assert(!Number.isNaN(agentTimeout));
assert(agentTimeout - appTimeout > 1000);
assert(!/app worker never called after timeout/.test(app.stdout));
assert(!/agent worker never called after timeout/.test(app.stdout));
});
it('close master will terminate all sub processes', async () => {
mm.env('local');
app = utils.cluster('apps/sub-process');
await app.expect('stdout', /egg start/)
// .debug()
.expect('stdout', /egg started/)
.expect('code', 0)
.end();
await sleep(3000);
app.proc.kill('SIGTERM');
await sleep(5000);
assert(app.proc.killed === true);
app.expect('stdout', /worker1 \[\d+\] started/);
app.expect('stdout', /worker2 \[\d+\] started/);
app.expect('stdout', /\[master\] master is killed by signal SIGTERM, closing/);
app.expect('stdout', /\[master\] system memory: total \d+, free \d+/);
app.expect('stdout', /\[master\] process info: heap_limit \d+, heap_used \d+/);
app.expect('stdout', /\[master\] exit with code:0/);
app.expect('stdout', /worker1 on sigterm and exit/);
app.expect('stdout', /worker2 on sigterm and exit/);
// worker1 and worker2 are both exit
let res = app.stdout.match(/worker1 \[(\d+)\] started/);
const pid1 = res && res[1];
res = app.stdout.match(/worker2 \[(\d+)\] started/);
const pid2 = res && res[1];
assert(!alive(pid1));
assert(!alive(pid2));
});
it('close master will terminate all sub processes with sigkill', async () => {
mm.env('local');
app = utils.cluster('apps/sub-process-sigkill');
await app.expect('stdout', /egg start/)
// .debug()
.expect('stdout', /egg started/)
.expect('code', 0)
.end();
await sleep(5000);
app.proc.kill('SIGTERM');
await sleep(8000);
assert(app.proc.killed === true);
app.expect('stdout', /worker1 \[\d+\] started/);
app.expect('stdout', /worker2 \[\d+\] started/);
app.expect('stdout', /\[master\] master is killed by signal SIGTERM, closing/);
app.expect('stdout', /\[master\] system memory: total \d+, free \d+/);
app.expect('stdout', /\[master\] process info: heap_limit \d+, heap_used \d+/);
app.expect('stdout', /\[master\] exit with code:0/);
app.expect('stdout', /worker1 on sigterm and not exit/);
app.expect('stdout', /worker2 on sigterm and exit/);
app.expect('stdout', /worker1 alived/);
// worker1 and worker2 are both exit
let res = app.stdout.match(/worker1 \[(\d+)\] started/);
const pid1 = res && res[1];
res = app.stdout.match(/worker2 \[(\d+)\] started/);
const pid2 = res && res[1];
assert(!alive(pid1));
assert(!alive(pid2));
});
});
describe('pid file', () => {
const runDir = path.join(__dirname, './fixtures/apps/master-worker-started/run');
const pidFile = path.join(runDir, './pid');
beforeEach(() => rm(runDir, { force: true, recursive: true }));
afterEach(() => app.close());
it('master should write pid file and delete', async () => {
app = utils.cluster('apps/master-worker-started', { pidFile });
// app.debug();
await app.expect('stdout', /egg start/)
.expect('stdout', /egg started/)
.expect('code', 0)
.end();
assert(fs.existsSync(pidFile));
const pid = fs.readFileSync(pidFile, 'utf-8');
assert(pid === String(app.process.pid));
app.proc.kill('SIGTERM');
await sleep(6000);
app.expect('stdout', /\[master\] exit with code:0/);
assert(!fs.existsSync(pidFile));
});
it('master should ignore fail when delete pid file ', async () => {
app = utils.cluster('apps/master-worker-started', { pidFile });
// app.debug();
await app.expect('stdout', /egg start/)
.expect('stdout', /egg started/)
.expect('code', 0)
.end();
assert(fs.existsSync(pidFile));
const pid = fs.readFileSync(pidFile, 'utf-8');
assert(pid === String(app.process.pid));
// delete
fs.unlinkSync(pidFile);
app.proc.kill('SIGTERM');
await sleep(6000);
app.expect('stdout', /\[master\] exit with code:0/);
assert(!fs.existsSync(pidFile));
});
});
describe('Messenger', () => {
afterEach(() => app.close());
it('parent -> app/agent', async () => {
app = utils.cluster('apps/messenger');
// app.debug();
await app.end();
app.proc.send({
action: 'parent2app',
data: 'parent -> app',
to: 'app',
});
app.proc.send({
action: 'parent2agent',
data: 'parent -> agent',
to: 'agent',
});
await sleep(1000);
app.expect('stdout', /parent -> agent/);
app.expect('stdout', /parent -> app/);
});
it('app/agent -> parent', done => {
done = pedding(3, done);
app = utils.cluster('apps/messenger');
// app.debug();
app.end(done);
setTimeout(() => {
app.proc.on('message', msg => {
if (msg.action === 'app2parent') done();
if (msg.action === 'agent2parent') done();
});
}, 1);
});
it('should app <-> agent', async () => {
app = utils.cluster('apps/messenger');
// app.debug();
await app.end();
await sleep(10000);
app.expect('stdout', /app -> agent/);
app.expect('stdout', /agent -> app/);
app.expect('stdout', /app: agent2appbystring/);
app.expect('stdout', /agent: app2agentbystring/);
});
it('should send multi app worker', async () => {
app = utils.cluster('apps/send-to-multiapp', { workers: 4 });
// app.debug();
await app.end();
await sleep(1000);
app.expect('stdout', /\d+ '?got'?/);
});
it('sendTo should work', async () => {
app = utils.cluster('apps/messenger');
// app.debug();
await app.end();
app.proc.on('message', console.log);
await sleep(1000);
app.expect('stdout', /app sendTo agent done/);
app.expect('stdout', /agent sendTo agent done/);
app.expect('stdout', /app sendTo app done/);
app.expect('stdout', /agent sendTo app done/);
});
it('egg-script exit', async () => {
app = {
close: () => {},
};
const appDir = path.join(__dirname, 'fixtures/apps/script-start');
const errLogPath = path.join(appDir, 'stderr.log');
const errFd = fs.openSync(errLogPath, 'w+');
const p = cp.fork(path.join(appDir, 'start-server.js'), {
stdio: [
'ignore',
'ignore',
errFd,
'ipc',
],
});
let masterPid;
p.on('message', msg => {
masterPid = msg;
});
await sleep(10000);
process.kill(masterPid);
process.kill(p.pid);
fs.closeSync(errFd);
const stderr = fs.readFileSync(errLogPath).toString();
assert(!/channel closed/.test(stderr));
});
});
describe('--cluster', () => {
before(() => {
app = utils.cluster('apps/cluster_mod_app');
return app.ready();
});
after(() => app.close());
it('should online cluster mode startup success', () => {
return app.httpRequest()
.get('/portal/i.htm')
.expect('hi cluster')
.expect(200);
});
});
describe('framework start', () => {
let app;
afterEach(() => app.close());
before(() => {
app = utils.cluster('apps/frameworkapp', {
customEgg: utils.getFilepath('apps/frameworkbiz'),
});
return app.ready();
});
it('should start with prod env', () => {
return app.httpRequest()
.get('/')
.expect({
frameworkCore: true,
frameworkPlugin: true,
frameworkAgent: true,
})
.expect(200);
});
});
describe('reload worker', () => {
let app;
after(() => app.close());
before(() => {
app = utils.cluster('apps/reload-worker', {
workers: 4,
});
// app.debug();
return app.ready();
});
it('should restart 4 workers', async () => {
app.process.send({
to: 'master',
action: 'reload-worker',
});
await sleep(20000);
app.expect('stdout', /app_worker#4:\d+ disconnect/);
app.expect('stdout', /app_worker#8:\d+ started/);
});
});
describe('after started', () => {
let app;
let readyMsg;
before(() => {
mm.env('default');
app = utils.cluster('apps/egg-ready');
// app.debug();
setTimeout(() => {
app.proc.on('message', msg => {
if (msg.to === 'parent' && msg.action === 'egg-ready') {
readyMsg = `parent: port=${msg.data.port}, address=${msg.data.address}`;
}
});
}, 1);
return app.ready();
});
after(() => app.close());
it('app/agent should recieve egg-ready', async () => {
// work for message sent
await sleep(5000);
assert(readyMsg.match(/parent: port=\d+, address=http:\/\/127.0.0.1:\d+/));
app.expect('stdout', /agent receive egg-ready, with 1 workers/);
app.expect('stdout', /app receive egg-ready, worker 1/);
});
it('should recieve egg-ready when app restart', async () => {
await request(app.callback())
.get('/exception-app')
.expect(200);
await sleep(5000);
app.expect('stdout', /app receive egg-ready, worker 2/);
});
it('should recieve egg-ready when agent restart', async () => {
await request(app.callback())
.get('/exception-agent')
.expect(200);
await sleep(5000);
const matched = app.stdout.match(/agent receive egg-ready/g);
assert(matched.length === 2);
});
});
describe('agent should recieve app worker nums', () => {
let app;
before(() => {
mm.env('default');
app = utils.cluster('apps/pid', { workers: 2 });
// app.debug();
return app.ready();
});
after(() => app.close());
it('should every app worker will get message', async () => {
await sleep(1000);
// start two workers
app.expect('stdout', /#1 agent get 1 workers \[ \d+ \]/);
app.expect('stdout', /#2 agent get 2 workers \[ \d+, \d+ \]/);
});
it('agent should get update message after app died', async () => {
try {
await app.httpRequest()
.get('/exit');
} catch (_) {
// ignore
}
await sleep(9000);
// oh, one worker dead
app.expect('stdout', /#3 agent get 1 workers \[ \d+ \]/);
// never mind, fork new worker
app.expect('stdout', /#4 agent get 2 workers \[ \d+, \d+ \]/);
});
it('agent should get message when agent restart', async () => {
app.process.send({
to: 'agent',
action: 'kill-agent',
});
await sleep(9000);
app.expect('stdout', /#1 agent get 2 workers \[ \d+, \d+ \]/);
});
});
describe('app should recieve agent worker nums', () => {
let app;
before(() => {
mm.env('default');
app = utils.cluster('apps/pid');
app.coverage(false);
// app.debug();
return app.ready();
});
after(() => app.close());
it('agent start should get message', async () => {
app.process.send({
to: 'agent',
action: 'kill-agent',
});
await sleep(9000);
app.expect('stdout', /#1 app get 0 workers \[\]/);
app.expect('stdout', /#2 app get 1 workers \[ \d+ \]/);
});
});
describe('debug', () => {
let app;
afterEach(() => app.close());
// Debugger listening on ws://127.0.0.1:9229/221caad4-e2d0-4630-b0bb-f7fb27b81ff6
const debugProtocol = 'inspect';
it('should debug', () => {
app = utils.cluster('apps/debug-port', { workers: 2, opt: { execArgv: [ `--${debugProtocol}` ] } });
return app
// .debug()
.coverage(false)
// master
.expect('stderr', /Debugger listening on .*:(5858|9229)/)
// agent
.expect('stderr', /Debugger listening on .*:5800/)
.expect('stdout', /debug port of agent is 5800/)
// worker#1
.expect('stderr', /Debugger listening on .*:(5859|9230)/)
.expect('stdout', /debug port of app is (5859|9230)/)
// worker#2
.expect('stderr', /Debugger listening on .*:(5860|9231)/)
.expect('stdout', /debug port of app is (5860|9231)/)
.end();
});
it('should debug with port', () => {
app = utils.cluster('apps/debug-port', { workers: 2, opt: { execArgv: [ `--${debugProtocol}=9000` ] } });
return app
// .debug()
.coverage(false)
// master
.expect('stderr', /Debugger listening on .*:9000/)
// agent
.expect('stderr', /Debugger listening on .*:5800/)
.expect('stdout', /debug port of agent is 5800/)
// worker#1
.expect('stderr', /Debugger listening on .*:9001/)
.expect('stdout', /debug port of app is 9001/)
// worker#2
.expect('stderr', /Debugger listening on .*:9002/)
.expect('stdout', /debug port of app is 9002/)
.end();
});
describe('debug message', () => {
const result = { app: [], agent: {} };
after(() => app.close());
before(() => {
app = utils.cluster('apps/egg-ready', { workers: 2, opt: { execArgv: [ `--${debugProtocol}` ] } });
// app.debug();
setTimeout(() => {
app.proc.on('message', msg => {
if (msg.to === 'parent' && msg.action === 'debug') {
if (msg.from === 'agent') {
result.agent = msg.data;
} else {
result.app.push(msg.data);
}
}
});
}, 1);
return app.ready();
});
it('parent should recieve debug', async () => {
// work for message sent
await sleep(5000);
app.expect('stdout', /agent receive egg-ready, with 2 workers/);
app.expect('stdout', /app receive egg-ready/);
assert(result.agent.debugPort === 5800);
assert(result.app.length === 2);
assert(result.app[0].pid);
assert(result.app[0].debugPort === 5859 || result.app[0].debugPort === 9230);
assert(result.app[1].debugPort === 5860 || result.app[1].debugPort === 9231);
});
});
describe('debug message with port', () => {
const result = { app: [], agent: {} };
after(() => app.close());
before(() => {
app = utils.cluster('apps/egg-ready', { workers: 2, opt: { execArgv: [ `--${debugProtocol}=9000` ] } });
// app.debug();
setTimeout(() => {
app.proc.on('message', msg => {
if (msg.to === 'parent' && msg.action === 'debug') {
if (msg.from === 'agent') {
result.agent = msg.data;
} else {
result.app.push(msg.data);
}
}
});
}, 1);
return app.ready();
});
it('parent should recieve debug', async () => {
// work for message sent
await sleep(5000);
app.expect('stdout', /agent receive egg-ready, with 2 workers/);
app.expect('stdout', /app receive egg-ready/);
assert(result.agent.debugPort === 5800);
assert(result.app.length === 2);
assert(result.app[0].debugPort && result.app[0].pid);
assert(result.app[0].debugPort === 9001);
assert(result.app[1].debugPort === 9002);
});
});
describe('should not debug message', () => {
let result;
after(() => app.close());
before(() => {
app = utils.cluster('apps/egg-ready');
// app.debug();
setTimeout(() => {
app.proc.on('message', msg => {
if (msg.to === 'parent' && msg.action === 'debug') {
result = true;
}
});
}, 1);
return app.ready();
});
it('parent should not recieve debug', async () => {
// work for message sent
await sleep(5000);
app.expect('stdout', /agent receive egg-ready, with 1 workers/);
app.expect('stdout', /app receive egg-ready/);
assert(!result);
});
});
describe('kill at debug', () => {
let workerPid;
after(() => app.close());
before(() => {
app = utils.cluster('apps/egg-ready', { workers: 1, opt: { execArgv: [ `--${debugProtocol}` ] } });
// app.debug();
setTimeout(() => {
app.proc.on('message', msg => {
if (msg.to === 'parent' && msg.action === 'debug' && msg.from === 'app') {
workerPid = msg.data.pid;
}
if (msg.action === 'egg-ready') {
process.kill(workerPid, 'SIGKILL');
}
});
}, 1);
return app.ready();
});
it('should not log err', async () => {
// work for message sent
await sleep(6000);
app.expect('stderr', /\[master] app_worker#.*signal: SIGKILL/);
app.expect('stderr', /\[master] worker kill by debugger, exiting/);
app.expect('stdout', /\[master] exit with code:0/);
app.notExpect('stderr', /AppWorkerDiedError/);
});
});
});
describe('--sticky', () => {
before(() => {
app = utils.cluster('apps/cluster_mod_sticky', { sticky: true });
// app.debug();
return app.ready();
});
after(() => app.close());
it('should online sticky cluster mode startup success', () => {
app.expect('stdout', /app_worker#\d:\d+ started at (?!9500)/);
app.expect('stdout', /egg started on http:\/\/127.0.0.1:17010/);
return request('http://127.0.0.1:17010')
.get('/portal/i.htm')
.expect('hi cluster')
.expect(200);
});
});
describe('agent and worker exception', () => {
it('should not exit when local env', async () => {
mm.env('local');
app = utils.cluster('apps/check-status');
// app.debug();
await app.ready();
fs.writeFileSync(path.join(app.baseDir, 'logs/started'), '');
await sleep(30000);
// process should exist
assert(app.process.exitCode === null);
app.process.kill('SIGINT');
});
it('should exit when no agent after check 3 times', async () => {
mm.env('prod');
app = utils.cluster('apps/check-status');
// app.debug();
await app.ready();
fs.mkdirSync(path.join(app.baseDir, 'logs'), { recursive: true });
fs.writeFileSync(path.join(app.baseDir, 'logs/started'), '');
// kill agent worker and will exit when start
app.process.send({ to: 'agent', action: 'kill' });
await awaitEvent(app.proc, 'exit');
assert(app.stderr.includes('nodejs.ClusterWorkerExceptionError: [master] 0 agent and 1 worker(s) alive, exit to avoid unknown state'));
assert(app.stderr.includes('[master] exit with code:1'));
});
it('should exit when no app after check 3 times', async () => {
mm.env('prod');
app = utils.cluster('apps/check-status');
// app.debug();
await app.ready();
fs.mkdirSync(path.join(app.baseDir, 'logs'), { recursive: true });
fs.writeFileSync(path.join(app.baseDir, 'logs/started'), '');
// kill app worker and wait checking
app.process.send({ to: 'app', action: 'kill' });
await awaitEvent(app.proc, 'exit');
assert(app.stderr.includes('nodejs.ClusterWorkerExceptionError: [master] 1 agent and 0 worker(s) alive, exit to avoid unknown state'));
assert(app.stderr.includes('[master] exit with code:1'));
});
});
describe('beforeClose', () => {
it('should wait app close', async () => {
mm.env('local');
app = utils.cluster('apps/before-close');
// app.debug();
await app.ready();
await app.close();
await sleep(5000);
app.expect('stdout', /app closing/);
app.expect('stdout', /app closed/);
app.expect('stdout', /agent closing/);
app.expect('stdout', /agent closed/);
});
});
describe('--require', () => {
describe('one', () => {
before(() => {
app = utils.cluster('apps/options-require', {
require: path.join(__dirname, './fixtures/apps/options-require/inject.js'),
});
// app.debug();
return app.ready();
});
after(() => app.close());
it('should inject', () => {
app.expect('stdout', /### inject application/);
app.expect('stdout', /### inject agent/);
});
});
describe('array', () => {
before(() => {
app = utils.cluster('apps/options-require', {
require: [
path.join(__dirname, './fixtures/apps/options-require/inject.js'),
'ts-node/register',
],
});
// app.debug();
return app.ready();
});
after(() => app.close());
it('should inject', () => {
app.expect('stdout', /### inject application/);
app.expect('stdout', /### inject agent/);
app.expect('stdout', /### inject ts-node\/register at app/);
app.expect('stdout', /### inject ts-node\/register at agent/);
});
});
});
});
function alive(pid) {
try {
// success means it's still alive
process.kill(pid, 0);