11'use strict' ;
2- var common = require ( '../common' ) ;
3- var assert = require ( 'assert' ) ;
2+ const common = require ( '../common' ) ;
3+ const assert = require ( 'assert' ) ;
44
55// Here we are testing the HTTP server module's flood prevention mechanism.
66// When writeable.write returns false (ie the underlying send() indicated the
@@ -18,29 +18,31 @@ switch (process.argv[2]) {
1818 case 'child' :
1919 return child ( ) ;
2020 default :
21- throw new Error ( 'wtf' ) ;
21+ throw new Error ( `Unexpected value: ${ process . argv [ 2 ] } ` ) ;
2222}
2323
2424function 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' ) ;
2727 var gotTimeout = false ;
2828 var childClosed = false ;
2929 var requests = 0 ;
3030 var connections = 0 ;
3131 var backloggedReqs = 0 ;
3232
33- var server = http . createServer ( function ( req , res ) {
33+ const server = http . createServer ( function ( req , res ) {
3434 requests ++ ;
3535 res . setHeader ( 'content-length' , bigResponse . length ) ;
3636 if ( ! res . write ( bigResponse ) ) {
37- if ( backloggedReqs == 0 ) {
37+ if ( backloggedReqs === 0 ) {
3838 // Once the native buffer fills (ie write() returns false), the flood
3939 // prevention should kick in.
4040 // This means the stream should emit no more 'data' events. However we
4141 // 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+ } ) ;
4446 }
4547 backloggedReqs ++ ;
4648 }
@@ -51,56 +53,46 @@ function parent() {
5153 connections ++ ;
5254 } ) ;
5355
54- server . setTimeout ( 200 , function ( conn ) {
55- gotTimeout = true ;
56- } ) ;
57-
5856 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 ( ) {
6461 childClosed = true ;
6562 server . close ( ) ;
6663 } ) ;
64+
65+ server . setTimeout ( common . platformTimeout ( 200 ) , function ( conn ) {
66+ gotTimeout = true ;
67+ child . kill ( ) ;
68+ } ) ;
6769 } ) ;
6870
6971 process . on ( 'exit' , function ( ) {
7072 assert ( gotTimeout ) ;
7173 assert ( childClosed ) ;
7274 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' ) ;
7975 } ) ;
8076}
8177
8278function child ( ) {
83- var net = require ( 'net' ) ;
79+ const net = require ( 'net' ) ;
8480
85- var conn = net . connect ( { port : common . PORT } ) ;
81+ const conn = net . connect ( { port : common . PORT } ) ;
8682
8783 var req = 'GET / HTTP/1.1\r\nHost: localhost:' +
8884 common . PORT + '\r\nAccept: */*\r\n\r\n' ;
8985
9086 req = new Array ( 10241 ) . join ( req ) ;
9187
9288 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 ) ) ;
9591 write ( ) ;
9692 } ) ;
9793
9894 conn . on ( 'drain' , write ) ;
9995
100- process . on ( 'exit' , function ( ) {
101- console . log ( 'ok - child' ) ;
102- } ) ;
103-
10496 function write ( ) {
10597 while ( false !== conn . write ( req , 'ascii' ) ) ;
10698 }
0 commit comments