Skip to content

Commit 5c56b23

Browse files
refactor to spy on debounced funcitionality
We can now spy on the underlying getToken method to verify it is being debounced as expected. Also reduce the debounce interval to 50ms to reduce the delay but still catch a barrage of calls Signed-off-by: nkomonen-amazon <[email protected]>
1 parent 2efa3ec commit 5c56b23

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

packages/core/src/auth/sso/ssoAccessTokenProvider.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,9 @@ export abstract class SsoAccessTokenProvider {
108108
public async getToken(): Promise<SsoToken | undefined> {
109109
return this.getTokenDebounced()
110110
}
111-
private getTokenDebounced = debounce(this._getToken.bind(this), 100)
112-
private async _getToken(): Promise<SsoToken | undefined> {
111+
private getTokenDebounced = debounce(() => this._getToken(), 50)
112+
/** Exposed for testing purposes only */
113+
public async _getToken(): Promise<SsoToken | undefined> {
113114
const data = await this.cache.token.load(this.tokenCacheKey)
114115
SsoAccessTokenProvider.logIfChanged(
115116
indent(

packages/core/src/test/credentials/sso/ssoAccessTokenProvider.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,15 @@ describe('SsoAccessTokenProvider', function () {
156156
assert.strictEqual(cachedToken, undefined)
157157
})
158158

159-
it('concurrent calls resolve successfully', async function () {
160-
// This test verifies debounced getToken() does not break things.
161-
// But this test is not perfect since we are unable to spy _getToken() for some reason.
159+
it('concurrent calls are debounced', async function () {
162160
const validToken = createToken(hourInMs)
163161
await cache.token.save(startUrl, { region, startUrl, token: validToken })
162+
const actualGetToken = sinon.spy(sut, '_getToken')
164163

165164
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)
166168
for (const r of result) {
167169
assert.deepStrictEqual(r, validToken)
168170
}

0 commit comments

Comments
 (0)