Skip to content

Commit ae43cc7

Browse files
authoredFeb 25, 2025
make unit tests faster (microsoft#241847)
1 parent 649a74e commit ae43cc7

File tree

6 files changed

+340
-248
lines changed

6 files changed

+340
-248
lines changed
 

‎src/vs/base/test/browser/ui/tree/asyncDataTree.test.ts

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { IAsyncDataSource, ITreeNode } from '../../../../browser/ui/tree/tree.js
1212
import { timeout } from '../../../../common/async.js';
1313
import { Iterable } from '../../../../common/iterator.js';
1414
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../common/utils.js';
15+
import { runWithFakedTimers } from '../../../common/timeTravelScheduler.js';
1516

1617
interface Element {
1718
id: string;
@@ -465,45 +466,47 @@ suite('AsyncDataTree', function () {
465466
});
466467

467468
test('issue #80098 - first expand should call getChildren', async () => {
468-
const container = document.createElement('div');
469+
return runWithFakedTimers({ useFakeTimers: true }, async () => {
470+
const container = document.createElement('div');
469471

470-
const calls: Function[] = [];
471-
const dataSource = new class implements IAsyncDataSource<Element, Element> {
472-
hasChildren(element: Element): boolean {
473-
return !!element.children && element.children.length > 0;
474-
}
475-
getChildren(element: Element): Promise<Element[]> {
476-
return new Promise(c => calls.push(() => c(element.children || [])));
477-
}
478-
};
472+
const calls: Function[] = [];
473+
const dataSource = new class implements IAsyncDataSource<Element, Element> {
474+
hasChildren(element: Element): boolean {
475+
return !!element.children && element.children.length > 0;
476+
}
477+
getChildren(element: Element): Promise<Element[]> {
478+
return new Promise(c => calls.push(() => c(element.children || [])));
479+
}
480+
};
479481

480-
const model = new Model({
481-
id: 'root',
482-
children: [{
483-
id: 'a', children: [{
484-
id: 'aa'
482+
const model = new Model({
483+
id: 'root',
484+
children: [{
485+
id: 'a', children: [{
486+
id: 'aa'
487+
}]
485488
}]
486-
}]
487-
});
489+
});
488490

489-
const tree = store.add(new AsyncDataTree<Element, Element>('test', container, new VirtualDelegate(), [new Renderer()], dataSource, { identityProvider: new IdentityProvider() }));
490-
tree.layout(200);
491+
const tree = store.add(new AsyncDataTree<Element, Element>('test', container, new VirtualDelegate(), [new Renderer()], dataSource, { identityProvider: new IdentityProvider() }));
492+
tree.layout(200);
491493

492-
const pSetInput = tree.setInput(model.root);
493-
calls.pop()!(); // resolve getChildren(root)
494-
await pSetInput;
494+
const pSetInput = tree.setInput(model.root);
495+
calls.pop()!(); // resolve getChildren(root)
496+
await pSetInput;
495497

496-
const pExpandA = tree.expand(model.get('a'));
497-
assert.strictEqual(calls.length, 1, 'expand(a) should\'ve called getChildren(a)');
498+
const pExpandA = tree.expand(model.get('a'));
499+
assert.strictEqual(calls.length, 1, 'expand(a) should\'ve called getChildren(a)');
498500

499-
let race = await Promise.race([pExpandA.then(() => 'expand'), timeout(1).then(() => 'timeout')]);
500-
assert.strictEqual(race, 'timeout', 'expand(a) should not be yet done');
501+
let race = await Promise.race([pExpandA.then(() => 'expand'), timeout(1).then(() => 'timeout')]);
502+
assert.strictEqual(race, 'timeout', 'expand(a) should not be yet done');
501503

502-
calls.pop()!();
503-
assert.strictEqual(calls.length, 0, 'no pending getChildren calls');
504+
calls.pop()!();
505+
assert.strictEqual(calls.length, 0, 'no pending getChildren calls');
504506

505-
race = await Promise.race([pExpandA.then(() => 'expand'), timeout(1).then(() => 'timeout')]);
506-
assert.strictEqual(race, 'expand', 'expand(a) should now be done');
507+
race = await Promise.race([pExpandA.then(() => 'expand'), timeout(1).then(() => 'timeout')]);
508+
assert.strictEqual(race, 'expand', 'expand(a) should now be done');
509+
});
507510
});
508511

509512
test('issue #78388 - tree should react to hasChildren toggles', async () => {

0 commit comments

Comments
 (0)
Please sign in to comment.