Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More test disposables cleanup #192575

Merged
merged 4 commits into from Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/vs/base/test/browser/actionbar.test.ts
Expand Up @@ -6,16 +6,19 @@
import * as assert from 'assert';
import { ActionBar, prepareActions } from 'vs/base/browser/ui/actionbar/actionbar';
import { Action, Separator } from 'vs/base/common/actions';
import { ensureNoDisposablesAreLeakedInTestSuite } from 'vs/base/test/common/utils';

suite('Actionbar', () => {

const store = ensureNoDisposablesAreLeakedInTestSuite();

test('prepareActions()', function () {
const a1 = new Separator();
const a2 = new Separator();
const a3 = new Action('a3');
const a3 = store.add(new Action('a3'));
const a4 = new Separator();
const a5 = new Separator();
const a6 = new Action('a6');
const a6 = store.add(new Action('a6'));
const a7 = new Separator();

const actions = prepareActions([a1, a2, a3, a4, a5, a6, a7]);
Expand All @@ -27,10 +30,10 @@ suite('Actionbar', () => {

test('hasAction()', function () {
const container = document.createElement('div');
const actionbar = new ActionBar(container);
const actionbar = store.add(new ActionBar(container));

const a1 = new Action('a1');
const a2 = new Action('a2');
const a1 = store.add(new Action('a1'));
const a2 = store.add(new Action('a2'));

actionbar.push(a1);
assert.strictEqual(actionbar.hasAction(a1), true);
Expand Down
46 changes: 29 additions & 17 deletions src/vs/base/test/common/async.test.ts
Expand Up @@ -11,14 +11,17 @@ import { isCancellationError } from 'vs/base/common/errors';
import { Event } from 'vs/base/common/event';
import { URI } from 'vs/base/common/uri';
import { runWithFakedTimers } from 'vs/base/test/common/timeTravelScheduler';
import { ensureNoDisposablesAreLeakedInTestSuite } from 'vs/base/test/common/utils';

suite('Async', () => {

const store = ensureNoDisposablesAreLeakedInTestSuite();

suite('cancelablePromise', function () {
test('set token, don\'t wait for inner promise', function () {
let canceled = 0;
const promise = async.createCancelablePromise(token => {
token.onCancellationRequested(_ => { canceled += 1; });
store.add(token.onCancellationRequested(_ => { canceled += 1; }));
return new Promise(resolve => { /*never*/ });
});
const result = promise.then(_ => assert.ok(false), err => {
Expand All @@ -33,7 +36,7 @@ suite('Async', () => {
test('cancel despite inner promise being resolved', function () {
let canceled = 0;
const promise = async.createCancelablePromise(token => {
token.onCancellationRequested(_ => { canceled += 1; });
store.add(token.onCancellationRequested(_ => { canceled += 1; }));
return Promise.resolve(1234);
});
const result = promise.then(_ => assert.ok(false), err => {
Expand All @@ -51,7 +54,7 @@ suite('Async', () => {

const cancellablePromise = async.createCancelablePromise(token => {
order.push('in callback');
token.onCancellationRequested(_ => order.push('cancelled'));
store.add(token.onCancellationRequested(_ => order.push('cancelled')));
return Promise.resolve(1234);
});

Expand All @@ -73,7 +76,7 @@ suite('Async', () => {

const cancellablePromise = async.createCancelablePromise(token => {
order.push('in callback');
token.onCancellationRequested(_ => order.push('cancelled'));
store.add(token.onCancellationRequested(_ => order.push('cancelled')));
return new Promise(c => setTimeout(c.bind(1234), 0));
});

Expand Down Expand Up @@ -797,42 +800,51 @@ suite('Async', () => {
});

test('raceCancellation', async () => {
const cts = new CancellationTokenSource();
const cts = store.add(new CancellationTokenSource());
const ctsTimeout = store.add(new CancellationTokenSource());

let triggered = false;
const p = async.raceCancellation(async.timeout(100).then(() => triggered = true), cts.token);
const timeout = async.timeout(100, ctsTimeout.token);
const p = async.raceCancellation(timeout.then(() => triggered = true), cts.token);
cts.cancel();

await p;

assert.ok(!triggered);
ctsTimeout.cancel();
});

test('raceTimeout', async () => {
const cts = new CancellationTokenSource();
const cts = store.add(new CancellationTokenSource());

// timeout wins
let timedout = false;
let triggered = false;

const p1 = async.raceTimeout(async.timeout(100).then(() => triggered = true), 1, () => timedout = true);
const ctsTimeout1 = store.add(new CancellationTokenSource());
const timeout1 = async.timeout(100, ctsTimeout1.token);
const p1 = async.raceTimeout(timeout1.then(() => triggered = true), 1, () => timedout = true);
cts.cancel();

await p1;

assert.ok(!triggered);
assert.strictEqual(timedout, true);
ctsTimeout1.cancel();

// promise wins
timedout = false;

const p2 = async.raceTimeout(async.timeout(1).then(() => triggered = true), 100, () => timedout = true);
const ctsTimeout2 = store.add(new CancellationTokenSource());
const timeout2 = async.timeout(1, ctsTimeout2.token);
const p2 = async.raceTimeout(timeout2.then(() => triggered = true), 100, () => timedout = true);
cts.cancel();

await p2;

assert.ok(triggered);
assert.strictEqual(timedout, false);
ctsTimeout2.cancel();
});

test('SequencerByKey', async () => {
Expand Down Expand Up @@ -1111,11 +1123,11 @@ suite('Async', () => {
}
};

const worker = new async.ThrottledWorker<number>({
const worker = store.add(new async.ThrottledWorker<number>({
maxWorkChunkSize: 5,
maxBufferedWork: undefined,
throttleDelay: 1
}, handler);
}, handler));

// Work less than chunk size

Expand Down Expand Up @@ -1223,11 +1235,11 @@ suite('Async', () => {
const handled: number[] = [];
const handler = (units: readonly number[]) => handled.push(...units);

const worker = new async.ThrottledWorker<number>({
const worker = store.add(new async.ThrottledWorker<number>({
maxWorkChunkSize: 5,
maxBufferedWork: 5,
throttleDelay: 1
}, handler);
}, handler));

let worked = worker.work([1, 2, 3]);
assert.strictEqual(worked, true);
Expand All @@ -1249,11 +1261,11 @@ suite('Async', () => {
const handled: number[] = [];
const handler = (units: readonly number[]) => handled.push(...units);

const worker = new async.ThrottledWorker<number>({
const worker = store.add(new async.ThrottledWorker<number>({
maxWorkChunkSize: 5,
maxBufferedWork: 5,
throttleDelay: 1
}, handler);
}, handler));

let worked = worker.work([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
assert.strictEqual(worked, false);
Expand All @@ -1268,11 +1280,11 @@ suite('Async', () => {
const handled: number[] = [];
const handler = (units: readonly number[]) => handled.push(...units);

const worker = new async.ThrottledWorker<number>({
const worker = store.add(new async.ThrottledWorker<number>({
maxWorkChunkSize: 5,
maxBufferedWork: undefined,
throttleDelay: 1
}, handler);
}, handler));
worker.dispose();
const worked = worker.work([1, 2, 3]);

Expand Down
3 changes: 3 additions & 0 deletions src/vs/base/test/common/buffer.test.ts
Expand Up @@ -7,9 +7,12 @@ import * as assert from 'assert';
import { timeout } from 'vs/base/common/async';
import { bufferedStreamToBuffer, bufferToReadable, bufferToStream, decodeBase64, encodeBase64, newWriteableBufferStream, readableToBuffer, streamToBuffer, VSBuffer } from 'vs/base/common/buffer';
import { peekStream } from 'vs/base/common/stream';
import { ensureNoDisposablesAreLeakedInTestSuite } from 'vs/base/test/common/utils';

suite('Buffer', () => {

ensureNoDisposablesAreLeakedInTestSuite();

test('issue #71993 - VSBuffer#toString returns numbers', () => {
const data = new Uint8Array([1, 2, 3, 'h'.charCodeAt(0), 'i'.charCodeAt(0), 4, 5]).buffer;
const buffer = VSBuffer.wrap(new Uint8Array(data, 3, 2));
Expand Down
30 changes: 13 additions & 17 deletions src/vs/base/test/common/cancellation.test.ts
Expand Up @@ -4,9 +4,12 @@
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { CancellationToken, CancellationTokenSource } from 'vs/base/common/cancellation';
import { ensureNoDisposablesAreLeakedInTestSuite } from 'vs/base/test/common/utils';

suite('CancellationToken', function () {

const store = ensureNoDisposablesAreLeakedInTestSuite();

test('None', () => {
assert.strictEqual(CancellationToken.None.isCancellationRequested, false);
assert.strictEqual(typeof CancellationToken.None.onCancellationRequested, 'function');
Expand Down Expand Up @@ -35,7 +38,7 @@ suite('CancellationToken', function () {
cancelCount += 1;
}

source.token.onCancellationRequested(onCancel);
store.add(source.token.onCancellationRequested(onCancel));

source.cancel();
source.cancel();
Expand All @@ -48,15 +51,9 @@ suite('CancellationToken', function () {
let count = 0;

const source = new CancellationTokenSource();
source.token.onCancellationRequested(function () {
count += 1;
});
source.token.onCancellationRequested(function () {
count += 1;
});
source.token.onCancellationRequested(function () {
count += 1;
});
store.add(source.token.onCancellationRequested(() => count++));
store.add(source.token.onCancellationRequested(() => count++));
store.add(source.token.onCancellationRequested(() => count++));

source.cancel();
assert.strictEqual(count, 3);
Expand Down Expand Up @@ -85,9 +82,7 @@ suite('CancellationToken', function () {
let count = 0;

const source = new CancellationTokenSource();
source.token.onCancellationRequested(function () {
count += 1;
});
store.add(source.token.onCancellationRequested(() => count++));

source.dispose();
source.cancel();
Expand All @@ -99,9 +94,7 @@ suite('CancellationToken', function () {
let count = 0;

const source = new CancellationTokenSource();
source.token.onCancellationRequested(function () {
count += 1;
});
store.add(source.token.onCancellationRequested(() => count++));

source.dispose(true);
// source.cancel();
Expand All @@ -120,12 +113,15 @@ suite('CancellationToken', function () {
const child = new CancellationTokenSource(parent.token);

let count = 0;
child.token.onCancellationRequested(() => count += 1);
store.add(child.token.onCancellationRequested(() => count++));

parent.cancel();

assert.strictEqual(count, 1);
assert.strictEqual(child.token.isCancellationRequested, true);
assert.strictEqual(parent.token.isCancellationRequested, true);

child.dispose();
parent.dispose();
});
});