11'use strict' ;
2- // test that errors propagated from cluster children are properly
3- // received in their master creates an EADDRINUSE condition by also
4- // forking a child process to listen on a socket
5-
6- var common = require ( '../common' ) ;
7- var assert = require ( 'assert' ) ;
8- var cluster = require ( 'cluster' ) ;
9- var fork = require ( 'child_process' ) . fork ;
10- var fs = require ( 'fs' ) ;
11- var net = require ( 'net' ) ;
2+ // test that errors propagated from cluster workers are properly
3+ // received in their master. Creates an EADDRINUSE condition by forking
4+ // a process in child cluster and propagates the error to the master.
125
6+ const common = require ( '../common' ) ;
7+ const assert = require ( 'assert' ) ;
8+ const cluster = require ( 'cluster' ) ;
9+ const fork = require ( 'child_process' ) . fork ;
10+ const fs = require ( 'fs' ) ;
11+ const net = require ( 'net' ) ;
1312
1413if ( cluster . isMaster ) {
15- var worker = cluster . fork ( ) ;
16- var gotError = 0 ;
17- worker . on ( 'message' , function ( err ) {
18- gotError ++ ;
14+ const worker = cluster . fork ( ) ;
15+
16+ // makes sure master is able to fork the worker
17+ cluster . on ( 'fork' , common . mustCall ( function ( ) { } ) ) ;
18+
19+ // makes sure the worker is ready
20+ worker . on ( 'online' , common . mustCall ( function ( ) { } ) ) ;
21+
22+ worker . on ( 'message' , common . mustCall ( function ( err ) {
23+ // disconnect first, so that we will not leave zombies
24+ worker . disconnect ( ) ;
25+
1926 console . log ( err ) ;
2027 assert . strictEqual ( 'EADDRINUSE' , err . code ) ;
21- worker . disconnect ( ) ;
22- } ) ;
28+ } ) ) ;
29+
2330 process . on ( 'exit' , function ( ) {
2431 console . log ( 'master exited' ) ;
2532 try {
2633 fs . unlinkSync ( common . PIPE ) ;
27- } catch ( e ) {
34+ } catch ( ex ) {
35+ if ( ex . code !== 'ENOENT' ) {
36+ throw ex ;
37+ }
2838 }
29- assert . equal ( gotError , 1 ) ;
3039 } ) ;
40+
3141} else {
32- var cp = fork ( common . fixturesDir + '/listen-on-socket-and-exit.js' ,
42+ const cp = fork ( common . fixturesDir + '/listen-on-socket-and-exit.js' ,
3343 { stdio : 'inherit' } ) ;
3444
3545 // message from the child indicates it's ready and listening
36- cp . on ( 'message' , function ( ) {
37- var server = net . createServer ( ) . listen ( common . PIPE , function ( ) {
38- console . log ( 'parent listening, should not be!' ) ;
46+ cp . on ( 'message' , common . mustCall ( function ( ) {
47+ const server = net . createServer ( ) . listen ( common . PIPE , function ( ) {
48+ // message child process so that it can exit
49+ cp . send ( 'end' ) ;
50+ // inform master about the unexpected situation
51+ process . send ( 'PIPE should have been in use.' ) ;
3952 } ) ;
4053
4154 server . on ( 'error' , function ( err ) {
@@ -45,5 +58,6 @@ if (cluster.isMaster) {
4558 // propagate error to parent
4659 process . send ( err ) ;
4760 } ) ;
48- } ) ;
61+
62+ } ) ) ;
4963}
0 commit comments