@@ -4,31 +4,48 @@ const common = require('../common');
4
4
5
5
common . skipIfInspectorDisabled ( ) ;
6
6
7
+ // Assert that even when started with `--inspect=0` workers are assigned
8
+ // consecutive (i.e. deterministically predictable) debug ports
9
+
7
10
const assert = require ( 'assert' ) ;
8
11
const cluster = require ( 'cluster' ) ;
9
12
10
- if ( cluster . isMaster ) {
11
- const ports = [ ] ;
12
- for ( const worker of [ cluster . fork ( ) ,
13
- cluster . fork ( ) ,
14
- cluster . fork ( ) ] ) {
15
- worker . on ( 'message' , common . mustCall ( ( message ) => {
16
- ports . push ( message . debugPort ) ;
17
- worker . kill ( ) ;
13
+ function serialFork ( ) {
14
+ return new Promise ( ( res ) => {
15
+ const worker = cluster . fork ( ) ;
16
+ worker . on ( 'exit' , common . mustCall ( ( code , signal ) => {
17
+ // code 0 is normal
18
+ // code 12 can happen if inspector could not bind because of a port clash
19
+ if ( code !== 0 && code !== 12 )
20
+ assert . fail ( `code: ${ code } , signal: ${ signal } ` ) ;
21
+ const port = worker . process . spawnargs
22
+ . map ( ( a ) => ( / = (?: .* : ) ? ( \d { 2 , 5 } ) $ / . exec ( a ) || [ ] ) [ 1 ] )
23
+ . filter ( ( p ) => p )
24
+ . pop ( ) ;
25
+ res ( Number ( port ) ) ;
18
26
} ) ) ;
19
- worker . send ( 'debugPort' ) ;
20
- }
21
- process . on ( 'exit' , ( ) => {
22
- ports . sort ( ) ;
23
- assert . strictEqual ( ports . length , 3 ) ;
24
- assert ( ports . every ( ( port ) => port > 0 ) ) ;
25
- assert ( ports . every ( ( port ) => port < 65536 ) ) ;
26
- assert . strictEqual ( ports [ 0 ] + 1 , ports [ 1 ] ) ; // Ports should be consecutive.
27
- assert . strictEqual ( ports [ 1 ] + 1 , ports [ 2 ] ) ;
28
27
} ) ;
28
+ }
29
+
30
+ if ( cluster . isMaster ) {
31
+ Promise . all ( [ serialFork ( ) , serialFork ( ) , serialFork ( ) ] )
32
+ . then ( common . mustCall ( ( ports ) => {
33
+ ports . push ( process . debugPort ) ;
34
+ ports . sort ( ) ;
35
+ // 4 = [master, worker1, worker2, worker3].length()
36
+ assert . strictEqual ( ports . length , 4 ) ;
37
+ assert ( ports . every ( ( port ) => port > 0 ) ) ;
38
+ assert ( ports . every ( ( port ) => port < 65536 ) ) ;
39
+ // Ports should be consecutive.
40
+ assert . strictEqual ( ports [ 0 ] + 1 , ports [ 1 ] ) ;
41
+ assert . strictEqual ( ports [ 1 ] + 1 , ports [ 2 ] ) ;
42
+ assert . strictEqual ( ports [ 2 ] + 1 , ports [ 3 ] ) ;
43
+ } ) )
44
+ . catch (
45
+ ( err ) => {
46
+ console . error ( err ) ;
47
+ process . exit ( 1 ) ;
48
+ } ) ;
29
49
} else {
30
- process . on ( 'message' , ( message ) => {
31
- if ( message === 'debugPort' )
32
- process . send ( { debugPort : process . debugPort } ) ;
33
- } ) ;
50
+ process . exit ( 0 ) ;
34
51
}
0 commit comments