promise.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. 'use strict'
  2. const test = require('tape')
  3. const buildQueue = require('../').promise
  4. const { promisify } = require('util')
  5. const sleep = promisify(setTimeout)
  6. test('concurrency', function (t) {
  7. t.plan(2)
  8. t.throws(buildQueue.bind(null, worker, 0))
  9. t.doesNotThrow(buildQueue.bind(null, worker, 1))
  10. async function worker (arg) {
  11. return true
  12. }
  13. })
  14. test('worker execution', async function (t) {
  15. var queue = buildQueue(worker, 1)
  16. const result = await queue.push(42)
  17. t.equal(result, true, 'result matches')
  18. async function worker (arg) {
  19. t.equal(arg, 42)
  20. return true
  21. }
  22. })
  23. test('limit', async function (t) {
  24. var queue = buildQueue(worker, 1)
  25. const [res1, res2] = await Promise.all([queue.push(10), queue.push(0)])
  26. t.equal(res1, 10, 'the result matches')
  27. t.equal(res2, 0, 'the result matches')
  28. async function worker (arg) {
  29. await sleep(arg)
  30. return arg
  31. }
  32. })
  33. test('multiple executions', async function (t) {
  34. var queue = buildQueue(worker, 1)
  35. var toExec = [1, 2, 3, 4, 5]
  36. var expected = ['a', 'b', 'c', 'd', 'e']
  37. var count = 0
  38. await Promise.all(toExec.map(async function (task, i) {
  39. const result = await queue.push(task)
  40. t.equal(result, expected[i], 'the result matches')
  41. }))
  42. async function worker (arg) {
  43. t.equal(arg, toExec[count], 'arg matches')
  44. return expected[count++]
  45. }
  46. })
  47. test('set this', async function (t) {
  48. t.plan(1)
  49. var that = {}
  50. var queue = buildQueue(that, worker, 1)
  51. await queue.push(42)
  52. async function worker (arg) {
  53. t.equal(this, that, 'this matches')
  54. }
  55. })
  56. test('unshift', async function (t) {
  57. var queue = buildQueue(worker, 1)
  58. var expected = [1, 2, 3, 4]
  59. await Promise.all([
  60. queue.push(1),
  61. queue.push(4),
  62. queue.unshift(3),
  63. queue.unshift(2)
  64. ])
  65. t.is(expected.length, 0)
  66. async function worker (arg) {
  67. t.equal(expected.shift(), arg, 'tasks come in order')
  68. }
  69. })
  70. test('push with worker throwing error', async function (t) {
  71. t.plan(5)
  72. var q = buildQueue(async function (task, cb) {
  73. throw new Error('test error')
  74. }, 1)
  75. q.error(function (err, task) {
  76. t.ok(err instanceof Error, 'global error handler should catch the error')
  77. t.match(err.message, /test error/, 'error message should be "test error"')
  78. t.equal(task, 42, 'The task executed should be passed')
  79. })
  80. try {
  81. await q.push(42)
  82. } catch (err) {
  83. t.ok(err instanceof Error, 'push callback should catch the error')
  84. t.match(err.message, /test error/, 'error message should be "test error"')
  85. }
  86. })
  87. test('unshift with worker throwing error', async function (t) {
  88. t.plan(2)
  89. var q = buildQueue(async function (task, cb) {
  90. throw new Error('test error')
  91. }, 1)
  92. try {
  93. await q.unshift(42)
  94. } catch (err) {
  95. t.ok(err instanceof Error, 'push callback should catch the error')
  96. t.match(err.message, /test error/, 'error message should be "test error"')
  97. }
  98. })