@@ -11,19 +11,34 @@ const http = require('http');
1111const N = 50 ;
1212const KEEP_ALIVE = 100 ;
1313
14- const createdIds = [ ] ;
15- const destroyedIds = [ ] ;
14+ const createdIdsIncomingMessage = [ ] ;
15+ const createdIdsClientRequest = [ ] ;
16+ const destroyedIdsIncomingMessage = [ ] ;
17+ const destroyedIdsClientRequest = [ ] ;
18+
1619async_hooks . createHook ( {
1720 init : ( asyncId , type ) => {
18- if ( type === 'HTTPINCOMINGMESSAGE' || type === 'HTTPCLIENTREQUEST' ) {
19- createdIds . push ( asyncId ) ;
21+ if ( type === 'HTTPINCOMINGMESSAGE' ) {
22+ createdIdsIncomingMessage . push ( asyncId ) ;
23+ }
24+ if ( type === 'HTTPCLIENTREQUEST' ) {
25+ createdIdsClientRequest . push ( asyncId ) ;
2026 }
2127 } ,
2228 destroy : ( asyncId ) => {
23- if ( createdIds . includes ( asyncId ) ) {
24- destroyedIds . push ( asyncId ) ;
29+ if ( createdIdsIncomingMessage . includes ( asyncId ) ) {
30+ destroyedIdsIncomingMessage . push ( asyncId ) ;
2531 }
26- if ( destroyedIds . length === 2 * N ) {
32+ if ( createdIdsClientRequest . includes ( asyncId ) ) {
33+ destroyedIdsClientRequest . push ( asyncId ) ;
34+ }
35+
36+ if ( destroyedIdsClientRequest . length === N && keepAliveAgent ) {
37+ keepAliveAgent . destroy ( ) ;
38+ keepAliveAgent = undefined ;
39+ }
40+
41+ if ( destroyedIdsIncomingMessage . length === N && server . listening ) {
2742 server . close ( ) ;
2843 }
2944 }
@@ -33,28 +48,34 @@ const server = http.createServer((req, res) => {
3348 res . end ( 'Hello' ) ;
3449} ) ;
3550
36- const keepAliveAgent = new http . Agent ( {
51+ let keepAliveAgent = new http . Agent ( {
3752 keepAlive : true ,
3853 keepAliveMsecs : KEEP_ALIVE ,
3954} ) ;
4055
41- server . listen ( 0 , function ( ) {
56+ server . listen ( 0 , ( ) => {
4257 for ( let i = 0 ; i < N ; ++ i ) {
4358 ( function makeRequest ( ) {
4459 http . get ( {
4560 port : server . address ( ) . port ,
4661 agent : keepAliveAgent
47- } , function ( res ) {
62+ } , ( res ) => {
4863 res . resume ( ) ;
4964 } ) ;
5065 } ) ( ) ;
5166 }
5267} ) ;
5368
5469function checkOnExit ( ) {
55- assert . deepStrictEqual ( destroyedIds . sort ( ) , createdIds . sort ( ) ) ;
56- // There should be two IDs for each request.
57- assert . strictEqual ( createdIds . length , N * 2 ) ;
70+ assert . strictEqual ( createdIdsIncomingMessage . length , N ) ;
71+ assert . strictEqual ( createdIdsClientRequest . length , N ) ;
72+ assert . strictEqual ( destroyedIdsIncomingMessage . length , N ) ;
73+ assert . strictEqual ( destroyedIdsClientRequest . length , N ) ;
74+
75+ assert . deepStrictEqual ( destroyedIdsIncomingMessage . sort ( ) ,
76+ createdIdsIncomingMessage . sort ( ) ) ;
77+ assert . deepStrictEqual ( destroyedIdsClientRequest . sort ( ) ,
78+ createdIdsClientRequest . sort ( ) ) ;
5879}
5980
6081process . on ( 'SIGTERM' , ( ) => {
0 commit comments