@@ -3,6 +3,10 @@ const common = require('../common');
3
3
const http = require ( 'http' ) ;
4
4
const fs = require ( 'fs' ) ;
5
5
const path = require ( 'path' ) ;
6
+ const Countdown = require ( '../common/countdown' ) ;
7
+ const NUMBER_OF_STREAMS = 2 ;
8
+
9
+ const countdown = new Countdown ( NUMBER_OF_STREAMS , ( ) => server . close ( ) ) ;
6
10
7
11
common . refreshTmpDir ( ) ;
8
12
@@ -18,27 +22,23 @@ const server = http.createServer(common.mustCall(function(req, res) {
18
22
} , 2 ) ) . listen ( 0 , function ( ) {
19
23
http . globalAgent . maxSockets = 1 ;
20
24
21
- for ( let i = 0 ; i < 2 ; ++ i ) {
22
- ( function ( i ) {
23
- const req = http . request ( {
24
- port : server . address ( ) . port ,
25
- method : 'POST' ,
26
- headers : {
27
- 'Content-Length' : 5
28
- }
29
- } , function ( res ) {
30
- res . on ( 'end' , function ( ) {
31
- console . error ( `res${ i } end` ) ;
32
- if ( i === 2 ) {
33
- server . close ( ) ;
34
- }
35
- } ) ;
36
- res . resume ( ) ;
37
- } ) ;
38
- req . on ( 'socket' , function ( s ) {
39
- console . error ( `req${ i } start` ) ;
25
+ for ( let i = 0 ; i < NUMBER_OF_STREAMS ; ++ i ) {
26
+ const req = http . request ( {
27
+ port : server . address ( ) . port ,
28
+ method : 'POST' ,
29
+ headers : {
30
+ 'Content-Length' : 5
31
+ }
32
+ } , function ( res ) {
33
+ res . on ( 'end' , function ( ) {
34
+ console . error ( `res${ i + 1 } end` ) ;
35
+ countdown . dec ( ) ;
40
36
} ) ;
41
- req . end ( '12345' ) ;
42
- } ( i + 1 ) ) ;
37
+ res . resume ( ) ;
38
+ } ) ;
39
+ req . on ( 'socket' , function ( s ) {
40
+ console . error ( `req${ i + 1 } start` ) ;
41
+ } ) ;
42
+ req . end ( '12345' ) ;
43
43
}
44
44
} ) ;
0 commit comments