66import http from 'http'
77import assert from 'assert'
88import { SSMClient , DescribeSessionsCommand } from '@aws-sdk/client-ssm'
9- import { NodeHttpHandler } from '@smithy/node-http-handler'
109import { globals } from '../../shared'
10+ import { Socket } from 'net'
11+
12+ async function closeSocket ( socket : Socket ) {
13+ return new Promise ( ( resolve , reject ) => {
14+ socket . end ( ( ) => {
15+ resolve ( true )
16+ } )
17+ } )
18+ }
1119
1220describe ( 'AWSClientBuilderV3' , function ( ) {
1321 const port = 3000
1422 let server : http . Server
1523 let requests : http . IncomingMessage [ ]
16- let activeConnections : number
24+ let connections : Socket [ ]
1725
1826 before ( function ( ) {
1927 server = http . createServer ( { keepAlive : true } , ( req , rsp ) => {
@@ -24,36 +32,55 @@ describe('AWSClientBuilderV3', function () {
2432 server . on ( 'request' , ( req ) => {
2533 requests . push ( req )
2634 } )
27- server . on ( 'connection' , ( _req ) => {
28- activeConnections ++
35+ server . on ( 'connection' , ( connection ) => {
36+ connections . push ( connection )
2937 } )
38+ connections = [ ]
3039 } )
3140
32- beforeEach ( function ( ) {
41+ beforeEach ( async function ( ) {
3342 requests = [ ]
34- activeConnections = 0
43+ await Promise . all ( connections . map ( ( c ) => closeSocket ( c ) ) )
44+ connections = [ ]
3545 } )
3646
3747 after ( function ( ) {
3848 server . close ( )
3949 } )
4050
41- it ( 'reuses existing HTTP connections' , async function ( ) {
42- const httpHandler = new NodeHttpHandler ( {
43- httpAgent : new http . Agent ( { keepAlive : true } ) ,
44- } )
51+ it ( 'reuses existing HTTP connections by default' , async function ( ) {
4552 const client = await globals . sdkClientBuilderV3 . createAwsService ( SSMClient , {
4653 region : 'us-east-1' ,
4754 endpoint : `http://localhost:${ port } ` ,
48- requestHandler : httpHandler ,
4955 } )
50- assert . strictEqual ( activeConnections , 0 )
56+ assert . strictEqual ( connections . length , 0 )
5157 await client . send ( new DescribeSessionsCommand ( { State : 'Active' } ) )
52- assert . strictEqual ( activeConnections , 1 )
58+ assert . strictEqual ( connections . length , 1 )
5359 await client . send ( new DescribeSessionsCommand ( { State : 'Active' } ) )
54- assert . strictEqual ( activeConnections , 1 )
60+ assert . strictEqual ( connections . length , 1 )
5561
5662 assert . strictEqual ( requests [ 0 ] . headers . connection , 'keep-alive' )
5763 assert . strictEqual ( requests [ 1 ] . headers . connection , 'keep-alive' )
5864 } )
65+
66+ it ( 'does not reuse HTTP connections if told not to' , async function ( ) {
67+ const client = await globals . sdkClientBuilderV3 . createAwsService (
68+ SSMClient ,
69+ {
70+ region : 'us-east-1' ,
71+ endpoint : `http://localhost:${ port } ` ,
72+ } ,
73+ 'us-east-1' ,
74+ true ,
75+ false
76+ )
77+ assert . strictEqual ( connections . length , 0 , 'no connections before requesting' )
78+ await client . send ( new DescribeSessionsCommand ( { State : 'Active' } ) )
79+ assert . strictEqual ( connections . length , 1 , 'one connection after first request' )
80+ await client . send ( new DescribeSessionsCommand ( { State : 'Active' } ) )
81+ assert . strictEqual ( connections . length , 2 , 'two connections after both requests' )
82+
83+ assert . strictEqual ( requests [ 0 ] . headers . connection , 'close' )
84+ assert . strictEqual ( requests [ 1 ] . headers . connection , 'close' )
85+ } )
5986} )
0 commit comments