stream: Emit readable on ended streams via read(0)
authorisaacs <i@izs.me>
Wed, 27 Mar 2013 05:43:53 +0000 (22:43 -0700)
committerisaacs <i@izs.me>
Thu, 28 Mar 2013 17:27:18 +0000 (10:27 -0700)
cc: @mjijackson

lib/_stream_readable.js
test/simple/test-stream-readable-event.js

index 2f4d8a4..e30a327 100644 (file)
@@ -236,7 +236,7 @@ Readable.prototype.read = function(n) {
   // the 'readable' event and move on.
   if (n === 0 &&
       state.needReadable &&
-      state.length >= state.highWaterMark) {
+      (state.length >= state.highWaterMark || state.ended)) {
     emitReadable(this);
     return null;
   }
index be17f04..fdbcb48 100644 (file)
@@ -80,3 +80,27 @@ var Readable = require('stream').Readable;
     console.log('ok 2');
   });
 })();
+
+(function third() {
+  // Third test, not reading when the stream has not passed
+  // the highWaterMark but *has* reached EOF.
+  var r = new Readable({
+    highWaterMark: 30
+  });
+
+  // This triggers a 'readable' event, which is lost.
+  r.push(new Buffer('blerg'));
+  r.push(null);
+
+  var caughtReadable = false;
+  setTimeout(function() {
+    r.on('readable', function() {
+      caughtReadable = true;
+    });
+  });
+
+  process.on('exit', function() {
+    assert(caughtReadable);
+    console.log('ok 3');
+  });
+})();