From 30face99ec161876378b49e71e78ae3714ed5797 Mon Sep 17 00:00:00 2001 From: Hweinstock <42325418+Hweinstock@users.noreply.github.com> Date: Thu, 30 Jan 2025 11:47:14 -0500 Subject: [PATCH] test(flaky): avoid relying on race condition for tested behavior #6452 ## Problem https://github.com/aws/aws-toolkit-vscode/issues/6451 This rename test relies on a specific result of a race condition for the expected result. - The test is checking for a telemetry result that is only emitted when `fs.exists` takes more than 1 attempt to resolve to true. - Therefore, it wants the first `fs.exists` check to fail, then a subsequent one to succeed. - It does this by not awaiting the result, and then writing the file to be renamed. Usually this is fine, but it is possible that the write (`toFile`) happens before the read (`fs.exists`) since neither is awaited. This behavior leads to a flaky test as described in the issue. ## Solution - use a stub to force the first call to `fs.exists` to fail. - allow all other calls to "go through" to the original function. --- packages/core/src/test/shared/fs/fs.test.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/core/src/test/shared/fs/fs.test.ts b/packages/core/src/test/shared/fs/fs.test.ts index c816ecdde83..15db2c6b73e 100644 --- a/packages/core/src/test/shared/fs/fs.test.ts +++ b/packages/core/src/test/shared/fs/fs.test.ts @@ -411,16 +411,20 @@ describe('FileSystem', function () { const oldPath = testFolder.pathFrom('oldFile.txt') const newPath = testFolder.pathFrom('newFile.txt') - const result = fs.rename(oldPath, newPath) - // this file is created after the first "exists" check fails, the following check should pass - void testutil.toFile('hello world', oldPath) - await result + const existsStub = Sinon.stub(FileSystem.prototype, 'exists') + existsStub.onFirstCall().resolves(false) + existsStub.callThrough() + + await testutil.toFile('hello world', oldPath) + await fs.rename(oldPath, newPath) testutil.assertTelemetry('ide_fileSystem', { action: 'rename', result: 'Succeeded', reason: 'RenameRaceCondition', }) + + existsStub.restore() }) })