Skip to content

Commit 4ddea68

Browse files
authored
test(cwl): Fix TailLogGroup test not disposing its event listeners #6267
## Problem Currently, the TailLogGroup tests are passing, but emitting noisy errors in the CI logs: ``` <...> No LiveTail session found for URI: test-region:test-log-group:all: Error: No LiveTail session found for URI: test-region:test-log-group:all at D:\a\aws-toolkit-vscode\aws-toolkit-vscode\packages\core\src\awsService\cloudWatchLogs\commands\tailLogGroup.ts:94:19 at AsyncLocalStorage.run (node:async_hooks:346:14) <...> ``` The cause for this, is that the event listener for closing the tailing session when Editor tabs close in TailLogGroup is not being disposed of. Disposal happens [here](https://github.com/aws/aws-toolkit-vscode/blob/master/packages/core/src/awsService/cloudWatchLogs/commands/tailLogGroup.ts#L80). However, currently the "mock response stream" in the test blocks indefinitely on an [un-resolvable promise](https://github.com/aws/aws-toolkit-vscode/blob/master/packages/core/src/test/awsService/cloudWatchLogs/commands/tailLogGroup.test.ts#L67). So the test ends, and this finally block never triggers. ## Solution Instead of using an un-resolving promise to keep the mock responseStream open, use an AbortController. When the tests no longer need the functionality of the disposables, the test can fire the AbortController. This causes the stream to exit exceptionally, which triggers the `finally` block in TailLogGroup and runs disposal. This stops the event listeners from running after the test is completed, and has eliminated the noisy logs. I have tested this by running `npm run test` from the CLI.
1 parent 04a5b68 commit 4ddea68

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

packages/core/src/test/awsService/cloudWatchLogs/commands/tailLogGroup.test.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,21 @@ describe('TailLogGroup', function () {
5656
getSessionUpdateFrame(false, `${testMessage}-2`, startTimestamp + 2000),
5757
getSessionUpdateFrame(false, `${testMessage}-3`, startTimestamp + 3000),
5858
]
59-
// Returns the configured update frames and then indefinitely blocks.
59+
// Returns the configured update frames and then blocks until an AbortController is signaled.
6060
// This keeps the stream 'open', simulating an open network stream waiting for new events.
6161
// If the stream were to close, the event listeners in the TailLogGroup command would dispose,
6262
// breaking the 'closes tab closes session' assertions this test makes.
63-
async function* generator(): AsyncIterable<StartLiveTailResponseStream> {
63+
const controller = new AbortController()
64+
const p = new Promise((resolve, reject) => {
65+
controller.signal.addEventListener('abort', () => {
66+
reject()
67+
})
68+
})
69+
async function* generator() {
6470
for (const frame of updateFrames) {
6571
yield frame
6672
}
67-
await new Promise(() => {})
73+
await p
6874
}
6975

7076
startLiveTailSessionSpy = sandbox
@@ -84,10 +90,14 @@ describe('TailLogGroup', function () {
8490
})
8591

8692
// The mock stream doesn't 'close', causing tailLogGroup to not return. If we `await`, it will never resolve.
87-
// Run it in the background and use waitUntil to poll its state.
93+
// Run it in the background and use waitUntil to poll its state. Due to the test setup, we expect this to throw
94+
// after the abortController is fired at the end of the test.
8895
void tailLogGroup(registry, testSource, codeLensProvider, {
8996
groupName: testLogGroup,
9097
regionName: testRegion,
98+
}).catch((e) => {
99+
const err = e as Error
100+
assert.strictEqual(err.message.startsWith('Unexpected on-stream exception while tailing session:'), true)
91101
})
92102
await waitUntil(async () => registry.size !== 0, { interval: 100, timeout: 1000 })
93103

@@ -121,6 +131,11 @@ describe('TailLogGroup', function () {
121131
tabs = tabs.concat(getLiveTailSessionTabsFromTabGroup(tabGroup, sessionUri!))
122132
})
123133
await Promise.all(tabs.map((tab) => window.tabGroups.close(tab)))
134+
135+
// Before the test ends, signal the abort controller, interrupting the mock response stream. This
136+
// causes `handleSessionStream` in TailLogGroup to throw, triggering the disposables to dispose.
137+
controller.abort()
138+
124139
assert.strictEqual(registry.size, 0)
125140
assert.strictEqual(stopLiveTailSessionSpy.calledOnce, true)
126141
})

0 commit comments

Comments
 (0)