test.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. const tape = require('tape')
  2. const tee = require('./')
  3. const { Readable } = require('streamx')
  4. tape('throttled by eachother', function (t) {
  5. const r = new Readable()
  6. for (let i = 0; i < 1000; i++) {
  7. r.push(Buffer.alloc(1000))
  8. }
  9. const [a, b] = tee(r)
  10. let aTicks = 0
  11. a.on('data', function (data) {
  12. aTicks++
  13. })
  14. setTimeout(() => b.read(), 100)
  15. setTimeout(() => {
  16. t.same(aTicks, 18)
  17. t.end()
  18. }, 200)
  19. })
  20. tape('does not premature destroy', function (t) {
  21. const r = new Readable()
  22. const [a, b] = tee(r)
  23. r.push('a')
  24. r.push('b')
  25. r.push('c')
  26. r.push(null)
  27. setTimeout(() => {
  28. const aSeen = []
  29. const bSeen = []
  30. a.on('data', function (data) {
  31. aSeen.push(data)
  32. })
  33. a.on('end', function () {
  34. aSeen.push(null)
  35. })
  36. b.on('data', function (data) {
  37. bSeen.push(data)
  38. })
  39. b.on('end', function () {
  40. bSeen.push(null)
  41. })
  42. let missing = 2
  43. a.on('close', onclose)
  44. b.on('close', onclose)
  45. function onclose () {
  46. if (--missing === 0) {
  47. t.same(aSeen, ['a', 'b', 'c', null])
  48. t.same(bSeen, ['a', 'b', 'c', null])
  49. t.end()
  50. }
  51. }
  52. }, 200)
  53. })