Fix MdDocumentInfoCache computing values twice (#152799)

* Fix MdDocumentInfoCache computing values twice

Fixes a race where values could be computed twice before being cached

* Remove only
This commit is contained in:
Matt Bierner
2022-06-21 16:22:07 -07:00
committed by GitHub
parent 3406329190
commit 389aa8a935
2 changed files with 72 additions and 17 deletions

View File

@@ -0,0 +1,33 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import 'mocha';
import { InMemoryDocument } from '../util/inMemoryDocument';
import { MdDocumentInfoCache } from '../util/workspaceCache';
import { InMemoryWorkspaceMarkdownDocuments } from './inMemoryWorkspace';
import { workspacePath } from './util';
suite('DocumentInfoCache', () => {
test('Repeated calls should only compute value once', async () => {
const doc = workspacePath('doc.md');
const workspace = new InMemoryWorkspaceMarkdownDocuments([
new InMemoryDocument(doc, '')
]);
let i = 0;
const cache = new MdDocumentInfoCache<number>(workspace, async () => {
return ++i;
});
const a = cache.get(doc);
const b = cache.get(doc);
assert.strictEqual(await a, 1);
assert.strictEqual(i, 1);
assert.strictEqual(await b, 1);
assert.strictEqual(i, 1);
});
});