<commit_before>const test = require('tape')
const qup = require('../')

test('runs, with N concurrent', (t) => {
  t.plan(12)

  let accum = 0

  function add (x, callback) {
    accum += x
    setTimeout(callback)
  }

  const su = qup(add, 2)

  su.push(2)
  t.equal(accum, 2) // 2 was added
  t.equal(su.running, 1) // add is now waiting on setTimeout
  t.equal(su.q.length, 0) // nothing is left in the queue

  su.push(3)
  t.equal(accum, 5) // 3 was added
  t.equal(su.running, 2) // two add's are now waiting on setTimeout
  t.equal(su.q.length, 0) // still, nothing is left in the queue

  su.push(60, () => {
    t.equal(accum, 65) // 60 was added
    t.equal(su.running, 0) // nothing is running during a callback
    t.equal(su.q.length, 0) // nothing is left in the queue
  })

  t.equal(accum, 5) // nothing was added
  t.equal(su.running, 2) // two add's are still waiting on setTimeout
  t.equal(su.q.length, 1) // 60 is now in the queue
})

test('README example as expected', (t) => {
  t.plan(8)

  const expected = [1, 2, 3, 4, 5, 6, 7, 8]
  const q = qup((x, callback) => {
    t.same(x, expected[0])
    expected.shift()

    // => in order, 1, 2, 3, 4, 5, 6, 7, then 8
    setTimeout(callback)
  }, 3)

  q.push(1)
  q.push(2)
  q.push(3)
  q.push(4)
  q.push(5)
  q.push(6)
  q.push(7)
  q.push(8)
})

test('push, with return value expected (non-batch only)', (t) => {
  const a = 9
  const su = qup((x, callback) => {
    callback(null, a + x)
  })

  t.plan(2)
  su.push(6, (err, y) => {
    t.ifErr(err)
    t.equal(y, 15)
  })
})

test('doesn\'t blow the stack', (t) => {
  const q = qup((x, callback) => {
    if (x === 0) return setTimeout(callback)
    if (x === 1e5 - 1) t.end()

    callback()
  }, 1)

  q.push(0)
  for (let i = 1; i < 1e5; ++i) q.push(i)
})

test('clear, depletes the queue', (t) => {
  t.plan(6)
  const expected = [1, 4, 5]

  const q = qup((x, callback) => {
    t.equal(x, expected[0])
    expected.shift()

    setTimeout(callback)
  }, 1)

  q.push(1)
  q.push(2)
  q.push(3)

  t.equal(q.q.length, 2)
  q.clear()
  t.equal(q.q.length, 0)

  q.push(4)
  q.push(5)
  t.equal(q.q.length, 2)
})

test('kill, depletes the queue and ignores running', (t) => {
  t.plan(5)

  let i = null
  const q = qup((x, callback) => {
    i = x
    setTimeout(callback)
  }, 1)
<commit_msg>test: s/deplete/empty, deplete implies usage<commit_after>const test = require('tape')
const qup = require('../')

test('runs, with N concurrent', (t) => {
  t.plan(12)

  let accum = 0

  function add (x, callback) {
    accum += x
    setTimeout(callback)
  }

  const su = qup(add, 2)

  su.push(2)
  t.equal(accum, 2) // 2 was added
  t.equal(su.running, 1) // add is now waiting on setTimeout
  t.equal(su.q.length, 0) // nothing is left in the queue

  su.push(3)
  t.equal(accum, 5) // 3 was added
  t.equal(su.running, 2) // two add's are now waiting on setTimeout
  t.equal(su.q.length, 0) // still, nothing is left in the queue

  su.push(60, () => {
    t.equal(accum, 65) // 60 was added
    t.equal(su.running, 0) // nothing is running during a callback
    t.equal(su.q.length, 0) // nothing is left in the queue
  })

  t.equal(accum, 5) // nothing was added
  t.equal(su.running, 2) // two add's are still waiting on setTimeout
  t.equal(su.q.length, 1) // 60 is now in the queue
})

test('README example as expected', (t) => {
  t.plan(8)

  const expected = [1, 2, 3, 4, 5, 6, 7, 8]
  const q = qup((x, callback) => {
    t.same(x, expected[0])
    expected.shift()

    // => in order, 1, 2, 3, 4, 5, 6, 7, then 8
    setTimeout(callback)
  }, 3)

  q.push(1)
  q.push(2)
  q.push(3)
  q.push(4)
  q.push(5)
  q.push(6)
  q.push(7)
  q.push(8)
})

test('push, with return value expected (non-batch only)', (t) => {
  const a = 9
  const su = qup((x, callback) => {
    callback(null, a + x)
  })

  t.plan(2)
  su.push(6, (err, y) => {
    t.ifErr(err)
    t.equal(y, 15)
  })
})

test('doesn\'t blow the stack', (t) => {
  const q = qup((x, callback) => {
    if (x === 0) return setTimeout(callback)
    if (x === 1e5 - 1) t.end()

    callback()
  }, 1)

  q.push(0)
  for (let i = 1; i < 1e5; ++i) q.push(i)
})

test('clear, empties the queue', (t) => {
  t.plan(6)
  const expected = [1, 4, 5]

  const q = qup((x, callback) => {
    t.equal(x, expected[0])
    expected.shift()

    setTimeout(callback)
  }, 1)

  q.push(1)
  q.push(2)
  q.push(3)

  t.equal(q.q.length, 2)
  q.clear()
  t.equal(q.q.length, 0)

  q.push(4)
  q.push(5)
  t.equal(q.q.length, 2)
})

test('kill, empties the queue and ignores running', (t) => {
  t.plan(5)

  let i = null
  const q = qup((x, callback) => {
    i = x
    setTimeout(callback)
  }, 1)

{'fraction_non_alphanumeric': 0.1898869212716023, 'fraction_numerical': 0.03584382334115639, 'mean_word_length': 2.5381132075471697, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '452139', 'n_tokens_mistral': 2097, 'n_tokens_neox': 1946, 'n_words': 538}