Skip to content

Commit 30face9

Browse files
authored
test(flaky): avoid relying on race condition for tested behavior #6452
## Problem #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.
1 parent 3a03e95 commit 30face9

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

packages/core/src/test/shared/fs/fs.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,16 +411,20 @@ describe('FileSystem', function () {
411411
const oldPath = testFolder.pathFrom('oldFile.txt')
412412
const newPath = testFolder.pathFrom('newFile.txt')
413413

414-
const result = fs.rename(oldPath, newPath)
415-
// this file is created after the first "exists" check fails, the following check should pass
416-
void testutil.toFile('hello world', oldPath)
417-
await result
414+
const existsStub = Sinon.stub(FileSystem.prototype, 'exists')
415+
existsStub.onFirstCall().resolves(false)
416+
existsStub.callThrough()
417+
418+
await testutil.toFile('hello world', oldPath)
419+
await fs.rename(oldPath, newPath)
418420

419421
testutil.assertTelemetry('ide_fileSystem', {
420422
action: 'rename',
421423
result: 'Succeeded',
422424
reason: 'RenameRaceCondition',
423425
})
426+
427+
existsStub.restore()
424428
})
425429
})
426430

0 commit comments

Comments
 (0)