index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. var from = require('..')
  2. var spec = require('stream-spec')
  3. var a = require('assertions')
  4. function read(stream, callback) {
  5. var actual = []
  6. stream.on('data', function (data) {
  7. actual.push(data)
  8. })
  9. stream.once('end', function () {
  10. callback(null, actual)
  11. })
  12. stream.once('error', function (err) {
  13. callback(err)
  14. })
  15. }
  16. function pause(stream) {
  17. stream.on('data', function () {
  18. if(Math.random() > 0.1) return
  19. stream.pause()
  20. process.nextTick(function () {
  21. stream.resume()
  22. })
  23. })
  24. }
  25. exports['inc'] = function (test) {
  26. var fs = from(function (i) {
  27. this.emit('data', i)
  28. if(i >= 99)
  29. return this.emit('end')
  30. return true
  31. })
  32. spec(fs).readable().validateOnExit()
  33. read(fs, function (err, arr) {
  34. test.equal(arr.length, 100)
  35. test.done()
  36. })
  37. }
  38. exports['simple'] = function (test) {
  39. var l = 1000
  40. , expected = []
  41. while(l--) expected.push(l * Math.random())
  42. var t = from(expected.slice())
  43. spec(t)
  44. .readable()
  45. .pausable({strict: true})
  46. .validateOnExit()
  47. read(t, function (err, actual) {
  48. if(err) test.error(err) //fail
  49. a.deepEqual(actual, expected)
  50. test.done()
  51. })
  52. }
  53. exports['simple pausable'] = function (test) {
  54. var l = 1000
  55. , expected = []
  56. while(l--) expected.push(l * Math.random())
  57. var t = from(expected.slice())
  58. spec(t)
  59. .readable()
  60. .pausable({strict: true})
  61. .validateOnExit()
  62. pause(t)
  63. read(t, function (err, actual) {
  64. if(err) test.error(err) //fail
  65. a.deepEqual(actual, expected)
  66. test.done()
  67. })
  68. }
  69. exports['simple (not strictly pausable) setTimeout'] = function (test) {
  70. var l = 10
  71. , expected = []
  72. while(l--) expected.push(l * Math.random())
  73. var _expected = expected.slice()
  74. var t = from(function (i, n) {
  75. var self = this
  76. setTimeout(function () {
  77. if(_expected.length)
  78. self.emit('data', _expected.shift())
  79. else
  80. self.emit('end')
  81. n()
  82. }, 3)
  83. })
  84. /*
  85. using from in this way will not be strictly pausable.
  86. it could be extended to buffer outputs, but I think a better
  87. way would be to use a PauseStream that implements strict pause.
  88. */
  89. spec(t)
  90. .readable()
  91. .pausable({strict: false })
  92. .validateOnExit()
  93. //pause(t)
  94. var paused = false
  95. var i = setInterval(function () {
  96. if(!paused) t.pause()
  97. else t.resume()
  98. paused = !paused
  99. }, 2)
  100. t.on('end', function () {
  101. clearInterval(i)
  102. })
  103. read(t, function (err, actual) {
  104. if(err) test.error(err) //fail
  105. a.deepEqual(actual, expected)
  106. test.done()
  107. })
  108. }