@@ -28,6 +28,7 @@ const fixtures = require('../common/fixtures');
28
28
const assert = require ( 'assert' ) ;
29
29
const http = require ( 'http' ) ;
30
30
const fs = require ( 'fs' ) ;
31
+ const Countdown = require ( '../common/countdown' ) ;
31
32
32
33
http . globalAgent . maxSockets = 1 ;
33
34
@@ -38,14 +39,12 @@ const image = fixtures.readSync('/person.jpg');
38
39
console . log ( `image.length = ${ image . length } ` ) ;
39
40
40
41
const total = 10 ;
41
- let requests = 0 ;
42
- let responses = 0 ;
42
+ const responseCountdown = new Countdown ( total , common . mustCall ( ( ) => {
43
+ checkFiles ( ) ;
44
+ server . close ( ) ;
45
+ } ) ) ;
43
46
44
47
const server = http . Server ( function ( req , res ) {
45
- if ( ++ requests === total ) {
46
- server . close ( ) ;
47
- }
48
-
49
48
setTimeout ( function ( ) {
50
49
res . writeHead ( 200 , {
51
50
'content-type' : 'image/jpeg' ,
@@ -74,9 +73,7 @@ server.listen(0, function() {
74
73
75
74
s . on ( 'finish' , function ( ) {
76
75
console . error ( `done ${ x } ` ) ;
77
- if ( ++ responses === total ) {
78
- checkFiles ( ) ;
79
- }
76
+ responseCountdown . dec ( ) ;
80
77
} ) ;
81
78
} ) . on ( 'error' , function ( e ) {
82
79
console . error ( 'error! ' , e . message ) ;
@@ -86,8 +83,6 @@ server.listen(0, function() {
86
83
}
87
84
} ) ;
88
85
89
-
90
- let checkedFiles = false ;
91
86
function checkFiles ( ) {
92
87
// Should see 1.jpg, 2.jpg, ..., 100.jpg in tmpDir
93
88
const files = fs . readdirSync ( common . tmpDir ) ;
@@ -101,13 +96,4 @@ function checkFiles() {
101
96
image . length , stat . size ,
102
97
`size doesn't match on '${ fn } '. Got ${ stat . size } bytes` ) ;
103
98
}
104
-
105
- checkedFiles = true ;
106
99
}
107
-
108
-
109
- process . on ( 'exit' , function ( ) {
110
- assert . strictEqual ( total , requests ) ;
111
- assert . strictEqual ( total , responses ) ;
112
- assert . ok ( checkedFiles ) ;
113
- } ) ;
0 commit comments