@@ -10,7 +10,10 @@ import { FakeMemento } from '../fakeExtensionContext'
1010import { FakeAwsContext } from '../utilities/fakeAwsContext'
1111import { GlobalState } from '../../shared/globalState'
1212import {
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'
2528import { assertLogsContain , assertLogsContainAllOf } from '../globalSetup.test'
2629import { TestSettings } from '../utilities/testSettingsConfiguration'
2730import { CredentialsShim } from '../../auth/deprecated/loginManager'
28- import { Credentials } from '@aws-sdk/types'
31+ import { Credentials , MetadataBearer , MiddlewareStack } from '@aws-sdk/types'
2932import { oneDay } from '../../shared/datetime'
33+ import { ConfiguredRetryStrategy } from '@smithy/util-retry'
3034
3135describe ( '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