Skip to content

Commit 00a6d29

Browse files
nkomonen-amazontverney
authored andcommitted
fix(test): unreliable "AuthUtil CodeWhisperer uses fallback connection" aws#5788
## Problem: This test was flaky due to the event emitter and the test expecting 2 of the same event to come in, but sometimes the second one was not capture. This looks to be due to the second `captureEventOnce` call needing to do some initial setup before it could capture an event. And I think there was a race condition between it being setup in time and the event being emitted before it could start listening. ## Solution: Make a new function which does the event capture once and then listens N amount of times. This is reliable.
1 parent 002d2c4 commit 00a6d29

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

packages/amazonq/test/unit/codewhisperer/util/authUtil.test.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import {
1313
} from 'aws-core-vscode/codewhisperer'
1414
import {
1515
assertTelemetry,
16-
captureEventOnce,
1716
getTestWindow,
1817
SeverityLevel,
1918
createBuilderIdProfile,
2019
createSsoProfile,
2120
createTestAuth,
21+
captureEventNTimes,
2222
} from 'aws-core-vscode/test'
2323
import { Auth, Connection, isAnySsoConnection, isBuilderIdConnection } from 'aws-core-vscode/auth'
2424
import { globals, vscodeComponent } from 'aws-core-vscode/shared'
@@ -231,15 +231,13 @@ describe('AuthUtil', async function () {
231231
assert.strictEqual(auth.activeConnection?.id, authUtil.conn?.id)
232232

233233
// Switch to unsupported connection
234-
const cwAuthUpdatedConnection = captureEventOnce(authUtil.secondaryAuth.onDidChangeActiveConnection)
234+
const cwAuthUpdatedConnection = captureEventNTimes(authUtil.secondaryAuth.onDidChangeActiveConnection, 2)
235235
await auth.useConnection(unsupportedConn)
236-
// This is triggered when the main Auth connection is switched
236+
// - This is triggered when the main Auth connection is switched
237+
// - This is triggered by registerAuthListener() when it saves the previous active connection as a fallback.
237238
await cwAuthUpdatedConnection
238-
// This is triggered by registerAuthListener() when it saves the previous active connection as a fallback.
239-
// TODO in a refactor see if we can simplify multiple multiple triggers on the same event.
240-
await captureEventOnce(authUtil.secondaryAuth.onDidChangeActiveConnection)
241239

242-
// Is using the fallback connection
240+
// TODO in a refactor see if we can simplify multiple multiple triggers on the same event.
243241
assert.ok(authUtil.isConnected())
244242
assert.ok(authUtil.isUsingSavedConnection)
245243
assert.notStrictEqual(auth.activeConnection?.id, authUtil.conn?.id)

packages/core/src/test/testUtil.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,9 +565,18 @@ export function captureEvent<T>(event: vscode.Event<T>): EventCapturer<T> {
565565
* Captures the first value emitted by an event, optionally with a timeout
566566
*/
567567
export function captureEventOnce<T>(event: vscode.Event<T>, timeout?: number): Promise<T> {
568+
return captureEventNTimes(event, 1, timeout)
569+
}
570+
571+
export function captureEventNTimes<T>(event: vscode.Event<T>, amount: number, timeout?: number): Promise<T> {
568572
return new Promise<T>((resolve, reject) => {
569573
const stop = () => reject(new Error('Timed out waiting for event'))
570-
event((data) => resolve(data))
574+
let count = 0
575+
event((data) => {
576+
if (++count === amount) {
577+
resolve(data)
578+
}
579+
})
571580

572581
if (timeout !== undefined) {
573582
setTimeout(stop, timeout)

0 commit comments

Comments
 (0)