<commit_before>// This file had a function whose name is all caps, which displeases eslint
/* eslint new-cap: "off" */

import 'source-map-support/register';
import Promise from 'bluebird';
const sdk = require("../..");
const MatrixScheduler = sdk.MatrixScheduler;
const MatrixError = sdk.MatrixError;
const utils = require("../test-utils");

import expect from 'expect';
import lolex from 'lolex';

describe("MatrixScheduler", function() {
    let clock;
    let scheduler;
    let retryFn;
    let queueFn;
    let defer;
    const roomId = "!foo:bar";
    const eventA = utils.mkMessage({
        user: "@alice:bar", room: roomId, event: true,
    });
    const eventB = utils.mkMessage({
        user: "@alice:bar", room: roomId, event: true,
    });

    beforeEach(function() {
        utils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
        clock = lolex.install();
        scheduler = new MatrixScheduler(function(ev, attempts, err) {
            if (retryFn) {
                return retryFn(ev, attempts, err);
            }
            return -1;
        }, function(event) {
            if (queueFn) {
                return queueFn(event);
            }
            return null;
        });
        retryFn = null;
        queueFn = null;
        defer = Promise.defer();
    });

    afterEach(function() {
        clock.uninstall();
    });

    it("should process events in a queue in a FIFO manner", async function() {
        retryFn = function() {
            return 0;
        };
        queueFn = function() {
            return "one_big_queue";
        };
        const deferA = Promise.defer();
        const deferB = Promise.defer();
        let yieldedA = false;
        scheduler.setProcessFunction(function(event) {
            if (yieldedA) {
                expect(event).toEqual(eventB);
                return deferB.promise;
            } else {
                yieldedA = true;
                expect(event).toEqual(eventA);
                return deferA.promise;
            }
        });
        const abPromise = Promise.all([
            scheduler.queueEvent(eventA),
            scheduler.queueEvent(eventB),
        ]);
        deferB.resolve({b: true});
        deferA.resolve({a: true});
        const [a, b] = await abPromise;
        expect(a.a).toEqual(true);
        expect(b.b).toEqual(true);
    });

    it("should invoke the retryFn on failure and wait the amount of time specified",
    async function() {
        const waitTimeMs = 1500;
        const retryDefer = Promise.defer();
        retryFn = function() {
            retryDefer.resolve();
            return waitTimeMs;
        };
        queueFn = function() {
            return "yep";
        };

        let procCount = 0;
        scheduler.setProcessFunction(function(ev) {
            procCount += 1;
            if (procCount === 1) {
                expect(ev).toEqual(eventA);
                return defer.promise;
            } else if (procCount === 2) {
                // don't care about this defer
                return new Promise();
            }
            expect(procCount).toBeLessThan(3);
        });

        scheduler.queueEvent(eventA);
        // as queing doesn't start processing
        // synchronously anymore (see commit bbdb5ac)
        // wait just long enough before it does
        await Promise.resolve();
        expect(procCount).toEqual(1);
        defer.reject({});
        await retryDefer.promise;
        expect(procCount).toEqual(1);
        clock.tick(waitTimeMs);
        await Promise.resolve();
        expect(procCount).toEqual(2);
    });<commit_msg>comment typo<commit_after>// This file had a function whose name is all caps, which displeases eslint
/* eslint new-cap: "off" */

import 'source-map-support/register';
import Promise from 'bluebird';
const sdk = require("../..");
const MatrixScheduler = sdk.MatrixScheduler;
const MatrixError = sdk.MatrixError;
const utils = require("../test-utils");

import expect from 'expect';
import lolex from 'lolex';

describe("MatrixScheduler", function() {
    let clock;
    let scheduler;
    let retryFn;
    let queueFn;
    let defer;
    const roomId = "!foo:bar";
    const eventA = utils.mkMessage({
        user: "@alice:bar", room: roomId, event: true,
    });
    const eventB = utils.mkMessage({
        user: "@alice:bar", room: roomId, event: true,
    });

    beforeEach(function() {
        utils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
        clock = lolex.install();
        scheduler = new MatrixScheduler(function(ev, attempts, err) {
            if (retryFn) {
                return retryFn(ev, attempts, err);
            }
            return -1;
        }, function(event) {
            if (queueFn) {
                return queueFn(event);
            }
            return null;
        });
        retryFn = null;
        queueFn = null;
        defer = Promise.defer();
    });

    afterEach(function() {
        clock.uninstall();
    });

    it("should process events in a queue in a FIFO manner", async function() {
        retryFn = function() {
            return 0;
        };
        queueFn = function() {
            return "one_big_queue";
        };
        const deferA = Promise.defer();
        const deferB = Promise.defer();
        let yieldedA = false;
        scheduler.setProcessFunction(function(event) {
            if (yieldedA) {
                expect(event).toEqual(eventB);
                return deferB.promise;
            } else {
                yieldedA = true;
                expect(event).toEqual(eventA);
                return deferA.promise;
            }
        });
        const abPromise = Promise.all([
            scheduler.queueEvent(eventA),
            scheduler.queueEvent(eventB),
        ]);
        deferB.resolve({b: true});
        deferA.resolve({a: true});
        const [a, b] = await abPromise;
        expect(a.a).toEqual(true);
        expect(b.b).toEqual(true);
    });

    it("should invoke the retryFn on failure and wait the amount of time specified",
    async function() {
        const waitTimeMs = 1500;
        const retryDefer = Promise.defer();
        retryFn = function() {
            retryDefer.resolve();
            return waitTimeMs;
        };
        queueFn = function() {
            return "yep";
        };

        let procCount = 0;
        scheduler.setProcessFunction(function(ev) {
            procCount += 1;
            if (procCount === 1) {
                expect(ev).toEqual(eventA);
                return defer.promise;
            } else if (procCount === 2) {
                // don't care about this defer
                return new Promise();
            }
            expect(procCount).toBeLessThan(3);
        });

        scheduler.queueEvent(eventA);
        // as queueing doesn't start processing
        // synchronously anymore (see commit bbdb5ac)
        // wait just long enough before it does
        await Promise.resolve();
        expect(procCount).toEqual(1);
        defer.reject({});
        await retryDefer.promise;
        expect(procCount).toEqual(1);
        clock.tick(waitTimeMs);
        await Promise.resolve();
        expect(procCount).toEqual(2);
    });
{'fraction_non_alphanumeric': 0.12057811424638679, 'fraction_numerical': 0.0041293874741913286, 'mean_word_length': 1.8054054054054054, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7630644', 'n_tokens_mistral': 2061, 'n_tokens_neox': 2010, 'n_words': 503}