<commit_before>import {expect} from 'chai';
import * as Rx from '../../dist/cjs/Rx';

const Scheduler = Rx.Scheduler;
const queue = Scheduler.queue;

/** @test {Scheduler} */
describe('Scheduler.queue', () => {
  it('should switch from synchronous to asynchronous at will', (done: MochaDone) => {
    let lastExecTime = 0;
    let asyncExec = false;
    queue.schedule(function (index) {
      if (index === 0) {
        lastExecTime = queue.now();
        this.schedule(1, 100);
      } else if (index === 1) {
        if (queue.now() - lastExecTime < 100) {
          done(new Error('Execution happened synchronously.'));
        } else {
          asyncExec = true;
          lastExecTime = queue.now();
          this.schedule(2, 0);
        }
      } else if (index === 2) {
        if (asyncExec === false) {
          done(new Error('Execution happened synchronously.'));
        } else {
          done();
        }
      }
    }, 0, 0);
    asyncExec = false;
  });
  it('should unsubscribe the rest of the scheduled actions if an action throws an error', () => {
    const actions = [];
    let action2Exec = false;
    let action3Exec = false;
    let errorValue = undefined;
    try {
      queue.schedule(() => {
        actions.push(
          queue.schedule(() => { throw new Error('oops'); }),
          queue.schedule(() => { action2Exec = true; }),
          queue.schedule(() => { action3Exec = true; })
        );
      });
    } catch (e) {<commit_msg>test(QueueScheduler): use fake timers for async schedule (#1862)<commit_after>import {expect} from 'chai';
import * as sinon from 'sinon';
import * as Rx from '../../dist/cjs/Rx';

const Scheduler = Rx.Scheduler;
const queue = Scheduler.queue;

/** @test {Scheduler} */
describe('Scheduler.queue', () => {
  it('should switch from synchronous to asynchronous at will', () => {
    const sandbox = sinon.sandbox.create();
    const fakeTimer = sandbox.useFakeTimers();

    let asyncExec = false;
    let state: Array<number> = [];

    queue.schedule(function (index) {
      state.push(index);
      if (index === 0) {
        this.schedule(1, 100);
      } else if (index === 1) {
        asyncExec = true;
        this.schedule(2, 0);
      }
    }, 0, 0);

    expect(asyncExec).to.be.false;
    expect(state).to.be.deep.equal([0]);

    fakeTimer.tick(100);

    expect(asyncExec).to.be.true;
    expect(state).to.be.deep.equal([0, 1, 2]);

    sandbox.restore();
  });

  it('should unsubscribe the rest of the scheduled actions if an action throws an error', () => {
    const actions = [];
    let action2Exec = false;
    let action3Exec = false;
    let errorValue = undefined;
    try {
      queue.schedule(() => {
        actions.push(
          queue.schedule(() => { throw new Error('oops'); }),
          queue.schedule(() => { action2Exec = true; }),
          queue.schedule(() => { action3Exec = true; })
        );
      });
    } catch (e) {
{'fraction_non_alphanumeric': 0.16252129471890972, 'fraction_numerical': 0.014991482112436116, 'mean_word_length': 2.3708381171067736, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 18, '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': '4808853', 'n_tokens_mistral': 927, 'n_tokens_neox': 882, 'n_words': 235}