1
1
'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.
12
5
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' ) ;
13
12
14
13
if ( 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
+
19
26
console . log ( err ) ;
20
27
assert . strictEqual ( 'EADDRINUSE' , err . code ) ;
21
- worker . disconnect ( ) ;
22
- } ) ;
28
+ } ) ) ;
29
+
23
30
process . on ( 'exit' , function ( ) {
24
31
console . log ( 'master exited' ) ;
25
32
try {
26
33
fs . unlinkSync ( common . PIPE ) ;
27
34
} catch ( e ) {
28
35
}
29
- assert . equal ( gotError , 1 ) ;
30
36
} ) ;
37
+
31
38
} else {
32
39
var cp = fork ( common . fixturesDir + '/listen-on-socket-and-exit.js' ,
33
40
{ stdio : 'inherit' } ) ;
34
41
35
42
// 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!' ) ;
43
+ cp . on ( 'message' , common . mustCall ( function ( ) {
44
+ const server = net . createServer ( ) . listen ( common . PIPE , function ( ) {
45
+ // message child process so that it can exit
46
+ cp . send ( 'end' ) ;
47
+ // inform master about the unexpected situation
48
+ process . send ( 'PIPE should have been in use.' ) ;
39
49
} ) ;
40
50
41
51
server . on ( 'error' , function ( err ) {
@@ -45,5 +55,6 @@ if (cluster.isMaster) {
45
55
// propagate error to parent
46
56
process . send ( err ) ;
47
57
} ) ;
48
- } ) ;
58
+
59
+ } ) ) ;
49
60
}
0 commit comments