Skip to content

Commit

Permalink
make unit tests faster (#241847)
Browse files Browse the repository at this point in the history
  • Loading branch information
joaomoreno authored Feb 25, 2025
1 parent 649a74e commit ae43cc7
Show file tree
Hide file tree
Showing 6 changed files with 340 additions and 248 deletions.
63 changes: 33 additions & 30 deletions src/vs/base/test/browser/ui/tree/asyncDataTree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { IAsyncDataSource, ITreeNode } from '../../../../browser/ui/tree/tree.js
import { timeout } from '../../../../common/async.js';
import { Iterable } from '../../../../common/iterator.js';
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../common/utils.js';
import { runWithFakedTimers } from '../../../common/timeTravelScheduler.js';

interface Element {
id: string;
Expand Down Expand Up @@ -465,45 +466,47 @@ suite('AsyncDataTree', function () {
});

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

const calls: Function[] = [];
const dataSource = new class implements IAsyncDataSource<Element, Element> {
hasChildren(element: Element): boolean {
return !!element.children && element.children.length > 0;
}
getChildren(element: Element): Promise<Element[]> {
return new Promise(c => calls.push(() => c(element.children || [])));
}
};
const calls: Function[] = [];
const dataSource = new class implements IAsyncDataSource<Element, Element> {
hasChildren(element: Element): boolean {
return !!element.children && element.children.length > 0;
}
getChildren(element: Element): Promise<Element[]> {
return new Promise(c => calls.push(() => c(element.children || [])));
}
};

const model = new Model({
id: 'root',
children: [{
id: 'a', children: [{
id: 'aa'
const model = new Model({
id: 'root',
children: [{
id: 'a', children: [{
id: 'aa'
}]
}]
}]
});
});

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

const pSetInput = tree.setInput(model.root);
calls.pop()!(); // resolve getChildren(root)
await pSetInput;
const pSetInput = tree.setInput(model.root);
calls.pop()!(); // resolve getChildren(root)
await pSetInput;

const pExpandA = tree.expand(model.get('a'));
assert.strictEqual(calls.length, 1, 'expand(a) should\'ve called getChildren(a)');
const pExpandA = tree.expand(model.get('a'));
assert.strictEqual(calls.length, 1, 'expand(a) should\'ve called getChildren(a)');

let race = await Promise.race([pExpandA.then(() => 'expand'), timeout(1).then(() => 'timeout')]);
assert.strictEqual(race, 'timeout', 'expand(a) should not be yet done');
let race = await Promise.race([pExpandA.then(() => 'expand'), timeout(1).then(() => 'timeout')]);
assert.strictEqual(race, 'timeout', 'expand(a) should not be yet done');

calls.pop()!();
assert.strictEqual(calls.length, 0, 'no pending getChildren calls');
calls.pop()!();
assert.strictEqual(calls.length, 0, 'no pending getChildren calls');

race = await Promise.race([pExpandA.then(() => 'expand'), timeout(1).then(() => 'timeout')]);
assert.strictEqual(race, 'expand', 'expand(a) should now be done');
race = await Promise.race([pExpandA.then(() => 'expand'), timeout(1).then(() => 'timeout')]);
assert.strictEqual(race, 'expand', 'expand(a) should now be done');
});
});

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

0 comments on commit ae43cc7

Please sign in to comment.