@@ -12,56 +12,56 @@ const waitStart = process.env.COV ? 5000 : 2000;
1212
1313describe ( 'test/index.test.js' , ( ) => {
1414 describe ( 'cluster' , ( ) => {
15- it ( 'should workers auto exit when master kill by SIGKILL' , function * ( ) {
15+ it ( 'should workers auto exit when master kill by SIGKILL' , async ( ) => {
1616 const startFile = path . join ( fixtures , 'cluster.js' ) ;
1717 const child = coffee . fork ( startFile )
1818 . debug ( ) ;
19- yield sleep ( waitStart ) ;
20- const result = yield urllib . request ( 'http://127.0.0.1:8000/' ) ;
19+ await sleep ( waitStart ) ;
20+ const result = await urllib . request ( 'http://127.0.0.1:8000/' ) ;
2121 assert ( result . status === 200 ) ;
2222 assert ( result . data . toString ( ) === 'hello world\n' ) ;
2323 // all workers exit by cluster
2424 child . proc . kill ( 'SIGKILL' ) ;
25- yield sleep ( 1000 ) ;
25+ await sleep ( 1000 ) ;
2626 child . expect ( 'stderr' , / \[ a p p - w o r k e r - 1 \] 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 / ) ;
2727 child . expect ( 'stderr' , / \[ a p p - w o r k e r - 2 \] 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 / ) ;
2828 child . expect ( 'stdout' , / \[ a p p - w o r k e r - 1 \] e x i t w i t h c o d e : 0 / ) ;
2929 child . expect ( 'stdout' , / \[ a p p - w o r k e r - 2 \] e x i t w i t h c o d e : 0 / ) ;
3030 } ) ;
3131
32- it ( 'should don\'t print info log' , function * ( ) {
32+ it ( 'should don\'t print info log' , async ( ) => {
3333 const startFile = path . join ( fixtures , 'cluster.js' ) ;
3434 const child = coffee . fork ( startFile , { env : { NODE_LOG_LEVEL : 'warn' } } )
3535 . debug ( ) ;
36- yield sleep ( waitStart ) ;
37- const result = yield urllib . request ( 'http://127.0.0.1:8000/' ) ;
36+ await sleep ( waitStart ) ;
37+ const result = await urllib . request ( 'http://127.0.0.1:8000/' ) ;
3838 assert ( result . status === 200 ) ;
3939 assert ( result . data . toString ( ) === 'hello world\n' ) ;
4040 // all workers exit by cluster
4141 child . proc . kill ( 'SIGKILL' ) ;
42- yield sleep ( 1000 ) ;
42+ await sleep ( 1000 ) ;
4343 child . expect ( 'stderr' , / \[ a p p - w o r k e r - 1 \] 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 / ) ;
4444 child . expect ( 'stderr' , / \[ a p p - w o r k e r - 2 \] 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 / ) ;
4545 child . notExpect ( 'stdout' , / \[ a p p - w o r k e r - 1 \] e x i t w i t h c o d e : 0 / ) ;
4646 child . notExpect ( 'stdout' , / \[ a p p - w o r k e r - 2 \] e x i t w i t h c o d e : 0 / ) ;
4747 } ) ;
4848
49- it ( 'should workers auto exit when master kill by SIGTERM' , function * ( ) {
49+ it ( 'should workers auto exit when master kill by SIGTERM' , async ( ) => {
5050 const startFile = path . join ( fixtures , 'cluster.js' ) ;
5151 const child = coffee . fork ( startFile )
5252 . debug ( ) ;
53- yield sleep ( waitStart ) ;
54- const result = yield urllib . request ( 'http://127.0.0.1:8000/' ) ;
53+ await sleep ( waitStart ) ;
54+ const result = await urllib . request ( 'http://127.0.0.1:8000/' ) ;
5555 assert ( result . status === 200 ) ;
5656 assert ( result . data . toString ( ) === 'hello world\n' ) ;
5757 // suicide
58- const result2 = yield urllib . request ( 'http://127.0.0.1:8000/suicide' ) ;
58+ const result2 = await urllib . request ( 'http://127.0.0.1:8000/suicide' ) ;
5959 assert ( result2 . status === 200 ) ;
6060 assert ( result2 . data . toString ( ) === 'hello world\n' ) ;
61- yield sleep ( 1000 ) ;
61+ await sleep ( 1000 ) ;
6262 // make sure all workers exit by itself after SIGTERM event fired
6363 child . proc . kill ( 'SIGTERM' ) ;
64- yield sleep ( 2000 ) ;
64+ await sleep ( 2000 ) ;
6565 child . notExpect ( 'stderr' , / \[ a p p - w o r k e r - 1 \] 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 / ) ;
6666 child . notExpect ( 'stderr' , / \[ a p p - w o r k e r - 2 \] 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 / ) ;
6767 if ( process . platform !== 'win32' ) {
@@ -72,39 +72,83 @@ describe('test/index.test.js', () => {
7272 child . expect ( 'stdout' , / \[ a p p - w o r k e r - 2 \] e x i t w i t h c o d e : 0 / ) ;
7373 child . expect ( 'stdout' , / w o r k e r \d + d i e d , c o d e 0 , s i g n a l n u l l / ) ;
7474 } ) ;
75+
76+ it ( 'should always listen sigterm work' , async ( ) => {
77+ const startFile = path . join ( fixtures , 'cluster.js' ) ;
78+ const child = coffee . fork ( startFile , [ ] , {
79+ env : {
80+ ...process . env ,
81+ ALWAYS_ON_SIGTERM : 'Y' ,
82+ } ,
83+ } )
84+ . debug ( ) ;
85+ await sleep ( waitStart ) ;
86+ child . proc . kill ( 'SIGTERM' ) ;
87+ await sleep ( 2200 ) ;
88+ if ( process . platform !== 'win32' ) {
89+ // windows can't handle SIGTERM signal
90+ child . expect ( 'stdout' , / \[ a p p - w o r k e r - \d \] r e c e i v e s i g n a l S I G T E R M , e x i t i n g w i t h c o d e : 0 / ) ;
91+ child . expect ( 'stdout' , / \[ a p p - w o r k e r - \d \] r e c e i v e s i g n a l S I G T E R M a g a i n , w a i t i n g f o r e x i t / ) ;
92+ child . expect ( 'stdout' , / e x i t a f t e r 1 0 0 0 m s / ) ;
93+ }
94+ child . expect ( 'stdout' , / \[ a p p - w o r k e r - 1 \] e x i t w i t h c o d e : 0 / ) ;
95+ child . expect ( 'stdout' , / \[ a p p - w o r k e r - 2 \] e x i t w i t h c o d e : 0 / ) ;
96+ } ) ;
7597 } ) ;
7698
7799 describe ( 'child_process.fork' , ( ) => {
78- it ( 'should worker exit after master kill by SIGKILL' , function * ( ) {
100+ it ( 'should worker exit after master kill by SIGKILL' , async ( ) => {
79101 const startFile = path . join ( fixtures , 'master.js' ) ;
80102 const child = coffee . fork ( startFile )
81103 . debug ( ) ;
82- yield sleep ( waitStart ) ;
83- const result = yield urllib . request ( 'http://127.0.0.1:8000/' ) ;
104+ await sleep ( waitStart ) ;
105+ const result = await urllib . request ( 'http://127.0.0.1:8000/' ) ;
84106 assert ( result . status === 200 ) ;
85107 assert ( result . data . toString ( ) === 'hello world\n' ) ;
86108 // the worker exit by graceful-process
87109 child . proc . kill ( 'SIGKILL' ) ;
88- yield sleep ( 2000 ) ;
110+ await sleep ( 2000 ) ;
89111 if ( process . platform !== 'win32' ) {
90112 child . expect ( 'stderr' , / \[ t e s t - c h i l d \] 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 / ) ;
91113 child . expect ( 'stderr' , / \[ t e s t - c h i l d \] e x i t w i t h c o d e : 1 1 0 / ) ;
92114 }
93115 } ) ;
116+
117+ it ( 'should always listen sigterm work' , async ( ) => {
118+ const startFile = path . join ( fixtures , 'master-sigterm.js' ) ;
119+ const child = coffee . fork ( startFile , [ ] , {
120+ env : {
121+ ...process . env ,
122+ ALWAYS_ON_SIGTERM : 'Y' ,
123+ } ,
124+ } )
125+ . debug ( ) ;
126+ await sleep ( waitStart ) ;
127+ // the worker exit by graceful-process
128+ child . proc . kill ( 'SIGTERM' ) ;
129+ await sleep ( 2000 ) ;
130+ if ( process . platform !== 'win32' ) {
131+ // windows can't handle SIGTERM signal
132+ child . expect ( 'stdout' , / \[ t e s t - c h i l d \] r e c e i v e s i g n a l S I G T E R M , e x i t i n g w i t h c o d e : 0 / ) ;
133+ child . expect ( 'stdout' , / \[ t e s t - c h i l d \] r e c e i v e s i g n a l S I G T E R M a g a i n , w a i t i n g f o r e x i t / ) ;
134+ child . expect ( 'stdout' , / e x i t a f t e r 1 0 0 0 m s / ) ;
135+ child . expect ( 'stdout' , / \[ t e s t - c h i l d \] e x i t w i t h c o d e : 0 / ) ;
136+ }
137+ } ) ;
94138 } ) ;
95139
96140 describe . skip ( 'child_process.spawn' , ( ) => {
97- it ( 'should worker exit after master kill by SIGKILL' , function * ( ) {
141+ it ( 'should worker exit after master kill by SIGKILL' , async ( ) => {
98142 const startFile = path . join ( fixtures , 'master-spawn.js' ) ;
99143 const child = coffee . fork ( startFile )
100144 . debug ( ) ;
101- yield sleep ( waitStart ) ;
102- const result = yield urllib . request ( 'http://127.0.0.1:8000/' ) ;
145+ await sleep ( waitStart ) ;
146+ const result = await urllib . request ( 'http://127.0.0.1:8000/' ) ;
103147 assert ( result . status === 200 ) ;
104148 assert ( result . data . toString ( ) === 'hello world\n' ) ;
105149 // the worker exit by graceful-process
106150 child . proc . kill ( 'SIGKILL' ) ;
107- yield sleep ( 1000 ) ;
151+ await sleep ( 1000 ) ;
108152 child . expect ( 'stderr' , / 2 2 2 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 / ) ;
109153 child . expect ( 'stderr' , / 2 2 2 e x i t w i t h c o d e : 1 1 0 / ) ;
110154 } ) ;
@@ -113,81 +157,81 @@ describe('test/index.test.js', () => {
113157 describe ( 'beforeExit' , ( ) => {
114158 afterEach ( mm . restore ) ;
115159
116- it ( 'should support normal function' , function * ( ) {
160+ it ( 'should support normal function' , async ( ) => {
117161 mm ( process . env , 'MODE' , '' ) ;
118162 const startFile = path . join ( fixtures , 'before-exit.js' ) ;
119163 const child = coffee . fork ( startFile )
120164 . debug ( ) ;
121- yield sleep ( waitStart ) ;
122- const result = yield urllib . request ( 'http://127.0.0.1:8000/' ) ;
165+ await sleep ( waitStart ) ;
166+ const result = await urllib . request ( 'http://127.0.0.1:8000/' ) ;
123167 assert ( result . status === 200 ) ;
124168
125169 child . proc . kill ( ) ;
126- yield sleep ( 5000 ) ;
170+ await sleep ( 5000 ) ;
127171 child . expect ( 'stdout' , / p r o c e s s e x i t / ) ;
128172 child . expect ( 'stdout' , / e x i t w i t h c o d e : 0 / ) ;
129173 } ) ;
130174
131- it ( 'should support function return promise' , function * ( ) {
175+ it ( 'should support function return promise' , async ( ) => {
132176 mm ( process . env , 'MODE' , 'promise' ) ;
133177 const startFile = path . join ( fixtures , 'before-exit.js' ) ;
134178 const child = coffee . fork ( startFile )
135179 . debug ( ) ;
136- yield sleep ( waitStart ) ;
137- const result = yield urllib . request ( 'http://127.0.0.1:8000/' ) ;
180+ await sleep ( waitStart ) ;
181+ const result = await urllib . request ( 'http://127.0.0.1:8000/' ) ;
138182 assert ( result . status === 200 ) ;
139183
140184 child . proc . kill ( ) ;
141- yield sleep ( 5000 ) ;
185+ await sleep ( 5000 ) ;
142186 child . expect ( 'stdout' , / b e f o r e E x i t s u c c e s s / ) ;
143187 child . expect ( 'stdout' , / p r o c e s s e x i t i n g \n p r o c e s s e x i t e d / ) ;
144188 child . expect ( 'stdout' , / e x i t w i t h c o d e : 0 / ) ;
145189 } ) ;
146190
147191 if ( parseInt ( process . versions . node ) < 8 ) return ;
148192
149- it ( 'should support async function' , function * ( ) {
193+ it ( 'should support async function' , async ( ) => {
150194 mm ( process . env , 'MODE' , 'async' ) ;
151195 const startFile = path . join ( fixtures , 'before-exit.js' ) ;
152196 const child = coffee . fork ( startFile )
153197 . debug ( ) ;
154- yield sleep ( waitStart ) ;
155- const result = yield urllib . request ( 'http://127.0.0.1:8000/' ) ;
198+ await sleep ( waitStart ) ;
199+ const result = await urllib . request ( 'http://127.0.0.1:8000/' ) ;
156200 assert ( result . status === 200 ) ;
157201
158202 child . proc . kill ( ) ;
159- yield sleep ( 5000 ) ;
203+ await sleep ( 5000 ) ;
160204 child . expect ( 'stdout' , / b e f o r e E x i t s u c c e s s / ) ;
161205 child . expect ( 'stdout' , / p r o c e s s e x i t i n g \n p r o c e s s e x i t e d / ) ;
162206 child . expect ( 'stdout' , / e x i t w i t h c o d e : 0 / ) ;
163207 } ) ;
164208
165- it ( 'should exit when async error' , function * ( ) {
209+ it ( 'should exit when async error' , async ( ) => {
166210 mm ( process . env , 'MODE' , 'async-error' ) ;
167211 const startFile = path . join ( fixtures , 'before-exit.js' ) ;
168212 const child = coffee . fork ( startFile )
169213 . debug ( ) ;
170- yield sleep ( waitStart ) ;
171- const result = yield urllib . request ( 'http://127.0.0.1:8000/' ) ;
214+ await sleep ( waitStart ) ;
215+ const result = await urllib . request ( 'http://127.0.0.1:8000/' ) ;
172216 assert ( result . status === 200 ) ;
173217
174218 child . proc . kill ( ) ;
175- yield sleep ( 5000 ) ;
219+ await sleep ( 5000 ) ;
176220 child . expect ( 'stderr' , / b e f o r e E x i t f a i l , e r r o r : r e j e c t / ) ;
177221 child . expect ( 'stdout' , / e x i t w i t h c o d e : 0 / ) ;
178222 } ) ;
179223
180- it ( 'should exit when function error' , function * ( ) {
224+ it ( 'should exit when function error' , async ( ) => {
181225 mm ( process . env , 'MODE' , 'function-error' ) ;
182226 const startFile = path . join ( fixtures , 'before-exit.js' ) ;
183227 const child = coffee . fork ( startFile )
184228 . debug ( ) ;
185- yield sleep ( waitStart ) ;
186- const result = yield urllib . request ( 'http://127.0.0.1:8000/' ) ;
229+ await sleep ( waitStart ) ;
230+ const result = await urllib . request ( 'http://127.0.0.1:8000/' ) ;
187231 assert ( result . status === 200 ) ;
188232
189233 child . proc . kill ( ) ;
190- yield sleep ( 5000 ) ;
234+ await sleep ( 5000 ) ;
191235 child . expect ( 'stderr' , / b e f o r e E x i t f a i l , e r r o r : p r o c e s s e x i t / ) ;
192236 child . expect ( 'stdout' , / e x i t w i t h c o d e : 0 / ) ;
193237 } ) ;
0 commit comments