<commit_before>'use strict';

global.__jymfony = global.__jymfony || {};

/**
 * Applies a timeout to the promise.
 *
 * @param {int} timeoutMs Timeout of the promise in ms
 * @param {Promise|AsyncFunction} promise The promise (or async function) to complete before timeout
 * @param {Error} timeoutError The error to throw in case of timeout
 * @param {boolean} weak Call unref on timeout to not prevent process exit (node.js only)
 */
__jymfony.promiseTimeout = (timeoutMs, promise, timeoutError = new Error('Timed out.'), weak = false) => {
    if ('function' === typeof promise) {
        promise = promise();
    }

    if (! 'then' in promise || 'function' !== typeof promise.then) {
        throw new TypeError('Argument #2 passed to __jymfony.promiseTimeout must be a promise or an async function');
    }


    let rejectFn, resolved = false;
    const clear = () => {
        resolved = true;
        clearTimeout(this._timeout);
    };

    const rejection = err => {
        if (resolved) {
            return;
        }

        clear();
        rejectFn(err);
    };

    this._timeout = setTimeout(() => {
        rejection(timeoutError);
    }, timeoutMs);

    if (weak && 'unref' in this._timeout) {
        this._timeout.unref();
    }

    return new Promise((resolve, reject) => {
        rejectFn = reject;
        promise.then((...args) => {
            clear();
            resolve(...args);
        }, rejection);
    });
};
<commit_msg>[Testing] add TimeSensitiveTestCaseTrait to correctly test timeouts and time-based events<commit_after>'use strict';

global.__jymfony = global.__jymfony || {};

/**
 * Applies a timeout to the promise.
 *
 * @param {int} timeoutMs Timeout of the promise in ms
 * @param {Promise|AsyncFunction} promise The promise (or async function) to complete before timeout
 * @param {Error} timeoutError The error to throw in case of timeout
 * @param {boolean} weak Call unref on timeout to not prevent process exit (node.js only)
 */
__jymfony.promiseTimeout = (timeoutMs, promise, timeoutError = new Error('Timed out.'), weak = false) => {
    if ('function' === typeof promise) {
        promise = promise();
    }

    if (! 'then' in promise || 'function' !== typeof promise.then) {
        throw new TypeError('Argument #2 passed to __jymfony.promiseTimeout must be a promise or an async function');
    }


    let rejectFn, resolved = false;
    const clear = () => {
        resolved = true;
        clearTimeout(this._timeout);
    };

    const rejection = err => {
        if (resolved) {
            return;
        }

        clear();
        rejectFn(err);
    };

    this._timeout = setTimeout(() => {
        rejection(timeoutError);
    }, timeoutMs);

    if (weak && this._timeout && 'unref' in this._timeout) {
        this._timeout.unref();
    }

    return new Promise((resolve, reject) => {
        rejectFn = reject;
        promise.then((...args) => {
            clear();
            resolve(...args);
        }, rejection);
    });
};

{'fraction_non_alphanumeric': 0.1319191249585681, 'fraction_numerical': 0.0006629101756711965, 'mean_word_length': 2.5174825174825175, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '3072710', 'n_tokens_mistral': 850, 'n_tokens_neox': 834, 'n_words': 270}