1- const assert = require ( 'assert' ) ;
2- const path = require ( 'path' ) ;
3- const coffee = require ( 'coffee' ) ;
4- const mm = require ( 'egg-mock' ) ;
5- const { readFile } = require ( 'fs/promises' ) ;
6- const { sleep } = require ( '../lib/utils/timer' ) ;
7- const utils = require ( './utils' ) ;
1+ import { strict as assert } from 'node:assert' ;
2+ import { readFile } from 'node:fs/promises' ;
3+ import { scheduler } from 'node:timers/promises' ;
4+ import coffee from 'coffee' ;
5+ import { mm , MockApplication } from '@eggjs/mock' ;
6+ import { cluster , getFilepath } from './utils.js' ;
87
9- describe ( 'test/agent_worker.test.js' , ( ) => {
10- let app ;
8+ describe ( 'test/agent_worker.test.ts' , ( ) => {
9+ let app : MockApplication ;
10+
11+ afterEach ( mm . restore ) ;
1112
1213 describe ( 'Fork Agent' , ( ) => {
1314 afterEach ( ( ) => app && app . close ( ) ) ;
1415
1516 it ( 'support config agent debug port' , ( ) => {
1617 mm ( process . env , 'EGG_AGENT_DEBUG_PORT' , '15800' ) ;
17- app = utils . cluster ( 'apps/agent-debug-port' , { isDebug : true } ) ;
18+ app = cluster ( 'apps/agent-debug-port' , { isDebug : true } as any ) ;
1819 return app
19- . expect ( 'stdout' , / 1 5 8 0 0 / )
20+ // .debug()
21+ . expect ( 'stdout' , / = 1 5 8 0 0 / )
2022 . end ( ) ;
2123 } ) ;
2224
2325 it ( 'agent debug port default 5800' , ( ) => {
24- app = utils . cluster ( 'apps/agent-debug-port' , { isDebug : true } ) ;
26+ app = cluster ( 'apps/agent-debug-port' , { isDebug : true } as any ) ;
2527 return app
26- . expect ( 'stdout' , / 5 8 0 0 / )
28+ // .debug()
29+ . expect ( 'stdout' , / = 5 8 0 0 / )
2730 . end ( ) ;
2831 } ) ;
2932
3033 it ( 'should exist when error happened during boot' , ( ) => {
31- app = utils . cluster ( 'apps/agent-die-onboot' ) ;
34+ app = cluster ( 'apps/agent-die-onboot' ) ;
3235 return app
3336 // .debug()
3437 . expect ( 'code' , 1 )
@@ -38,7 +41,7 @@ describe('test/agent_worker.test.js', () => {
3841 } ) ;
3942
4043 it ( 'should not start app when error happened during agent starting' , ( ) => {
41- app = utils . cluster ( 'apps/agent-die-onboot' ) ;
44+ app = cluster ( 'apps/agent-die-onboot' ) ;
4245 return app
4346 . expect ( 'code' , 1 )
4447 . expect ( 'stderr' , / \[ m a s t e r \] a g e n t _ w o r k e r # 1 : \d + s t a r t f a i l , e x i t i n g w i t h c o d e : 1 / )
@@ -48,7 +51,7 @@ describe('test/agent_worker.test.js', () => {
4851 } ) ;
4952
5053 it ( 'should refork new agent_worker after app started' , async ( ) => {
51- app = utils . cluster ( 'apps/agent-die' ) ;
54+ app = cluster ( 'apps/agent-die' ) ;
5255 await app
5356 // .debug()
5457 . expect ( 'stdout' , / \[ m a s t e r \] e g g s t a r t e d o n h t t p : \/ \/ 1 2 7 .0 .0 .1 : \d + / )
@@ -59,7 +62,7 @@ describe('test/agent_worker.test.js', () => {
5962 action : 'kill-agent' ,
6063 } ) ;
6164
62- await sleep ( 20000 ) ;
65+ await scheduler . wait ( 20000 ) ;
6366
6467 app . expect ( 'stderr' , / \[ m a s t e r \] a g e n t _ w o r k e r # 1 : \d + d i e d / ) ;
6568 app . expect ( 'stdout' , / \[ m a s t e r \] t r y t o s t a r t a n e w a g e n t _ w o r k e r a f t e r 1 s .../ ) ;
@@ -68,22 +71,22 @@ describe('test/agent_worker.test.js', () => {
6871 } ) ;
6972
7073 it ( 'should exit agent_worker when master die in accident' , async ( ) => {
71- app = utils . cluster ( 'apps/agent-die' ) ;
74+ app = cluster ( 'apps/agent-die' ) ;
7275 await app
7376 // .debug()
7477 . expect ( 'stdout' , / \[ m a s t e r \] e g g s t a r t e d o n h t t p : \/ \/ 1 2 7 .0 .0 .1 : \d + / )
7578 . end ( ) ;
7679
7780 // kill -9 master
7881 app . process . kill ( 'SIGKILL' ) ;
79- await sleep ( 5000 ) ;
82+ await scheduler . wait ( 5000 ) ;
8083 app . expect ( 'stderr' , / \[ a p p _ w o r k e r \] r e c e i v e d i s c o n n e c t e v e n t i n c l u s t e r f o r k m o d e , e x i t e d A f t e r D i s c o n n e c t : f a l s e / )
8184 . expect ( 'stderr' , / \[ a g e n t _ w o r k e r \] r e c e i v e d i s c o n n e c t e v e n t o n c h i l d _ p r o c e s s f o r k m o d e , e x i t i n g w i t h c o d e : 1 1 0 / )
8285 . expect ( 'stderr' , / \[ a g e n t _ w o r k e r \] e x i t w i t h c o d e : 1 1 0 / ) ;
8386 } ) ;
8487
8588 it ( 'should master exit when agent exit during app worker boot' , ( ) => {
86- app = utils . cluster ( 'apps/agent-die-on-forkapp' ) ;
89+ app = cluster ( 'apps/agent-die-on-forkapp' ) ;
8790
8891 return app
8992 // .debug()
@@ -97,7 +100,7 @@ describe('test/agent_worker.test.js', () => {
97100 } ) ;
98101
99102 it ( 'should exit when emit error during agent worker boot' , ( ) => {
100- app = utils . cluster ( 'apps/agent-start-error' ) ;
103+ app = cluster ( 'apps/agent-start-error' ) ;
101104 return app
102105 // .debug()
103106 . expect ( 'code' , 1 )
@@ -108,7 +111,7 @@ describe('test/agent_worker.test.js', () => {
108111 } ) ;
109112
110113 it ( 'should FrameworkErrorformater work during agent boot' , ( ) => {
111- app = utils . cluster ( 'apps/agent-start-framework-error' ) ;
114+ app = cluster ( 'apps/agent-start-framework-error' ) ;
112115 return app
113116 // .debug()
114117 . expect ( 'code' , 1 )
@@ -117,7 +120,7 @@ describe('test/agent_worker.test.js', () => {
117120 } ) ;
118121
119122 it ( 'should FrameworkErrorformater work during agent boot ready' , ( ) => {
120- app = utils . cluster ( 'apps/agent-start-framework-ready-error' ) ;
123+ app = cluster ( 'apps/agent-start-framework-ready-error' ) ;
121124 return app
122125 // .debug()
123126 . expect ( 'code' , 1 )
@@ -127,11 +130,11 @@ describe('test/agent_worker.test.js', () => {
127130
128131 // process.send is not exist if started by spawn
129132 it ( 'master should not die if spawn error' , async ( ) => {
130- app = coffee . spawn ( 'node' , [ utils . getFilepath ( 'apps/agent-die/start.js' ) ] ) ;
133+ app = coffee . spawn ( 'node' , [ getFilepath ( 'apps/agent-die/start.js' ) ] ) as any ;
131134 // app.debug();
132- app . close = ( ) => app . proc . kill ( ) ;
135+ app . close = async ( ) => app . proc . kill ( ) ;
133136
134- await sleep ( 3000 ) ;
137+ await scheduler . wait ( 3000 ) ;
135138 app . emit ( 'close' , 0 ) ;
136139 app . expect ( 'stderr' , / E r r o r : C a n n o t f i n d m o d u l e / ) ;
137140 app . notExpect ( 'stderr' , / T y p e E r r o r : p r o c e s s .s e n d i s n o t a f u n c t i o n / ) ;
@@ -140,15 +143,15 @@ describe('test/agent_worker.test.js', () => {
140143
141144 describe ( 'agent custom loggers' , ( ) => {
142145 before ( ( ) => {
143- app = utils . cluster ( 'apps/custom-logger' ) ;
146+ app = cluster ( 'apps/custom-logger' ) ;
144147 return app . ready ( ) ;
145148 } ) ;
146149 after ( ( ) => app . close ( ) ) ;
147150
148151 it ( 'should support custom logger in agent' , async ( ) => {
149- await sleep ( 1500 ) ;
152+ await scheduler . wait ( 1500 ) ;
150153 const content = await readFile (
151- path . join ( __dirname , 'fixtures/ apps/custom-logger/logs/monitor.log') , 'utf8' ) ;
154+ getFilepath ( ' apps/custom-logger/logs/monitor.log') , 'utf8' ) ;
152155 assert ( content === 'hello monitor!\n' ) ;
153156 } ) ;
154157 } ) ;
0 commit comments