@@ -24,6 +24,7 @@ require('../common');
24
24
const assert = require ( 'assert' ) ;
25
25
const http = require ( 'http' ) ;
26
26
const net = require ( 'net' ) ;
27
+ const Countdown = require ( '../common/countdown' ) ;
27
28
28
29
const SERVER_RESPONSES = [
29
30
'HTTP/1.0 200 ok\r\nContent-Length: 0\r\n\r\n' ,
@@ -41,34 +42,27 @@ const SHOULD_KEEP_ALIVE = [
41
42
true , // HTTP/1.1, Connection: keep-alive
42
43
false // HTTP/1.1, Connection: close
43
44
] ;
44
- let requests = 0 ;
45
- let responses = 0 ;
46
45
http . globalAgent . maxSockets = 5 ;
47
46
47
+ const countdown = new Countdown ( SHOULD_KEEP_ALIVE . length , ( ) => server . close ( ) ) ;
48
+
49
+ const getCountdownIndex = ( ) => SERVER_RESPONSES . length - countdown . remaining ;
50
+
48
51
const server = net . createServer ( function ( socket ) {
49
- socket . write ( SERVER_RESPONSES [ requests ] ) ;
50
- ++ requests ;
52
+ socket . write ( SERVER_RESPONSES [ getCountdownIndex ( ) ] ) ;
51
53
} ) . listen ( 0 , function ( ) {
52
54
function makeRequest ( ) {
53
55
const req = http . get ( { port : server . address ( ) . port } , function ( res ) {
54
56
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 ) {
60
62
makeRequest ( ) ;
61
- } else {
62
- server . close ( ) ;
63
63
}
64
64
res . resume ( ) ;
65
65
} ) ;
66
66
}
67
-
68
67
makeRequest ( ) ;
69
68
} ) ;
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