Skip to content

Commit 10959cb

Browse files
committed
test: find test case that breaks current approach
1 parent 1aaa3df commit 10959cb

File tree

3 files changed

+79
-5
lines changed

3 files changed

+79
-5
lines changed

packages/core/src/shared/awsClientBuilderV3.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { extensionVersion } from './vscode/env'
3333
import { getLogger } from './logger/logger'
3434
import { partialClone } from './utilities/collectionUtils'
3535
import { selectFrom } from './utilities/tsUtils'
36-
import { memoize } from './utilities/functionUtils'
36+
import { memoizeAsync } from './utilities/functionUtils'
3737

3838
export type AwsClientConstructor<C> = new (o: AwsClientOptions) => C
3939

@@ -53,7 +53,7 @@ export interface AwsCommand {
5353
resolveMiddleware: (stack: any, configuration: any, options: any) => Handler<any, any>
5454
}
5555

56-
interface AwsClientOptions {
56+
export interface AwsClientOptions {
5757
credentials: AwsCredentialIdentityProvider
5858
region: string | Provider<string>
5959
userAgent: UserAgent
@@ -78,7 +78,7 @@ export class AWSClientBuilderV3 {
7878
return shim
7979
}
8080

81-
public getAwsService = memoize(this.createAwsService.bind(this))
81+
public getAwsService = memoizeAsync(this.createAwsService.bind(this))
8282

8383
public async createAwsService<C extends AwsClient>(
8484
type: AwsClientConstructor<C>,

packages/core/src/shared/clients/clientWrapper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export abstract class ClientWrapper<C extends AwsClient> implements vscode.Dispo
1919
if (this.client) {
2020
return this.client
2121
}
22-
this.client = await globals.sdkClientBuilderV3.createAwsService(this.clientType, undefined, this.regionCode)
22+
this.client = await globals.sdkClientBuilderV3.getAwsService(this.clientType, undefined, this.regionCode)
2323
return this.client!
2424
}
2525

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

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import { FakeMemento } from '../fakeExtensionContext'
1010
import { FakeAwsContext } from '../utilities/fakeAwsContext'
1111
import { GlobalState } from '../../shared/globalState'
1212
import {
13+
AwsClient,
1314
AWSClientBuilderV3,
15+
AwsClientOptions,
16+
AwsCommand,
1417
emitOnRequest,
1518
getServiceId,
1619
logOnRequest,
@@ -25,8 +28,9 @@ import { HttpRequest, HttpResponse } from '@aws-sdk/protocol-http'
2528
import { assertLogsContain, assertLogsContainAllOf } from '../globalSetup.test'
2629
import { TestSettings } from '../utilities/testSettingsConfiguration'
2730
import { CredentialsShim } from '../../auth/deprecated/loginManager'
28-
import { Credentials } from '@aws-sdk/types'
31+
import { Credentials, MetadataBearer, MiddlewareStack } from '@aws-sdk/types'
2932
import { oneDay } from '../../shared/datetime'
33+
import { ConfiguredRetryStrategy } from '@smithy/util-retry'
3034

3135
describe('AwsClientBuilderV3', function () {
3236
let builder: AWSClientBuilderV3
@@ -69,6 +73,49 @@ describe('AwsClientBuilderV3', function () {
6973
assert.strictEqual(service.config.userAgent[0][0], 'CUSTOM USER AGENT')
7074
})
7175

76+
describe('caching mechanism', function () {
77+
it('avoids recreating client on duplicate calls', async function () {
78+
const firstClient = await builder.getAwsService(TestClient, {})
79+
const secondClient = await builder.getAwsService(TestClient, {})
80+
81+
assert.strictEqual(firstClient.id, secondClient.id)
82+
})
83+
84+
it('recreates client when region changes', async function () {
85+
const firstClient = await builder.getAwsService(TestClient, {}, 'test-region')
86+
const secondClient = await builder.getAwsService(TestClient, {}, 'test-region2')
87+
88+
assert.notStrictEqual(firstClient.id, secondClient.id)
89+
assert.strictEqual(firstClient.args.region, 'test-region')
90+
assert.strictEqual(secondClient.args.region, 'test-region2')
91+
})
92+
93+
it('recreates client when the underlying service changes', async function () {
94+
const firstClient = await builder.getAwsService(TestClient, {})
95+
const secondClient = await builder.getAwsService(TestClient2, {})
96+
97+
assert.notStrictEqual(firstClient.type, secondClient.type)
98+
})
99+
100+
it('recreates client when config options change', async function () {
101+
const firstClient = await builder.getAwsService(TestClient, {
102+
retryStrategy: new ConfiguredRetryStrategy(10),
103+
})
104+
105+
const secondClient = await builder.getAwsService(TestClient, {
106+
retryStrategy: new ConfiguredRetryStrategy(11),
107+
})
108+
109+
const thirdClient = await builder.getAwsService(TestClient, {
110+
retryStrategy: new ConfiguredRetryStrategy(10),
111+
})
112+
113+
assert.notStrictEqual(firstClient.id, secondClient.id)
114+
console.log('%s %s %s', firstClient.id, secondClient.id, thirdClient.id)
115+
assert.strictEqual(firstClient.id, thirdClient.id)
116+
})
117+
})
118+
72119
describe('middlewareStack', function () {
73120
let args: { request: { hostname: string; path: string }; input: any }
74121
let context: { clientName?: string; commandName?: string }
@@ -249,3 +296,30 @@ class MockCredentialsShim implements CredentialsShim {
249296
return this.refreshedCredentials
250297
}
251298
}
299+
300+
abstract class TestClientBase implements AwsClient {
301+
public constructor(
302+
public readonly args: AwsClientOptions,
303+
public readonly id: number,
304+
public readonly type: string
305+
) {}
306+
public middlewareStack: { add: MiddlewareStack<any, MetadataBearer>['add'] } = {
307+
add: (_: any, __: any) => {},
308+
}
309+
public async send(command: AwsCommand, options?: any): Promise<any> {}
310+
public destroy(): void {}
311+
}
312+
313+
class TestClient extends TestClientBase {
314+
private static nextId: number = 0
315+
public constructor(args: AwsClientOptions) {
316+
super(args, TestClient.nextId++, 'TestClient')
317+
}
318+
}
319+
320+
class TestClient2 extends TestClientBase {
321+
private static nextId: number = 0
322+
public constructor(args: AwsClientOptions) {
323+
super(args, TestClient2.nextId++, 'TestClient2')
324+
}
325+
}

0 commit comments

Comments
 (0)