Geen omschrijving

tests.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. var assert = require("chai").assert;
  2. var stream = require("readable-stream");
  3. var duplexer2 = require("../");
  4. describe("duplexer2", function() {
  5. var writable, readable;
  6. beforeEach(function() {
  7. writable = new stream.Writable({objectMode: true});
  8. readable = new stream.Readable({objectMode: true});
  9. writable._write = function _write(input, encoding, done) {
  10. return done();
  11. };
  12. readable._read = function _read(n) {
  13. };
  14. });
  15. it("should interact with the writable stream properly for writing", function(done) {
  16. var duplex = duplexer2(writable, readable);
  17. writable._write = function _write(input, encoding, _done) {
  18. assert.strictEqual(input, "well hello there");
  19. return done();
  20. };
  21. duplex.write("well hello there");
  22. });
  23. it("should interact with the readable stream properly for reading", function(done) {
  24. var duplex = duplexer2(writable, readable);
  25. duplex.on("data", function(e) {
  26. assert.strictEqual(e, "well hello there");
  27. return done();
  28. });
  29. readable.push("well hello there");
  30. });
  31. it("should end the writable stream, causing it to finish", function(done) {
  32. var duplex = duplexer2(writable, readable);
  33. writable.once("finish", done);
  34. duplex.end();
  35. });
  36. it("should finish when the writable stream finishes", function(done) {
  37. var duplex = duplexer2(writable, readable);
  38. duplex.once("finish", done);
  39. writable.end();
  40. });
  41. it("should end when the readable stream ends", function(done) {
  42. var duplex = duplexer2(writable, readable);
  43. // required to let "end" fire without reading
  44. duplex.resume();
  45. duplex.once("end", done);
  46. readable.push(null);
  47. });
  48. it("should bubble errors from the writable stream when no behaviour is specified", function(done) {
  49. var duplex = duplexer2(writable, readable);
  50. var originalErr = Error("testing");
  51. duplex.on("error", function(err) {
  52. assert.strictEqual(err, originalErr);
  53. return done();
  54. });
  55. writable.emit("error", originalErr);
  56. });
  57. it("should bubble errors from the readable stream when no behaviour is specified", function(done) {
  58. var duplex = duplexer2(writable, readable);
  59. var originalErr = Error("testing");
  60. duplex.on("error", function(err) {
  61. assert.strictEqual(err, originalErr);
  62. return done();
  63. });
  64. readable.emit("error", originalErr);
  65. });
  66. it("should bubble errors from the writable stream when bubbleErrors is true", function(done) {
  67. var duplex = duplexer2({bubbleErrors: true}, writable, readable);
  68. var originalErr = Error("testing");
  69. duplex.on("error", function(err) {
  70. assert.strictEqual(err, originalErr);
  71. return done();
  72. });
  73. writable.emit("error", originalErr);
  74. });
  75. it("should bubble errors from the readable stream when bubbleErrors is true", function(done) {
  76. var duplex = duplexer2({bubbleErrors: true}, writable, readable);
  77. var originalErr = Error("testing");
  78. duplex.on("error", function(err) {
  79. assert.strictEqual(err, originalErr);
  80. return done();
  81. });
  82. readable.emit("error", originalErr);
  83. });
  84. it("should not bubble errors from the writable stream when bubbleErrors is false", function(done) {
  85. var duplex = duplexer2({bubbleErrors: false}, writable, readable);
  86. var timeout = setTimeout(done, 25);
  87. duplex.on("error", function(err) {
  88. clearTimeout(timeout);
  89. return done(Error("shouldn't bubble error"));
  90. });
  91. // prevent uncaught error exception
  92. writable.on("error", function() {});
  93. writable.emit("error", Error("testing"));
  94. });
  95. it("should not bubble errors from the readable stream when bubbleErrors is false", function(done) {
  96. var duplex = duplexer2({bubbleErrors: false}, writable, readable);
  97. var timeout = setTimeout(done, 25);
  98. duplex.on("error", function(err) {
  99. clearTimeout(timeout);
  100. return done(Error("shouldn't bubble error"));
  101. });
  102. // prevent uncaught error exception
  103. readable.on("error", function() {});
  104. readable.emit("error", Error("testing"));
  105. });
  106. });