Support tracking multiple leaks

This commit is contained in:
Peng Lyu 2024-08-02 12:28:02 -07:00
Родитель 4a8f1db241
Коммит 317a4589c2
2 изменённых файлов: 25 добавлений и 14 удалений

Просмотреть файл

@ -11,27 +11,38 @@ export class Profiler {
constructor(private readonly code: Code) {
}
async checkLeaks(className: string, fn: () => Promise<void>): Promise<void> {
async checkLeaks(classNames: string | string[], fn: () => Promise<void>): Promise<void> {
await this.code.driver.startCDP();
const instancesBefore = await getInstances(this.code.driver);
const matchedInstances = instancesBefore.find(e => e.name !== undefined && e.name === className);
if (!matchedInstances) {
throw new Error(`${className} not found`);
}
const countBefore = matchedInstances.count;
const countsBefore: { [key: string]: number } = {};
const instancesBefore = await getInstances(this.code.driver);
const classNamesArray = Array.isArray(classNames) ? classNames : [classNames];
for (const className of classNamesArray) {
const matchedInstances = instancesBefore.find(e => e.name !== undefined && e.name === className);
if (!matchedInstances) {
throw new Error(`${className} not found`);
}
countsBefore[className] = matchedInstances.count;
}
await fn();
const instancesAfter = await getInstances(this.code.driver);
const matchedInstancesAfter = instancesAfter.find(e => e.name !== undefined && e.name === className);
if (!matchedInstancesAfter) {
throw new Error(`${className} not found`);
const leaks: string[] = [];
for (const className of classNamesArray) {
const matchedInstancesAfter = instancesAfter.find(e => e.name !== undefined && e.name === className);
if (!matchedInstancesAfter) {
throw new Error(`${className} not found`);
}
const countAfter = matchedInstancesAfter.count;
if (countAfter !== countsBefore[className]) {
leaks.push(`Leaked ${countAfter - countsBefore[className]} ${className}`);
}
}
const countAfter = matchedInstancesAfter.count;
if (countAfter !== countBefore) {
throw new Error(`Leaked ${countAfter - countBefore} ${className}`);
if (leaks.length > 0) {
throw new Error(leaks.join('\n'));
}
}
}

Просмотреть файл

@ -27,7 +27,7 @@ export function setup(logger: Logger) {
it('leaks check', async function () {
const app = this.app as Application;
await app.profiler.checkLeaks('NotebookTextModel', async () => {
await app.profiler.checkLeaks(['NotebookTextModel', 'NotebookCellTextModel'], async () => {
await app.workbench.notebook.openNotebook();
await app.workbench.quickaccess.runCommand('workbench.action.files.save');
await app.workbench.quickaccess.runCommand('workbench.action.closeActiveEditor');