ensureNoDisposablesAreLeakedInTestSuite: paging (#192477)

* ensureNoDisposablesAreLeakedInTestSuite: paging

related to #190503

* fix hygiene
This commit is contained in:
João Moreno
2023-09-08 14:16:40 +02:00
committed by GitHub
parent 758b4561b2
commit e2aa12d977
2 changed files with 18 additions and 13 deletions

View File

@@ -118,7 +118,7 @@ export class PagedModel<T> implements IPagedModel<T> {
});
}
cancellationToken.onCancellationRequested(() => {
const listener = cancellationToken.onCancellationRequested(() => {
if (!page.cts) {
return;
}
@@ -132,7 +132,8 @@ export class PagedModel<T> implements IPagedModel<T> {
page.promiseIndexes.add(index);
return page.promise.then(() => page.elements[indexInPage]);
return page.promise.then(() => page.elements[indexInPage])
.finally(() => listener.dispose());
}
}

View File

@@ -4,13 +4,15 @@
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { disposableTimeout } from 'vs/base/common/async';
import { CancellationToken, CancellationTokenSource } from 'vs/base/common/cancellation';
import { canceled, isCancellationError } from 'vs/base/common/errors';
import { CancellationError, isCancellationError } from 'vs/base/common/errors';
import { IPager, PagedModel } from 'vs/base/common/paging';
import { ensureNoDisposablesAreLeakedInTestSuite } from 'vs/base/test/common/utils';
function getPage(pageIndex: number, cancellationToken: CancellationToken): Promise<number[]> {
if (cancellationToken.isCancellationRequested) {
return Promise.reject(canceled());
return Promise.reject(new CancellationError());
}
return Promise.resolve([0, 1, 2, 3, 4].map(i => i + (pageIndex * 5)));
@@ -30,6 +32,8 @@ class TestPager implements IPager<number> {
suite('PagedModel', () => {
const store = ensureNoDisposablesAreLeakedInTestSuite();
test('isResolved', () => {
const pager = new TestPager();
const model = new PagedModel(pager);
@@ -116,11 +120,11 @@ suite('PagedModel', () => {
test('cancellation works', function () {
const pager = new TestPager((_, token) => new Promise((_, e) => {
token.onCancellationRequested(() => e(canceled()));
store.add(token.onCancellationRequested(() => e(new CancellationError())));
}));
const model = new PagedModel(pager);
const tokenSource = new CancellationTokenSource();
const tokenSource = store.add(new CancellationTokenSource());
const promise = model.resolve(5, tokenSource.token).then(
() => assert(false),
@@ -139,10 +143,10 @@ suite('PagedModel', () => {
state = 'resolving';
return new Promise((_, e) => {
token.onCancellationRequested(() => {
store.add(token.onCancellationRequested(() => {
state = 'idle';
e(canceled());
});
e(new CancellationError());
}));
});
});
@@ -166,17 +170,17 @@ suite('PagedModel', () => {
assert.strictEqual(state, 'resolving');
setTimeout(() => {
store.add(disposableTimeout(() => {
assert.strictEqual(state, 'resolving');
tokenSource1.cancel();
assert.strictEqual(state, 'resolving');
setTimeout(() => {
store.add(disposableTimeout(() => {
assert.strictEqual(state, 'resolving');
tokenSource2.cancel();
assert.strictEqual(state, 'idle');
}, 10);
}, 10);
}, 10));
}, 10));
return Promise.all([promise1, promise2]);
});