Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

test.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. 'use strict'
  2. var test = require('tape')
  3. var buildQueue = require('../')
  4. test('worker execution', function (t) {
  5. t.plan(3)
  6. var queue = buildQueue(worker, 1)
  7. queue.push(42, function (err, result) {
  8. t.error(err, 'no error')
  9. t.equal(result, true, 'result matches')
  10. })
  11. function worker (arg, cb) {
  12. t.equal(arg, 42)
  13. cb(null, true)
  14. }
  15. })
  16. test('limit', function (t) {
  17. t.plan(4)
  18. var expected = [10, 0]
  19. var queue = buildQueue(worker, 1)
  20. queue.push(10, result)
  21. queue.push(0, result)
  22. function result (err, arg) {
  23. t.error(err, 'no error')
  24. t.equal(arg, expected.shift(), 'the result matches')
  25. }
  26. function worker (arg, cb) {
  27. setTimeout(cb, arg, null, arg)
  28. }
  29. })
  30. test('multiple executions', function (t) {
  31. t.plan(15)
  32. var queue = buildQueue(worker, 1)
  33. var toExec = [1, 2, 3, 4, 5]
  34. var count = 0
  35. toExec.forEach(function (task) {
  36. queue.push(task, done)
  37. })
  38. function done (err, result) {
  39. t.error(err, 'no error')
  40. t.equal(result, toExec[count - 1], 'the result matches')
  41. }
  42. function worker (arg, cb) {
  43. t.equal(arg, toExec[count], 'arg matches')
  44. count++
  45. setImmediate(cb, null, arg)
  46. }
  47. })
  48. test('multiple executions, one after another', function (t) {
  49. t.plan(15)
  50. var queue = buildQueue(worker, 1)
  51. var toExec = [1, 2, 3, 4, 5]
  52. var count = 0
  53. queue.push(toExec[0], done)
  54. function done (err, result) {
  55. t.error(err, 'no error')
  56. t.equal(result, toExec[count - 1], 'the result matches')
  57. if (count < toExec.length) {
  58. queue.push(toExec[count], done)
  59. }
  60. }
  61. function worker (arg, cb) {
  62. t.equal(arg, toExec[count], 'arg matches')
  63. count++
  64. setImmediate(cb, null, arg)
  65. }
  66. })
  67. test('set this', function (t) {
  68. t.plan(3)
  69. var that = {}
  70. var queue = buildQueue(that, worker, 1)
  71. queue.push(42, function (err, result) {
  72. t.error(err, 'no error')
  73. t.equal(this, that, 'this matches')
  74. })
  75. function worker (arg, cb) {
  76. t.equal(this, that, 'this matches')
  77. cb(null, true)
  78. }
  79. })
  80. test('drain', function (t) {
  81. t.plan(4)
  82. var queue = buildQueue(worker, 1)
  83. var worked = false
  84. queue.push(42, function (err, result) {
  85. t.error(err, 'no error')
  86. t.equal(result, true, 'result matches')
  87. })
  88. queue.drain = function () {
  89. t.equal(true, worked, 'drained')
  90. }
  91. function worker (arg, cb) {
  92. t.equal(arg, 42)
  93. worked = true
  94. setImmediate(cb, null, true)
  95. }
  96. })
  97. test('pause && resume', function (t) {
  98. t.plan(7)
  99. var queue = buildQueue(worker, 1)
  100. var worked = false
  101. t.notOk(queue.paused, 'it should not be paused')
  102. queue.pause()
  103. queue.push(42, function (err, result) {
  104. t.error(err, 'no error')
  105. t.equal(result, true, 'result matches')
  106. })
  107. t.notOk(worked, 'it should be paused')
  108. t.ok(queue.paused, 'it should be paused')
  109. queue.resume()
  110. queue.resume() // second resume is a no-op
  111. t.notOk(queue.paused, 'it should not be paused')
  112. function worker (arg, cb) {
  113. t.equal(arg, 42)
  114. worked = true
  115. cb(null, true)
  116. }
  117. })
  118. test('pause in flight && resume', function (t) {
  119. t.plan(9)
  120. var queue = buildQueue(worker, 1)
  121. var expected = [42, 24]
  122. t.notOk(queue.paused, 'it should not be paused')
  123. queue.push(42, function (err, result) {
  124. t.error(err, 'no error')
  125. t.equal(result, true, 'result matches')
  126. t.ok(queue.paused, 'it should be paused')
  127. process.nextTick(function () { queue.resume() })
  128. })
  129. queue.push(24, function (err, result) {
  130. t.error(err, 'no error')
  131. t.equal(result, true, 'result matches')
  132. t.notOk(queue.paused, 'it should not be paused')
  133. })
  134. queue.pause()
  135. function worker (arg, cb) {
  136. t.equal(arg, expected.shift())
  137. process.nextTick(function () { cb(null, true) })
  138. }
  139. })
  140. test('altering concurrency', function (t) {
  141. t.plan(7)
  142. var queue = buildQueue(worker, 1)
  143. var count = 0
  144. queue.pause()
  145. queue.push(24, workDone)
  146. queue.push(24, workDone)
  147. queue.concurrency = 2
  148. queue.resume()
  149. t.equal(queue.running(), 2, '2 jobs running')
  150. function workDone (err, result) {
  151. t.error(err, 'no error')
  152. t.equal(result, true, 'result matches')
  153. }
  154. function worker (arg, cb) {
  155. t.equal(0, count, 'works in parallel')
  156. setImmediate(function () {
  157. count++
  158. cb(null, true)
  159. })
  160. }
  161. })
  162. test('idle()', function (t) {
  163. t.plan(12)
  164. var queue = buildQueue(worker, 1)
  165. t.ok(queue.idle(), 'queue is idle')
  166. queue.push(42, function (err, result) {
  167. t.error(err, 'no error')
  168. t.equal(result, true, 'result matches')
  169. t.notOk(queue.idle(), 'queue is not idle')
  170. })
  171. queue.push(42, function (err, result) {
  172. t.error(err, 'no error')
  173. t.equal(result, true, 'result matches')
  174. // it will go idle after executing this function
  175. setImmediate(function () {
  176. t.ok(queue.idle(), 'queue is now idle')
  177. })
  178. })
  179. t.notOk(queue.idle(), 'queue is not idle')
  180. function worker (arg, cb) {
  181. t.notOk(queue.idle(), 'queue is not idle')
  182. t.equal(arg, 42)
  183. setImmediate(cb, null, true)
  184. }
  185. })
  186. test('saturated', function (t) {
  187. t.plan(9)
  188. var queue = buildQueue(worker, 1)
  189. var preworked = 0
  190. var worked = 0
  191. queue.saturated = function () {
  192. t.pass('saturated')
  193. t.equal(preworked, 1, 'started 1 task')
  194. t.equal(worked, 0, 'worked zero task')
  195. }
  196. queue.push(42, done)
  197. queue.push(42, done)
  198. function done (err, result) {
  199. t.error(err, 'no error')
  200. t.equal(result, true, 'result matches')
  201. }
  202. function worker (arg, cb) {
  203. t.equal(arg, 42)
  204. preworked++
  205. setImmediate(function () {
  206. worked++
  207. cb(null, true)
  208. })
  209. }
  210. })
  211. test('length', function (t) {
  212. t.plan(7)
  213. var queue = buildQueue(worker, 1)
  214. t.equal(queue.length(), 0, 'nothing waiting')
  215. queue.push(42, done)
  216. t.equal(queue.length(), 0, 'nothing waiting')
  217. queue.push(42, done)
  218. t.equal(queue.length(), 1, 'one task waiting')
  219. queue.push(42, done)
  220. t.equal(queue.length(), 2, 'two tasks waiting')
  221. function done (err, result) {
  222. t.error(err, 'no error')
  223. }
  224. function worker (arg, cb) {
  225. setImmediate(function () {
  226. cb(null, true)
  227. })
  228. }
  229. })
  230. test('getQueue', function (t) {
  231. t.plan(10)
  232. var queue = buildQueue(worker, 1)
  233. t.equal(queue.getQueue().length, 0, 'nothing waiting')
  234. queue.push(42, done)
  235. t.equal(queue.getQueue().length, 0, 'nothing waiting')
  236. queue.push(42, done)
  237. t.equal(queue.getQueue().length, 1, 'one task waiting')
  238. t.equal(queue.getQueue()[0], 42, 'should be equal')
  239. queue.push(43, done)
  240. t.equal(queue.getQueue().length, 2, 'two tasks waiting')
  241. t.equal(queue.getQueue()[0], 42, 'should be equal')
  242. t.equal(queue.getQueue()[1], 43, 'should be equal')
  243. function done (err, result) {
  244. t.error(err, 'no error')
  245. }
  246. function worker (arg, cb) {
  247. setImmediate(function () {
  248. cb(null, true)
  249. })
  250. }
  251. })
  252. test('unshift', function (t) {
  253. t.plan(8)
  254. var queue = buildQueue(worker, 1)
  255. var expected = [1, 2, 3, 4]
  256. queue.push(1, done)
  257. queue.push(4, done)
  258. queue.unshift(3, done)
  259. queue.unshift(2, done)
  260. function done (err, result) {
  261. t.error(err, 'no error')
  262. }
  263. function worker (arg, cb) {
  264. t.equal(expected.shift(), arg, 'tasks come in order')
  265. setImmediate(function () {
  266. cb(null, true)
  267. })
  268. }
  269. })
  270. test('unshift && empty', function (t) {
  271. t.plan(2)
  272. var queue = buildQueue(worker, 1)
  273. var completed = false
  274. queue.pause()
  275. queue.empty = function () {
  276. t.notOk(completed, 'the task has not completed yet')
  277. }
  278. queue.unshift(1, done)
  279. queue.resume()
  280. function done (err, result) {
  281. completed = true
  282. t.error(err, 'no error')
  283. }
  284. function worker (arg, cb) {
  285. setImmediate(function () {
  286. cb(null, true)
  287. })
  288. }
  289. })
  290. test('push && empty', function (t) {
  291. t.plan(2)
  292. var queue = buildQueue(worker, 1)
  293. var completed = false
  294. queue.pause()
  295. queue.empty = function () {
  296. t.notOk(completed, 'the task has not completed yet')
  297. }
  298. queue.push(1, done)
  299. queue.resume()
  300. function done (err, result) {
  301. completed = true
  302. t.error(err, 'no error')
  303. }
  304. function worker (arg, cb) {
  305. setImmediate(function () {
  306. cb(null, true)
  307. })
  308. }
  309. })
  310. test('kill', function (t) {
  311. t.plan(5)
  312. var queue = buildQueue(worker, 1)
  313. var expected = [1]
  314. var predrain = queue.drain
  315. queue.drain = function drain () {
  316. t.fail('drain should never be called')
  317. }
  318. queue.push(1, done)
  319. queue.push(4, done)
  320. queue.unshift(3, done)
  321. queue.unshift(2, done)
  322. queue.kill()
  323. function done (err, result) {
  324. t.error(err, 'no error')
  325. setImmediate(function () {
  326. t.equal(queue.length(), 0, 'no queued tasks')
  327. t.equal(queue.running(), 0, 'no running tasks')
  328. t.equal(queue.drain, predrain, 'drain is back to default')
  329. })
  330. }
  331. function worker (arg, cb) {
  332. t.equal(expected.shift(), arg, 'tasks come in order')
  333. setImmediate(function () {
  334. cb(null, true)
  335. })
  336. }
  337. })
  338. test('killAndDrain', function (t) {
  339. t.plan(6)
  340. var queue = buildQueue(worker, 1)
  341. var expected = [1]
  342. var predrain = queue.drain
  343. queue.drain = function drain () {
  344. t.pass('drain has been called')
  345. }
  346. queue.push(1, done)
  347. queue.push(4, done)
  348. queue.unshift(3, done)
  349. queue.unshift(2, done)
  350. queue.killAndDrain()
  351. function done (err, result) {
  352. t.error(err, 'no error')
  353. setImmediate(function () {
  354. t.equal(queue.length(), 0, 'no queued tasks')
  355. t.equal(queue.running(), 0, 'no running tasks')
  356. t.equal(queue.drain, predrain, 'drain is back to default')
  357. })
  358. }
  359. function worker (arg, cb) {
  360. t.equal(expected.shift(), arg, 'tasks come in order')
  361. setImmediate(function () {
  362. cb(null, true)
  363. })
  364. }
  365. })
  366. test('pause && idle', function (t) {
  367. t.plan(11)
  368. var queue = buildQueue(worker, 1)
  369. var worked = false
  370. t.notOk(queue.paused, 'it should not be paused')
  371. t.ok(queue.idle(), 'should be idle')
  372. queue.pause()
  373. queue.push(42, function (err, result) {
  374. t.error(err, 'no error')
  375. t.equal(result, true, 'result matches')
  376. })
  377. t.notOk(worked, 'it should be paused')
  378. t.ok(queue.paused, 'it should be paused')
  379. t.notOk(queue.idle(), 'should not be idle')
  380. queue.resume()
  381. t.notOk(queue.paused, 'it should not be paused')
  382. t.notOk(queue.idle(), 'it should not be idle')
  383. function worker (arg, cb) {
  384. t.equal(arg, 42)
  385. worked = true
  386. process.nextTick(cb.bind(null, null, true))
  387. process.nextTick(function () {
  388. t.ok(queue.idle(), 'is should be idle')
  389. })
  390. }
  391. })
  392. test('push without cb', function (t) {
  393. t.plan(1)
  394. var queue = buildQueue(worker, 1)
  395. queue.push(42)
  396. function worker (arg, cb) {
  397. t.equal(arg, 42)
  398. cb()
  399. }
  400. })
  401. test('unshift without cb', function (t) {
  402. t.plan(1)
  403. var queue = buildQueue(worker, 1)
  404. queue.unshift(42)
  405. function worker (arg, cb) {
  406. t.equal(arg, 42)
  407. cb()
  408. }
  409. })