mirror of
https://github.com/microsoft/vscode.git
synced 2026-07-12 11:03:02 -05:00
ensureNoDisposablesAreLeakedInTestSuite: paging (#192477)
* ensureNoDisposablesAreLeakedInTestSuite: paging related to #190503 * fix hygiene
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user