3
3
const common = require ( '../common' ) ;
4
4
const assert = require ( 'assert' ) ;
5
5
const spawn = require ( 'child_process' ) . spawn ;
6
+ const os = require ( 'os' ) ;
6
7
const path = require ( 'path' ) ;
7
8
8
9
const port = common . PORT ;
@@ -11,13 +12,24 @@ const args = [`--debug-port=${port}`, serverPath];
11
12
const options = { stdio : [ 'inherit' , 'inherit' , 'pipe' , 'ipc' ] } ;
12
13
const child = spawn ( process . execPath , args , options ) ;
13
14
14
- const outputLines = [ ] ;
15
- var waitingForDebuggers = false ;
15
+ var expectedContent = [
16
+ 'Starting debugger agent.' ,
17
+ 'Debugger listening on 127.0.0.1:' + ( port + 0 ) ,
18
+ 'Starting debugger agent.' ,
19
+ 'Debugger listening on 127.0.0.1:' + ( port + 1 ) ,
20
+ 'Starting debugger agent.' ,
21
+ 'Debugger listening on 127.0.0.1:' + ( port + 2 ) ,
22
+ ] . join ( os . EOL ) ;
23
+ expectedContent += os . EOL ; // the last line also contains an EOL character
24
+
25
+ var debuggerAgentsOutput = '' ;
26
+ var debuggerAgentsStarted = false ;
16
27
17
28
var pids ;
18
29
19
30
child . stderr . on ( 'data' , function ( data ) {
20
- const lines = data . toString ( ) . replace ( / \r / g, '' ) . trim ( ) . split ( '\n' ) ;
31
+ const childStderrOutputString = data . toString ( ) ;
32
+ const lines = childStderrOutputString . replace ( / \r / g, '' ) . trim ( ) . split ( '\n' ) ;
21
33
22
34
lines . forEach ( function ( line ) {
23
35
console . log ( '> ' + line ) ;
@@ -30,24 +42,26 @@ child.stderr.on('data', function(data) {
30
42
pids = msg . pids ;
31
43
console . error ( 'got pids %j' , pids ) ;
32
44
33
- waitingForDebuggers = true ;
34
45
process . _debugProcess ( child . pid ) ;
46
+ debuggerAgentsStarted = true ;
35
47
} ) ;
36
48
37
49
child . send ( {
38
50
type : 'getpids'
39
51
} ) ;
40
- } else if ( waitingForDebuggers ) {
41
- outputLines . push ( line ) ;
42
52
}
43
-
44
53
} ) ;
45
- if ( outputLines . length === expectedLines . length )
46
- onNoMoreLines ( ) ;
54
+
55
+ if ( debuggerAgentsStarted ) {
56
+ debuggerAgentsOutput += childStderrOutputString ;
57
+ if ( debuggerAgentsOutput . length === expectedContent . length ) {
58
+ onNoMoreDebuggerAgentsOutput ( ) ;
59
+ }
60
+ }
47
61
} ) ;
48
62
49
- function onNoMoreLines ( ) {
50
- assertOutputLines ( ) ;
63
+ function onNoMoreDebuggerAgentsOutput ( ) {
64
+ assertDebuggerAgentsOutput ( ) ;
51
65
process . exit ( ) ;
52
66
}
53
67
@@ -63,21 +77,19 @@ process.on('exit', function onExit() {
63
77
} ) ;
64
78
} ) ;
65
79
66
- const expectedLines = [
67
- 'Starting debugger agent.' ,
68
- 'Debugger listening on 127.0.0.1:' + ( port + 0 ) ,
69
- 'Starting debugger agent.' ,
70
- 'Debugger listening on 127.0.0.1:' + ( port + 1 ) ,
71
- 'Starting debugger agent.' ,
72
- 'Debugger listening on 127.0.0.1:' + ( port + 2 ) ,
73
- ] ;
74
-
75
- function assertOutputLines ( ) {
76
- // Do not assume any particular order of output messages,
77
- // since workers can take different amout of time to
78
- // start up
79
- outputLines . sort ( ) ;
80
- expectedLines . sort ( ) ;
80
+ function assertDebuggerAgentsOutput ( ) {
81
+ // Workers can take different amout of time to start up, and child processes'
82
+ // output may be interleaved arbitrarily. Moreover, child processes' output
83
+ // may be written using an arbitrary number of system calls, and no assumption
84
+ // on buffering or atomicity of output should be made. Thus, we process the
85
+ // output of all child processes' debugger agents character by character, and
86
+ // remove each character from the set of expected characters. Once all the
87
+ // output from all debugger agents has been processed, we consider that we got
88
+ // the content we expected if there's no character left in the initial
89
+ // expected content.
90
+ debuggerAgentsOutput . split ( '' ) . forEach ( function gotChar ( char ) {
91
+ expectedContent = expectedContent . replace ( char , '' ) ;
92
+ } ) ;
81
93
82
- assert . deepStrictEqual ( outputLines , expectedLines ) ;
94
+ assert . strictEqual ( expectedContent , '' ) ;
83
95
}
0 commit comments