tests.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*jshint node:true */
  2. /*global describe:false, it:false */
  3. "use strict";
  4. var consume = require('../');
  5. var Stream = require('stream');
  6. var Readable = Stream.Readable;
  7. var Writable = Stream.Writable;
  8. var Duplex = Stream.Duplex;
  9. var should = require('should');
  10. var through = require('through2');
  11. require('mocha');
  12. describe('stream-consume', function() {
  13. it('should cause a Readable stream to complete if it\'s not piped anywhere', function(done) {
  14. var rs = new Readable({highWaterMark: 2});
  15. var a = 0;
  16. var ended = false;
  17. rs._read = function() {
  18. if (a++ < 100) {
  19. rs.push(a + "");
  20. } else {
  21. ended = true;
  22. rs.push(null);
  23. }
  24. };
  25. rs.on("end", function() {
  26. a.should.be.above(99);
  27. ended.should.be.true;
  28. done();
  29. });
  30. consume(rs);
  31. });
  32. it('should work with Readable streams in objectMode', function(done) {
  33. var rs = new Readable({highWaterMark: 2, objectMode: true});
  34. var a = 0;
  35. var ended = false;
  36. rs._read = function() {
  37. if (a++ < 100) {
  38. rs.push(a);
  39. } else {
  40. ended = true;
  41. rs.push(null);
  42. }
  43. };
  44. rs.on("end", function() {
  45. a.should.be.above(99);
  46. ended.should.be.true;
  47. done();
  48. });
  49. consume(rs);
  50. });
  51. it('should not interfere with a Readable stream that is piped somewhere', function(done) {
  52. var rs = new Readable({highWaterMark: 2});
  53. var a = 0;
  54. var ended = false;
  55. rs._read = function() {
  56. if (a++ < 100) {
  57. rs.push(".");
  58. } else {
  59. ended = true;
  60. rs.push(null);
  61. }
  62. };
  63. var sizeRead = 0;
  64. var ws = new Writable({highWaterMark: 2});
  65. ws._write = function(chunk, enc, next) {
  66. sizeRead += chunk.length;
  67. next();
  68. }
  69. ws.on("finish", function() {
  70. a.should.be.above(99);
  71. ended.should.be.true;
  72. sizeRead.should.equal(100);
  73. done();
  74. });
  75. rs.pipe(ws);
  76. consume(rs);
  77. });
  78. it('should not interfere with a Writable stream', function(done) {
  79. var rs = new Readable({highWaterMark: 2});
  80. var a = 0;
  81. var ended = false;
  82. rs._read = function() {
  83. if (a++ < 100) {
  84. rs.push(".");
  85. } else {
  86. ended = true;
  87. rs.push(null);
  88. }
  89. };
  90. var sizeRead = 0;
  91. var ws = new Writable({highWaterMark: 2});
  92. ws._write = function(chunk, enc, next) {
  93. sizeRead += chunk.length;
  94. next();
  95. }
  96. ws.on("finish", function() {
  97. a.should.be.above(99);
  98. ended.should.be.true;
  99. sizeRead.should.equal(100);
  100. done();
  101. });
  102. rs.pipe(ws);
  103. consume(ws);
  104. });
  105. it('should handle a Transform stream', function(done) {
  106. var rs = new Readable({highWaterMark: 2});
  107. var a = 0;
  108. var ended = false;
  109. rs._read = function() {
  110. if (a++ < 100) {
  111. rs.push(".");
  112. } else {
  113. ended = true;
  114. rs.push(null);
  115. }
  116. };
  117. var sizeRead = 0;
  118. var flushed = false;
  119. var ts = through({highWaterMark: 2}, function(chunk, enc, cb) {
  120. sizeRead += chunk.length;
  121. this.push(chunk);
  122. cb();
  123. }, function(cb) {
  124. flushed = true;
  125. cb();
  126. });
  127. ts.on("end", function() {
  128. a.should.be.above(99);
  129. ended.should.be.true;
  130. sizeRead.should.equal(100);
  131. flushed.should.be.true;
  132. done();
  133. });
  134. rs.pipe(ts);
  135. consume(ts);
  136. });
  137. it('should handle a classic stream', function(done) {
  138. var rs = new Stream();
  139. var ended = false;
  140. var i;
  141. rs.on("end", function() {
  142. ended.should.be.true;
  143. done();
  144. });
  145. consume(rs);
  146. for (i = 0; i < 100; i++) {
  147. rs.emit("data", i);
  148. }
  149. ended = true;
  150. rs.emit("end");
  151. });
  152. });