@@ -24,6 +24,7 @@ require('../common');
2424const assert = require ( 'assert' ) ;
2525const http = require ( 'http' ) ;
2626const net = require ( 'net' ) ;
27+ const Countdown = require ( '../common/countdown' ) ;
2728
2829const SERVER_RESPONSES = [
2930 'HTTP/1.0 200 ok\r\nContent-Length: 0\r\n\r\n' ,
@@ -41,34 +42,27 @@ const SHOULD_KEEP_ALIVE = [
4142 true , // HTTP/1.1, Connection: keep-alive
4243 false // HTTP/1.1, Connection: close
4344] ;
44- let requests = 0 ;
45- let responses = 0 ;
4645http . globalAgent . maxSockets = 5 ;
4746
47+ const countdown = new Countdown ( SHOULD_KEEP_ALIVE . length , ( ) => server . close ( ) ) ;
48+
49+ const getCountdownIndex = ( ) => SERVER_RESPONSES . length - countdown . remaining ;
50+
4851const server = net . createServer ( function ( socket ) {
49- socket . write ( SERVER_RESPONSES [ requests ] ) ;
50- ++ requests ;
52+ socket . write ( SERVER_RESPONSES [ getCountdownIndex ( ) ] ) ;
5153} ) . listen ( 0 , function ( ) {
5254 function makeRequest ( ) {
5355 const req = http . get ( { port : server . address ( ) . port } , function ( res ) {
5456 assert . strictEqual (
55- req . shouldKeepAlive , SHOULD_KEEP_ALIVE [ responses ] ,
56- `${ SERVER_RESPONSES [ responses ] } should ${
57- SHOULD_KEEP_ALIVE [ responses ] ? '' : 'not ' } Keep-Alive`) ;
58- ++ responses ;
59- if ( responses < SHOULD_KEEP_ALIVE . length ) {
57+ req . shouldKeepAlive , SHOULD_KEEP_ALIVE [ getCountdownIndex ( ) ] ,
58+ `${ SERVER_RESPONSES [ getCountdownIndex ( ) ] } should ${
59+ SHOULD_KEEP_ALIVE [ getCountdownIndex ( ) ] ? '' : 'not ' } Keep-Alive`) ;
60+ countdown . dec ( ) ;
61+ if ( countdown . remaining ) {
6062 makeRequest ( ) ;
61- } else {
62- server . close ( ) ;
6363 }
6464 res . resume ( ) ;
6565 } ) ;
6666 }
67-
6867 makeRequest ( ) ;
6968} ) ;
70-
71- process . on ( 'exit' , function ( ) {
72- assert . strictEqual ( requests , SERVER_RESPONSES . length ) ;
73- assert . strictEqual ( responses , SHOULD_KEEP_ALIVE . length ) ;
74- } ) ;
0 commit comments