<commit_before>/* Copyright (c) 2016 <NAME>, MIT License */
'use strict'

var Lab = require('lab')
var Code = require('code')

var lab = exports.lab = Lab.script()
var describe = lab.describe
var it = lab.it
var expect = Code.expect

var GateExecutor = require('..')

describe('gate-executor', function () {
  it('readme', function (done) {
    var ge = GateExecutor()

    ge.add({
      fn: function first (done) {
        console.log('first')
        done()
      }
    })

    var subge = ge.gate()

    ge.add({
      fn: function second (done) {
        console.log('second')
        done()
      }
    })

    subge.add({
      fn: function second (done) {
        console.log('third')
        done()
      }
    })

    ge.start(function () {
      console.log('done')
      done()
    })

    // prints:
    //   first
    //   third
    //   second
    //   done
  })

  it('happy', function (done) {
    var log = []

    var ge = GateExecutor()

    ge.add({ fn: function aa (d) { log.push('aa'); d() } })
    ge.add({ fn: function bb (d) { log.push('bb'); d() } })


    var ge2 = ge.gate()
    ge2.add({ fn: function ccc (d) { log.push('ccc'); d() } })

    ge.add({ fn: function dd (d) { log.push('dd'); d() } })

    ge2.add({ fn: function ddd (d) { log.push('ddd'); d() } })

    expect(ge.state()).to.deep.equal([
      { s: 'w', ge: 1, d: 'aa', id: '1' },
      { s: 'w', ge: 1, d: 'bb', id: '2' },
      [ { s: 'w', ge: 2, d: 'ccc', id: '1' },
        { s: 'w', ge: 2, d: 'ddd', id: '2' } ],
      { s: 'w', ge: 1, d: 'dd', id: '4' } ])

    ge2.clear(function () {
      expect(log).to.deep.equal(['aa', 'bb', 'ccc', 'ddd'])
      expect(ge.state()).to.deep.equal([
        { s: 'w', ge: 1, d: 'dd', id: '4' } ])
    })

    ge.clear(function () {
      expect(log).to.deep.equal(['aa', 'bb', 'ccc', 'ddd', 'dd'])
      done()
    })

    ge.start()
  })


  it('running', function (done) {
    var log = []

    var ge = GateExecutor()

    ge.add({ fn: function aa (d) { log.push('aa'); d() } })
    ge.add({ fn: function bb (d) { log.push('bb'); d() } })

    var cc = 0
    ge.clear(function () {
      ++cc
      if (1 === cc) {
        expect(log).to.deep.equal(['aa', 'bb'])
      }
      else if (2 === cc) {
        expect(log).to.deep.equal(['aa', 'bb', 'cc'])
        done()
      }
    })

    ge.start(function () {
      expect(log).to.deep.equal(['aa', 'bb'])
    })

    setImmediate(function () {
      ge.add({ fn: function cc (d) { log.push('cc'); d() } })
    })
  })


  it('timeout', function (done) {
    var log = []

    var ge = GateExecutor({ timeout: 200, interval: 11 })

    ge.add({
      fn: function aa (d) {
        log.push('s-aa')
        setTimeout(function () { log.push('e-aa'); d() }, 100)
      }
    })

    ge.add({
      fn: function bb (d) {
        log.push('s-bb')
        setTimeout(function () { log.push('e-bb'); d() }, 300)
      }
    })

    ge.add({
      fn: function cc (d) {
        log.push('s-cc')
        setTimeout(function () { log.push('e-cc'); d() }, 150)
      }
    })

    ge.add({
      tm: 50,
      ontm: function () {
        log.push('t-dd')
      },
      fn: function dd (d) {
        log.push('s-dd')
        setTimeout(function () { log.push('e-dd'); d() }, 150)
      }
    })

    ge.clear(function () {
      expect(log).to.deep.equal(
        [ 's-aa', 's-bb', 's-cc', 's-dd', 't-dd', 'e-aa', 'e-cc', 'e-dd' ])
      done()
    })

    ge.start()

    setTimeout(function () {
      expect(ge.state()).to.deep.equal([ { s: 'a', ge: 1, d: 'bb', id: '2' } ])
    }, 200)
  })


  it('traverse', function (done) {
<commit_msg>add test to expose timeout bug<commit_after>/* Copyright (c) 2016 <NAME>, MIT License */
'use strict'

var Lab = require('lab')
var Code = require('code')

var lab = exports.lab = Lab.script()
var describe = lab.describe
var it = lab.it
var expect = Code.expect

var GateExecutor = require('..')

describe('gate-executor', function () {
  it('readme', function (done) {
    var ge = GateExecutor()

    ge.add({
      fn: function first (done) {
        console.log('first')
        done()
      }
    })

    var subge = ge.gate()

    ge.add({
      fn: function second (done) {
        console.log('second')
        done()
      }
    })

    subge.add({
      fn: function second (done) {
        console.log('third')
        done()
      }
    })

    ge.start(function () {
      console.log('done')
      done()
    })

    // prints:
    //   first
    //   third
    //   second
    //   done
  })

  it('happy', function (done) {
    var log = []

    var ge = GateExecutor()

    ge.add({ fn: function aa (d) { log.push('aa'); d() } })
    ge.add({ fn: function bb (d) { log.push('bb'); d() } })


    var ge2 = ge.gate()
    ge2.add({ fn: function ccc (d) { log.push('ccc'); d() } })

    ge.add({ fn: function dd (d) { log.push('dd'); d() } })

    ge2.add({ fn: function ddd (d) { log.push('ddd'); d() } })

    expect(ge.state()).to.deep.equal([
      { s: 'w', ge: 1, d: 'aa', id: '1' },
      { s: 'w', ge: 1, d: 'bb', id: '2' },
      [ { s: 'w', ge: 2, d: 'ccc', id: '1' },
        { s: 'w', ge: 2, d: 'ddd', id: '2' } ],
      { s: 'w', ge: 1, d: 'dd', id: '4' } ])

    ge2.clear(function () {
      expect(log).to.deep.equal(['aa', 'bb', 'ccc', 'ddd'])
      expect(ge.state()).to.deep.equal([
        { s: 'w', ge: 1, d: 'dd', id: '4' } ])
    })

    ge.clear(function () {
      expect(log).to.deep.equal(['aa', 'bb', 'ccc', 'ddd', 'dd'])
      done()
    })

    ge.start()
  })


  it('running', function (done) {
    var log = []

    var ge = GateExecutor()

    ge.add({ fn: function aa (d) { log.push('aa'); d() } })
    ge.add({ fn: function bb (d) { log.push('bb'); d() } })

    var cc = 0
    ge.clear(function () {
      ++cc
      if (1 === cc) {
        expect(log).to.deep.equal(['aa', 'bb'])
      }
      else if (2 === cc) {
        expect(log).to.deep.equal(['aa', 'bb', 'cc'])
        done()
      }
    })

    ge.start(function () {
      expect(log).to.deep.equal(['aa', 'bb'])
    })

    setImmediate(function () {
      ge.add({ fn: function cc (d) { log.push('cc'); d() } })
    })
  })


  it('timeout', function (done) {
    var log = []

    var ge = GateExecutor({ timeout: 200, interval: 11 })

    ge.add({
      fn: function aa (d) {
        log.push('s-aa')
        setTimeout(function () { log.push('e-aa'); d() }, 100)
      }
    })

    ge.add({
      fn: function bb (d) {
        log.push('s-bb')
        setTimeout(function () { log.push('e-bb'); d() }, 300)
      }
    })

    ge.add({
      fn: function cc (d) {
        log.push('s-cc')
        setTimeout(function () { log.push('e-cc'); d() }, 150)
      }
    })

    ge.add({
      tm: 50,
      ontm: function () {
        log.push('t-dd')
      },
      fn: function dd (d) {
        log.push('s-dd')
        setTimeout(function () { log.push('e-dd'); d() }, 150)
      }
    })

    ge.clear(function () {
      expect(log).to.deep.equal(
        [ 's-aa', 's-bb', 's-cc', 's-dd', 't-dd', 'e-aa', 'e-cc', 'e-dd' ])
      done()
    })

    ge.start()

    setTimeout(function () {
      expect(ge.state()).to.deep.equal([ { s: 'a', ge: 1, d: 'bb', id: '2' } ])
    }, 200)
  })

  it('timeout after clear', function (done) {
    var log = []

    var ge = GateExecutor({ timeout: 200, interval: 11 })

    var firstTime = true

    ge.add({
      fn: function aa (d) {
        log.push('s-aa')
        log.push('e-aa')
        d()
      }
    })

    ge.clear(function () {
      if (firstTime) {
        expect(log).to.deep.equal([ 's-aa', 'e-aa' ])
        firstTime = false

        ge.add({
          tm: 50,
          ontm: function () {
            log.push('t-bb')
          },
          fn: function bb (d) {
            log.push('s-bb')
            setTimeout(function () { log.push('e-bb'); d() }, 150)
          }
        })

        return
      }

      expect(log).to.deep.equal([ 's-aa', 'e-aa', 's-bb', 't-bb', 'e-bb' ])
      done()
    })

    ge.start()

  })


  it('traverse', function (done) {

{'fraction_non_alphanumeric': 0.22762211753037442, 'fraction_numerical': 0.012893627572526656, 'mean_word_length': 1.825569176882662, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2982321', 'n_tokens_mistral': 3165, 'n_tokens_neox': 2983, 'n_words': 690}