<commit_before>/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

import * as assert from 'assert';
import { TPromise } from 'vs/base/common/winjs.base';
import * as async from 'vs/base/common/async';
import URI from 'vs/base/common/uri';
import { isPromiseCanceledError } from 'vs/base/common/errors';

suite('Async', () => {

	test('cancelablePromise - set token, don\'t wait for inner promise', function () {
		let canceled = 0;
		let promise = async.createCancelablePromise(token => {
			token.onCancellationRequested(_ => { canceled += 1; });
			return new Promise(resolve => { /*never*/ });
		});
		let result = promise.then(_ => assert.ok(false), err => {
			assert.equal(canceled, 1);
			assert.ok(isPromiseCanceledError(err));
		});
		promise.cancel();
		promise.cancel(); // cancel only once
		return result;
	});

	test('cancelablePromise - cancel despite inner promise being resolved', function () {
		let canceled = 0;
		let promise = async.createCancelablePromise(token => {
			token.onCancellationRequested(_ => { canceled += 1; });
			return Promise.resolve(1234);
		});
		let result = promise.then(_ => assert.ok(false), err => {
			assert.equal(canceled, 1);
			assert.ok(isPromiseCanceledError(err));
		});
		promise.cancel();
		return result;
	});

	test('cancelablePromise - get inner result', async function () {
		let promise = async.createCancelablePromise(token => {
			return async.timeout(12).then(_ => 1234);
		});

		let result = await promise;
		assert.equal(result, 1234);
	});

	test('asDisposablePromise', async function () {
		let value = await async.asDisposablePromise(TPromise.as(1)).promise;
		assert.equal(value, 1);

		let disposablePromise = async.asDisposablePromise(TPromise.timeout(1000).then(_ => 1), 2);
		disposablePromise.dispose();
		value = await disposablePromise.promise;
		assert.equal(value, 2);
	});

	test('Throttler - non async', function () {
		let count = 0;
		let factory = () => {
			return TPromise.as(++count);
		};
<commit_msg>async execution order tests<commit_after>/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

import * as assert from 'assert';
import { TPromise } from 'vs/base/common/winjs.base';
import * as async from 'vs/base/common/async';
import URI from 'vs/base/common/uri';
import { isPromiseCanceledError } from 'vs/base/common/errors';

suite('Async', () => {

	test('cancelablePromise - set token, don\'t wait for inner promise', function () {
		let canceled = 0;
		let promise = async.createCancelablePromise(token => {
			token.onCancellationRequested(_ => { canceled += 1; });
			return new Promise(resolve => { /*never*/ });
		});
		let result = promise.then(_ => assert.ok(false), err => {
			assert.equal(canceled, 1);
			assert.ok(isPromiseCanceledError(err));
		});
		promise.cancel();
		promise.cancel(); // cancel only once
		return result;
	});

	test('cancelablePromise - cancel despite inner promise being resolved', function () {
		let canceled = 0;
		let promise = async.createCancelablePromise(token => {
			token.onCancellationRequested(_ => { canceled += 1; });
			return Promise.resolve(1234);
		});
		let result = promise.then(_ => assert.ok(false), err => {
			assert.equal(canceled, 1);
			assert.ok(isPromiseCanceledError(err));
		});
		promise.cancel();
		return result;
	});

	// Cancelling a sync cancelable promise will fire the cancelled token.
	// Also, every `then` callback runs in another execution frame.
	test('CancelablePromise execution order (sync)', function () {
		const order = [];

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

		order.push('afterCreate');

		const promise = cancellablePromise
			.then(null, err => null)
			.then(() => order.push('finally'));

		cancellablePromise.cancel();
		order.push('afterCancel');

		return promise.then(() => assert.deepEqual(order, ['in callback', 'afterCreate', 'cancelled', 'afterCancel', 'finally']));
	});

	// Cancelling an async cancelable promise is just the same as a sync cancellable promise.
	test('CancelablePromise execution order (async)', function () {
		const order = [];

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

		order.push('afterCreate');

		const promise = cancellablePromise
			.then(null, err => null)
			.then(() => order.push('finally'));

		cancellablePromise.cancel();
		order.push('afterCancel');

		return promise.then(() => assert.deepEqual(order, ['in callback', 'afterCreate', 'cancelled', 'afterCancel', 'finally']));
	});

	// Cancelling a sync tpromise will NOT cancel the promise, since it has resolved already.
	// Every `then` callback runs sync in the same execution frame, thus `finally` executes
	// before `afterCancel`.
	test('TPromise execution order (sync)', function () {
		const order = [];
		let promise = new TPromise(resolve => {
			order.push('in executor');
			resolve(1234);
		}, () => order.push('cancelled'));

		order.push('afterCreate');

		promise = promise
			.then(null, err => null)
			.then(() => order.push('finally'));

		promise.cancel();
		order.push('afterCancel');

		return promise.then(() => assert.deepEqual(order, ['in executor', 'afterCreate', 'finally', 'afterCancel']));
	});

	// Cancelling an async tpromise will cancel the promise.
	// Every `then` callback runs sync on the same execution frame as the `cancel` call,
	// so finally still executes before `afterCancel`.
	test('TPromise execution order (async)', function () {
		const order = [];
		let promise = new TPromise(resolve => {
			order.push('in executor');
			setTimeout(() => resolve(1234));
		}, () => order.push('cancelled'));

		order.push('afterCreate');

		promise = promise
			.then(null, err => null)
			.then(() => order.push('finally'));

		promise.cancel();
		order.push('afterCancel');

		return promise.then(() => assert.deepEqual(order, ['in executor', 'afterCreate', 'cancelled', 'finally', 'afterCancel']));
	});

	test('cancelablePromise - get inner result', async function () {
		let promise = async.createCancelablePromise(token => {
			return async.timeout(12).then(_ => 1234);
		});

		let result = await promise;
		assert.equal(result, 1234);
	});

	test('asDisposablePromise', async function () {
		let value = await async.asDisposablePromise(TPromise.as(1)).promise;
		assert.equal(value, 1);

		let disposablePromise = async.asDisposablePromise(TPromise.timeout(1000).then(_ => 1), 2);
		disposablePromise.dispose();
		value = await disposablePromise.promise;
		assert.equal(value, 2);
	});

	test('Throttler - non async', function () {
		let count = 0;
		let factory = () => {
			return TPromise.as(++count);
		};

{'fraction_non_alphanumeric': 0.2002659574468085, 'fraction_numerical': 0.010239361702127659, 'mean_word_length': 3.3650609402205456, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5309331', 'n_tokens_mistral': 2405, 'n_tokens_neox': 2144, 'n_words': 562}