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

buffering.js 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. var test = require('tape')
  2. var through = require('../')
  3. // must emit end before close.
  4. test('buffering', function(assert) {
  5. var ts = through(function (data) {
  6. this.queue(data)
  7. }, function () {
  8. this.queue(null)
  9. })
  10. var ended = false, actual = []
  11. ts.on('data', actual.push.bind(actual))
  12. ts.on('end', function () {
  13. ended = true
  14. })
  15. ts.write(1)
  16. ts.write(2)
  17. ts.write(3)
  18. assert.deepEqual(actual, [1, 2, 3])
  19. ts.pause()
  20. ts.write(4)
  21. ts.write(5)
  22. ts.write(6)
  23. assert.deepEqual(actual, [1, 2, 3])
  24. ts.resume()
  25. assert.deepEqual(actual, [1, 2, 3, 4, 5, 6])
  26. ts.pause()
  27. ts.end()
  28. assert.ok(!ended)
  29. ts.resume()
  30. assert.ok(ended)
  31. assert.end()
  32. })
  33. test('buffering has data in queue, when ends', function (assert) {
  34. /*
  35. * If stream ends while paused with data in the queue,
  36. * stream should still emit end after all data is written
  37. * on resume.
  38. */
  39. var ts = through(function (data) {
  40. this.queue(data)
  41. }, function () {
  42. this.queue(null)
  43. })
  44. var ended = false, actual = []
  45. ts.on('data', actual.push.bind(actual))
  46. ts.on('end', function () {
  47. ended = true
  48. })
  49. ts.pause()
  50. ts.write(1)
  51. ts.write(2)
  52. ts.write(3)
  53. ts.end()
  54. assert.deepEqual(actual, [], 'no data written yet, still paused')
  55. assert.ok(!ended, 'end not emitted yet, still paused')
  56. ts.resume()
  57. assert.deepEqual(actual, [1, 2, 3], 'resumed, all data should be delivered')
  58. assert.ok(ended, 'end should be emitted once all data was delivered')
  59. assert.end();
  60. })