1
1
'use strict' ;
2
- var common = require ( '../common' ) ;
3
- var assert = require ( 'assert' ) ;
2
+ const common = require ( '../common' ) ;
3
+ const assert = require ( 'assert' ) ;
4
4
5
5
// Here we are testing the HTTP server module's flood prevention mechanism.
6
6
// When writeable.write returns false (ie the underlying send() indicated the
@@ -18,29 +18,31 @@ switch (process.argv[2]) {
18
18
case 'child' :
19
19
return child ( ) ;
20
20
default :
21
- throw new Error ( 'wtf' ) ;
21
+ throw new Error ( `Unexpected value: ${ process . argv [ 2 ] } ` ) ;
22
22
}
23
23
24
24
function parent ( ) {
25
- var http = require ( 'http' ) ;
26
- var bigResponse = new Buffer ( 10240 ) . fill ( 'x' ) ;
25
+ const http = require ( 'http' ) ;
26
+ const bigResponse = new Buffer ( 10240 ) . fill ( 'x' ) ;
27
27
var gotTimeout = false ;
28
28
var childClosed = false ;
29
29
var requests = 0 ;
30
30
var connections = 0 ;
31
31
var backloggedReqs = 0 ;
32
32
33
- var server = http . createServer ( function ( req , res ) {
33
+ const server = http . createServer ( function ( req , res ) {
34
34
requests ++ ;
35
35
res . setHeader ( 'content-length' , bigResponse . length ) ;
36
36
if ( ! res . write ( bigResponse ) ) {
37
- if ( backloggedReqs == 0 ) {
37
+ if ( backloggedReqs === 0 ) {
38
38
// Once the native buffer fills (ie write() returns false), the flood
39
39
// prevention should kick in.
40
40
// This means the stream should emit no more 'data' events. However we
41
41
// may still be asked to process more requests if they were read before
42
- // mechanism activated.
43
- req . socket . on ( 'data' , function ( ) { assert ( false ) ; } ) ;
42
+ // the flood-prevention mechanism activated.
43
+ setImmediate ( ( ) => {
44
+ req . socket . on ( 'data' , ( ) => common . fail ( 'Unexpected data received' ) ) ;
45
+ } ) ;
44
46
}
45
47
backloggedReqs ++ ;
46
48
}
@@ -51,56 +53,46 @@ function parent() {
51
53
connections ++ ;
52
54
} ) ;
53
55
54
- server . setTimeout ( 200 , function ( conn ) {
55
- gotTimeout = true ;
56
- } ) ;
57
-
58
56
server . listen ( common . PORT , function ( ) {
59
- var spawn = require ( 'child_process' ) . spawn ;
60
- var args = [ __filename , 'child' ] ;
61
- var child = spawn ( process . execPath , args , { stdio : 'inherit' } ) ;
62
- child . on ( 'close' , function ( code ) {
63
- assert ( ! code ) ;
57
+ const spawn = require ( 'child_process' ) . spawn ;
58
+ const args = [ __filename , 'child' ] ;
59
+ const child = spawn ( process . execPath , args , { stdio : 'inherit' } ) ;
60
+ child . on ( 'close' , function ( ) {
64
61
childClosed = true ;
65
62
server . close ( ) ;
66
63
} ) ;
64
+
65
+ server . setTimeout ( common . platformTimeout ( 200 ) , function ( conn ) {
66
+ gotTimeout = true ;
67
+ child . kill ( ) ;
68
+ } ) ;
67
69
} ) ;
68
70
69
71
process . on ( 'exit' , function ( ) {
70
72
assert ( gotTimeout ) ;
71
73
assert ( childClosed ) ;
72
74
assert . equal ( connections , 1 ) ;
73
- // The number of requests we end up processing before the outgoing
74
- // connection backs up and requires a drain is implementation-dependent.
75
- console . log ( 'server got %d requests' , requests ) ;
76
- console . log ( 'server sent %d backlogged requests' , backloggedReqs ) ;
77
-
78
- console . log ( 'ok' ) ;
79
75
} ) ;
80
76
}
81
77
82
78
function child ( ) {
83
- var net = require ( 'net' ) ;
79
+ const net = require ( 'net' ) ;
84
80
85
- var conn = net . connect ( { port : common . PORT } ) ;
81
+ const conn = net . connect ( { port : common . PORT } ) ;
86
82
87
83
var req = 'GET / HTTP/1.1\r\nHost: localhost:' +
88
84
common . PORT + '\r\nAccept: */*\r\n\r\n' ;
89
85
90
86
req = new Array ( 10241 ) . join ( req ) ;
91
87
92
88
conn . on ( 'connect' , function ( ) {
93
- //kill child after 1s of flooding
94
- setTimeout ( function ( ) { conn . destroy ( ) ; } , 1000 ) ;
89
+ // Terminate child after flooding.
90
+ setTimeout ( function ( ) { conn . destroy ( ) ; } , common . platformTimeout ( 1000 ) ) ;
95
91
write ( ) ;
96
92
} ) ;
97
93
98
94
conn . on ( 'drain' , write ) ;
99
95
100
- process . on ( 'exit' , function ( ) {
101
- console . log ( 'ok - child' ) ;
102
- } ) ;
103
-
104
96
function write ( ) {
105
97
while ( false !== conn . write ( req , 'ascii' ) ) ;
106
98
}
0 commit comments