main.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. var should = require('should');
  2. var through = require('through2');
  3. var OrderedStreams = require('../');
  4. describe('ordered-read-streams', function () {
  5. it('should end if no streams are given', function (done) {
  6. var streams = OrderedStreams();
  7. streams.on('data', function () {
  8. done('error');
  9. });
  10. streams.on('end', done);
  11. });
  12. it('should throw error if one or more streams are not readable', function (done) {
  13. var writable = { readable: false };
  14. try {
  15. new OrderedStreams(writable);
  16. } catch (e) {
  17. e.message.should.equal('All input streams must be readable');
  18. done();
  19. }
  20. });
  21. it('should emit data from all streams', function(done) {
  22. var s1 = through.obj(function (data, enc, next) {
  23. this.push(data);
  24. next();
  25. });
  26. var s2 = through.obj(function (data, enc, next) {
  27. this.push(data);
  28. next();
  29. });
  30. var s3 = through.obj(function (data, enc, next) {
  31. this.push(data);
  32. next();
  33. });
  34. var streams = new OrderedStreams([s1, s2, s3]);
  35. var results = [];
  36. streams.on('data', function (data) {
  37. results.push(data);
  38. });
  39. streams.on('end', function () {
  40. results.length.should.be.exactly(3);
  41. results[0].should.equal('stream 1');
  42. results[1].should.equal('stream 2');
  43. results[2].should.equal('stream 3');
  44. done();
  45. });
  46. s1.write('stream 1');
  47. s1.end();
  48. s2.write('stream 2');
  49. s2.end();
  50. s3.write('stream 3');
  51. s3.end();
  52. });
  53. it('should emit all data event from each stream', function (done) {
  54. var s = through.obj(function (data, enc, next) {
  55. this.push(data);
  56. next();
  57. });
  58. var streams = new OrderedStreams(s);
  59. var results = [];
  60. streams.on('data', function (data) {
  61. results.push(data);
  62. });
  63. streams.on('end', function () {
  64. results.length.should.be.exactly(3);
  65. done();
  66. });
  67. s.write('data1');
  68. s.write('data2');
  69. s.write('data3');
  70. s.end();
  71. });
  72. it('should preserve streams order', function(done) {
  73. var s1 = through.obj(function (data, enc, next) {
  74. var self = this;
  75. setTimeout(function () {
  76. self.push(data);
  77. next();
  78. }, 200);
  79. });
  80. var s2 = through.obj(function (data, enc, next) {
  81. var self = this;
  82. setTimeout(function () {
  83. self.push(data);
  84. next();
  85. }, 30);
  86. });
  87. var s3 = through.obj(function (data, enc, next) {
  88. var self = this;
  89. setTimeout(function () {
  90. self.push(data);
  91. next();
  92. }, 100);
  93. });
  94. var streams = new OrderedStreams([s1, s2, s3]);
  95. var results = [];
  96. streams.on('data', function (data) {
  97. results.push(data);
  98. });
  99. streams.on('end', function () {
  100. results.length.should.be.exactly(3);
  101. results[0].should.equal('stream 1');
  102. results[1].should.equal('stream 2');
  103. results[2].should.equal('stream 3');
  104. done();
  105. });
  106. s1.write('stream 1');
  107. s1.end();
  108. s2.write('stream 2');
  109. s2.end();
  110. s3.write('stream 3');
  111. s3.end();
  112. });
  113. it('should emit stream errors downstream', function (done) {
  114. var s = through.obj(function (data, enc, next) {
  115. this.emit('error', new Error('stahp!'));
  116. next();
  117. });
  118. var s2 = through.obj(function (data, enc, next) {
  119. this.push(data);
  120. next();
  121. });
  122. var errMsg;
  123. var streamData;
  124. var streams = new OrderedStreams([s, s2]);
  125. streams.on('data', function (data) {
  126. streamData = data;
  127. });
  128. streams.on('error', function (err) {
  129. errMsg = err.message;
  130. });
  131. streams.on('end', function () {
  132. errMsg.should.equal('stahp!');
  133. streamData.should.equal('Im ok!');
  134. done();
  135. });
  136. s.write('go');
  137. s.end();
  138. s2.write('Im ok!');
  139. s2.end();
  140. });
  141. });