Skip to content

Commit

Permalink
fix(test): unreliable "AuthUtil CodeWhisperer uses fallback connection"
Browse files Browse the repository at this point in the history
#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.
  • Loading branch information
nkomonen-amazon authored Oct 15, 2024
1 parent 11b1ce6 commit ebcb987
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
12 changes: 5 additions & 7 deletions packages/amazonq/test/unit/codewhisperer/util/authUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import {
} from 'aws-core-vscode/codewhisperer'
import {
assertTelemetry,
captureEventOnce,
getTestWindow,
SeverityLevel,
createBuilderIdProfile,
createSsoProfile,
createTestAuth,
captureEventNTimes,
} from 'aws-core-vscode/test'
import { Auth, Connection, isAnySsoConnection, isBuilderIdConnection } from 'aws-core-vscode/auth'
import { globals, vscodeComponent } from 'aws-core-vscode/shared'
Expand Down Expand Up @@ -231,15 +231,13 @@ describe('AuthUtil', async function () {
assert.strictEqual(auth.activeConnection?.id, authUtil.conn?.id)

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

// Is using the fallback connection
// TODO in a refactor see if we can simplify multiple multiple triggers on the same event.
assert.ok(authUtil.isConnected())
assert.ok(authUtil.isUsingSavedConnection)
assert.notStrictEqual(auth.activeConnection?.id, authUtil.conn?.id)
Expand Down
11 changes: 10 additions & 1 deletion packages/core/src/test/testUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,9 +565,18 @@ export function captureEvent<T>(event: vscode.Event<T>): EventCapturer<T> {
* Captures the first value emitted by an event, optionally with a timeout
*/
export function captureEventOnce<T>(event: vscode.Event<T>, timeout?: number): Promise<T> {
return captureEventNTimes(event, 1, timeout)
}

export function captureEventNTimes<T>(event: vscode.Event<T>, amount: number, timeout?: number): Promise<T> {
return new Promise<T>((resolve, reject) => {
const stop = () => reject(new Error('Timed out waiting for event'))
event((data) => resolve(data))
let count = 0
event((data) => {
if (++count === amount) {
resolve(data)
}
})

if (timeout !== undefined) {
setTimeout(stop, timeout)
Expand Down

0 comments on commit ebcb987

Please sign in to comment.