@@ -27,6 +27,7 @@ import { ToolkitError } from '../../../shared/errors'
2727import * as fs from 'fs' // eslint-disable-line no-restricted-imports
2828import * as path from 'path'
2929import { Stub , stub } from '../../utilities/stubber'
30+ import { globals } from '../../../shared'
3031
3132const hourInMs = 3600000
3233
@@ -37,14 +38,14 @@ describe('SsoAccessTokenProvider', function () {
3738 let oidcClient : Stub < OidcClient >
3839 let sut : SsoAccessTokenProvider
3940 let cache : ReturnType < typeof getCache >
40- let clock : FakeTimers . InstalledClock
41+ let clock : FakeTimers . InstalledClock | undefined
4142 let tempDir : string
4243 let reAuthState : TestReAuthState
4344
4445 function createToken ( timeDelta : number , extras : Partial < SsoToken > = { } ) {
4546 return {
4647 accessToken : 'dummyAccessToken' ,
47- expiresAt : new clock . Date ( clock . Date . now ( ) + timeDelta ) ,
48+ expiresAt : new globals . clock . Date ( globals . clock . Date . now ( ) + timeDelta ) ,
4849 ...extras ,
4950 }
5051 }
@@ -54,7 +55,7 @@ describe('SsoAccessTokenProvider', function () {
5455 scopes : [ ] ,
5556 clientId : 'dummyClientId' ,
5657 clientSecret : 'dummyClientSecret' ,
57- expiresAt : new clock . Date ( clock . Date . now ( ) + timeDelta ) ,
58+ expiresAt : new globals . clock . Date ( globals . clock . Date . now ( ) + timeDelta ) ,
5859 startUrl,
5960 ...extras ,
6061 }
@@ -66,7 +67,7 @@ describe('SsoAccessTokenProvider', function () {
6667 deviceCode : 'dummyCode' ,
6768 userCode : 'dummyUserCode' ,
6869 verificationUri : 'dummyLink' ,
69- expiresAt : new clock . Date ( clock . Date . now ( ) + timeDelta ) ,
70+ expiresAt : new globals . clock . Date ( globals . clock . Date . now ( ) + timeDelta ) ,
7071 }
7172 }
7273
@@ -77,14 +78,6 @@ describe('SsoAccessTokenProvider', function () {
7778 return cacheDir
7879 }
7980
80- before ( function ( ) {
81- clock = installFakeClock ( )
82- } )
83-
84- after ( function ( ) {
85- clock . uninstall ( )
86- } )
87-
8881 beforeEach ( async function ( ) {
8982 oidcClient = stub ( OidcClient )
9083 tempDir = await makeTemporaryTokenCacheFolder ( )
@@ -95,7 +88,7 @@ describe('SsoAccessTokenProvider', function () {
9588
9689 afterEach ( async function ( ) {
9790 sinon . restore ( )
98- clock . reset ( )
91+ clock ?. uninstall ( )
9992 await tryRemoveFolder ( tempDir )
10093 } )
10194
@@ -163,6 +156,20 @@ describe('SsoAccessTokenProvider', function () {
163156 assert . strictEqual ( cachedToken , undefined )
164157 } )
165158
159+ it ( 'concurrent calls are debounced' , async function ( ) {
160+ const validToken = createToken ( hourInMs )
161+ await cache . token . save ( startUrl , { region, startUrl, token : validToken } )
162+ const actualGetToken = sinon . spy ( sut , '_getToken' )
163+
164+ const result = await Promise . all ( [ sut . getToken ( ) , sut . getToken ( ) , sut . getToken ( ) ] )
165+
166+ // Subsequent other calls were debounced so this was only called once
167+ assert . strictEqual ( actualGetToken . callCount , 1 )
168+ for ( const r of result ) {
169+ assert . deepStrictEqual ( r , validToken )
170+ }
171+ } )
172+
166173 describe ( 'Exceptions' , function ( ) {
167174 it ( 'drops expired tokens if failure was a client-fault' , async function ( ) {
168175 const exception = new UnauthorizedClientException ( { message : '' , $metadata : { } } )
@@ -267,6 +274,7 @@ describe('SsoAccessTokenProvider', function () {
267274 } )
268275
269276 it ( `emits session duration between logins of the same startUrl` , async function ( ) {
277+ clock = installFakeClock ( )
270278 setupFlow ( )
271279 stubOpen ( )
272280
@@ -311,6 +319,7 @@ describe('SsoAccessTokenProvider', function () {
311319 } )
312320
313321 it ( 'respects the device authorization expiration time' , async function ( ) {
322+ clock = installFakeClock ( )
314323 setupFlow ( )
315324 stubOpen ( )
316325 const exception = new AuthorizationPendingException ( { message : '' , $metadata : { } } )
@@ -352,7 +361,7 @@ describe('SsoAccessTokenProvider', function () {
352361 const registration = {
353362 clientId : 'myExpiredClientId' ,
354363 clientSecret : 'myExpiredClientSecret' ,
355- expiresAt : new clock . Date ( clock . Date . now ( ) - 1 ) , // expired date
364+ expiresAt : new globals . clock . Date ( globals . clock . Date . now ( ) - 1 ) , // expired date
356365 startUrl : key . startUrl ,
357366 }
358367 await cache . registration . save ( key , registration )
0 commit comments