<commit_before>/**
 * Created by tushar.mathur on 02/10/16.
 */

import {ITask} from '../types/ITask';
import {IScheduler} from '../types/IScheduler';
import {IObservable} from '../types/core/IObservable';
import {ISubscription} from '../types/core/ISubscription';
import {IEvent} from '../types/IEvent';
import {TestObserver} from './TestObserver';
import {ColdTestObservable} from './ColdTestObservable';
import {ISchedulingStrategy} from '../types/ISchedulingStrategy';
import {HotTestObservable} from './HotTestObservable';

class TaskSchedule {
  constructor (public task: ITask, public time: number) {
  }
}
const MockDisposable = {unsubscribe: (): void => void 0, closed: false}

export const DEFAULT_TIMING = {
  start: 200,
  stop: 2000
}

export class TestScheduler implements IScheduler {
  private clock: number;
  private queue: Array<TaskSchedule>;

  constructor () {
    this.clock = 0
    this.queue = []
  }

  tick () {
    this.clock++
    this.run()
  }

  advanceBy (time: number): void {
    while (time--) this.tick()
  }

  now () {
    return this.clock
  }

  schedule (task: ITask, relativeTime: number): ISubscription {
    this.queue.push(new TaskSchedule(task, relativeTime + this.now()))
    return MockDisposable
  }

  scheduleAbsolute (task: ITask, absoluteTime: number): ISubscription {
    this.queue.push(new TaskSchedule(task, absoluteTime))
    return MockDisposable
  }

  scheduleASAP (task: ITask): ISubscription {
    return this.scheduleAbsolute(task, this.now() + 1)
  }

  scheduleNow (task: ITask): ISubscription {
    return this.scheduleAbsolute(task, this.now())
  }

  scheduleUsing (strategy: ISchedulingStrategy, task: ITask) {
    return strategy.run(task)
  }

  scheduleRepeatedly (task: ITask, interval: number): ISubscription {
    const repeatedTask = () => {
      task()
      this.schedule(repeatedTask, interval)
    }
    this.schedule(repeatedTask, interval)
    return MockDisposable;
  }

  private run () {
    var residual: Array<TaskSchedule> = []
    for (var i = 0; i < this.queue.length; ++i) {
      var qItem = this.queue[i]
      if (qItem.time <= this.clock) {
        qItem.task()
      } else {
        residual.push(qItem)
      }
    }
    this.queue = residual
  }

  start<T> (f: () => IObservable<T>,
            timing: {start: number, stop: number} = DEFAULT_TIMING): TestObserver<T> {
    var subscription: ISubscription
    var resultsObserver = new TestObserver(this);
    this.scheduleAbsolute(() => subscription = f().subscribe(resultsObserver, this), timing.start)
    this.scheduleAbsolute(() => subscription.unsubscribe(), timing.stop)

    this.run()
    this.advanceBy(timing.stop)
    return resultsObserver<commit_msg>refactor(TestScheduler): start() takes schedule times as arguments instead of an object<commit_after>/**
 * Created by tushar.mathur on 02/10/16.
 */

import {ITask} from '../types/ITask';
import {IScheduler} from '../types/IScheduler';
import {IObservable} from '../types/core/IObservable';
import {ISubscription} from '../types/core/ISubscription';
import {IEvent} from '../types/IEvent';
import {TestObserver} from './TestObserver';
import {ColdTestObservable} from './ColdTestObservable';
import {ISchedulingStrategy} from '../types/ISchedulingStrategy';
import {HotTestObservable} from './HotTestObservable';

class TaskSchedule {
  constructor (public task: ITask, public time: number) {
  }
}
const MockDisposable = {unsubscribe: (): void => void 0, closed: false}

export class TestScheduler implements IScheduler {
  private clock: number;
  private queue: Array<TaskSchedule>;

  constructor () {
    this.clock = 0
    this.queue = []
  }

  tick () {
    this.clock++
    this.run()
  }

  advanceBy (time: number): void {
    while (time--) this.tick()
  }

  now () {
    return this.clock
  }

  schedule (task: ITask, relativeTime: number): ISubscription {
    this.queue.push(new TaskSchedule(task, relativeTime + this.now()))
    return MockDisposable
  }

  scheduleAbsolute (task: ITask, absoluteTime: number): ISubscription {
    this.queue.push(new TaskSchedule(task, absoluteTime))
    return MockDisposable
  }

  scheduleASAP (task: ITask): ISubscription {
    return this.scheduleAbsolute(task, this.now() + 1)
  }

  scheduleNow (task: ITask): ISubscription {
    return this.scheduleAbsolute(task, this.now())
  }

  scheduleUsing (strategy: ISchedulingStrategy, task: ITask) {
    return strategy.run(task)
  }

  scheduleRepeatedly (task: ITask, interval: number): ISubscription {
    const repeatedTask = () => {
      task()
      this.schedule(repeatedTask, interval)
    }
    this.schedule(repeatedTask, interval)
    return MockDisposable;
  }

  private run () {
    var residual: Array<TaskSchedule> = []
    for (var i = 0; i < this.queue.length; ++i) {
      var qItem = this.queue[i]
      if (qItem.time <= this.clock) {
        qItem.task()
      } else {
        residual.push(qItem)
      }
    }
    this.queue = residual
  }

  start<T> (f: () => IObservable<T>, start = 200, stop = 2000): TestObserver<T> {
    var subscription: ISubscription
    var resultsObserver = new TestObserver(this);
    this.scheduleAbsolute(() => subscription = f().subscribe(resultsObserver, this), start)
    this.scheduleAbsolute(() => subscription.unsubscribe(), stop)

    this.run()
    this.advanceBy(stop)
    return resultsObserver
{'fraction_non_alphanumeric': 0.13412595207133568, 'fraction_numerical': 0.006316180568456251, 'mean_word_length': 3.539629005059022, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, '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': '2334735', 'n_tokens_mistral': 1705, 'n_tokens_neox': 1647, 'n_words': 412}