Skip to content

Commit 7c2362b

Browse files
committed
test: refactor test case to include no keepAlive
1 parent 015d8e6 commit 7c2362b

File tree

2 files changed

+48
-17
lines changed

2 files changed

+48
-17
lines changed

packages/core/src/shared/awsClientBuilderV3.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export class AWSClientBuilderV3 {
8383
options?: Partial<AwsClientOptions>,
8484
region?: string,
8585
userAgent: boolean = true,
86+
keepAlive: boolean = true,
8687
settings?: DevSettings
8788
): Promise<C> {
8889
const shim = this.getShim()
@@ -113,7 +114,10 @@ export class AWSClientBuilderV3 {
113114
service.middlewareStack.add(telemetryMiddleware, { step: 'deserialize' })
114115
service.middlewareStack.add(loggingMiddleware, { step: 'finalizeRequest' })
115116
service.middlewareStack.add(getEndpointMiddleware(settings), { step: 'build' })
116-
service.middlewareStack.add(keepAliveMiddleware, { step: 'build' })
117+
118+
if (keepAlive) {
119+
service.middlewareStack.add(keepAliveMiddleware, { step: 'build' })
120+
}
117121
return service
118122
}
119123
}
@@ -198,12 +202,12 @@ export function overwriteEndpoint(
198202
) {
199203
const request = args.request
200204
if (HttpRequest.isInstance(request)) {
201-
const serviceId = getServiceId(context satisfies { clientName?: string; commandName?: string })
205+
const serviceId = getServiceId(context)
202206
const endpoint = serviceId ? settings.get('endpoints', {})[serviceId] : undefined
203207
if (endpoint) {
204208
const url = new URL(endpoint)
205209
Object.assign(request, selectFrom(url, 'hostname', 'port', 'protocol', 'pathname'))
206-
request.path = (request as HttpRequest & { pathname: string }).pathname
210+
request.path = (request as typeof request & { pathname: string }).pathname
207211
}
208212
}
209213
return next(args)

packages/core/src/testE2E/shared/awsClientBuilderV3.test.ts

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,22 @@
66
import http from 'http'
77
import assert from 'assert'
88
import { SSMClient, DescribeSessionsCommand } from '@aws-sdk/client-ssm'
9-
import { NodeHttpHandler } from '@smithy/node-http-handler'
109
import { 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

1220
describe('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

Comments
 (0)