2013-03-26 22:42:56 +01:00
|
|
|
var common = require('../common');
|
|
|
|
var assert = require('assert');
|
|
|
|
|
|
|
|
var Readable = require('stream').Readable;
|
|
|
|
|
|
|
|
(function first() {
|
|
|
|
// First test, not reading when the readable is added.
|
2013-03-31 08:16:52 +02:00
|
|
|
// make sure that on('readable', ...) triggers a readable event.
|
2013-03-26 22:42:56 +01:00
|
|
|
var r = new Readable({
|
|
|
|
highWaterMark: 3
|
|
|
|
});
|
|
|
|
|
2013-03-31 08:16:52 +02:00
|
|
|
var _readCalled = false;
|
2013-03-26 22:42:56 +01:00
|
|
|
r._read = function(n) {
|
2013-03-31 08:16:52 +02:00
|
|
|
_readCalled = true;
|
2013-03-26 22:42:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// This triggers a 'readable' event, which is lost.
|
|
|
|
r.push(new Buffer('blerg'));
|
|
|
|
|
|
|
|
var caughtReadable = false;
|
|
|
|
setTimeout(function() {
|
2013-03-31 08:16:52 +02:00
|
|
|
// we're testing what we think we are
|
|
|
|
assert(!r._readableState.reading);
|
2013-03-26 22:42:56 +01:00
|
|
|
r.on('readable', function() {
|
|
|
|
caughtReadable = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('exit', function() {
|
2013-03-31 08:16:52 +02:00
|
|
|
// we're testing what we think we are
|
|
|
|
assert(!_readCalled);
|
|
|
|
|
2013-03-26 22:42:56 +01:00
|
|
|
assert(caughtReadable);
|
|
|
|
console.log('ok 1');
|
|
|
|
});
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function second() {
|
|
|
|
// second test, make sure that readable is re-emitted if there's
|
|
|
|
// already a length, while it IS reading.
|
|
|
|
|
|
|
|
var r = new Readable({
|
|
|
|
highWaterMark: 3
|
|
|
|
});
|
|
|
|
|
2013-03-31 08:16:52 +02:00
|
|
|
var _readCalled = false;
|
2013-03-26 22:42:56 +01:00
|
|
|
r._read = function(n) {
|
2013-03-31 08:16:52 +02:00
|
|
|
_readCalled = true;
|
2013-03-26 22:42:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// This triggers a 'readable' event, which is lost.
|
2013-03-31 08:16:52 +02:00
|
|
|
r.push(new Buffer('bl'));
|
2013-03-26 22:42:56 +01:00
|
|
|
|
|
|
|
var caughtReadable = false;
|
2013-03-31 08:16:52 +02:00
|
|
|
setTimeout(function() {
|
|
|
|
// assert we're testing what we think we are
|
|
|
|
assert(r._readableState.reading);
|
2013-03-26 22:42:56 +01:00
|
|
|
r.on('readable', function() {
|
|
|
|
caughtReadable = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('exit', function() {
|
2013-03-31 08:16:52 +02:00
|
|
|
// we're testing what we think we are
|
|
|
|
assert(_readCalled);
|
|
|
|
|
2013-03-26 22:42:56 +01:00
|
|
|
assert(caughtReadable);
|
|
|
|
console.log('ok 2');
|
|
|
|
});
|
|
|
|
})();
|
2013-03-27 06:43:53 +01:00
|
|
|
|
|
|
|
(function third() {
|
|
|
|
// Third test, not reading when the stream has not passed
|
|
|
|
// the highWaterMark but *has* reached EOF.
|
|
|
|
var r = new Readable({
|
|
|
|
highWaterMark: 30
|
|
|
|
});
|
|
|
|
|
2013-03-31 08:16:52 +02:00
|
|
|
var _readCalled = false;
|
|
|
|
r._read = function(n) {
|
|
|
|
_readCalled = true;
|
|
|
|
};
|
|
|
|
|
2013-03-27 06:43:53 +01:00
|
|
|
// This triggers a 'readable' event, which is lost.
|
|
|
|
r.push(new Buffer('blerg'));
|
|
|
|
r.push(null);
|
|
|
|
|
|
|
|
var caughtReadable = false;
|
|
|
|
setTimeout(function() {
|
2013-03-31 08:16:52 +02:00
|
|
|
// assert we're testing what we think we are
|
|
|
|
assert(!r._readableState.reading);
|
2013-03-27 06:43:53 +01:00
|
|
|
r.on('readable', function() {
|
|
|
|
caughtReadable = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('exit', function() {
|
2013-03-31 08:16:52 +02:00
|
|
|
// we're testing what we think we are
|
|
|
|
assert(!_readCalled);
|
|
|
|
|
2013-03-27 06:43:53 +01:00
|
|
|
assert(caughtReadable);
|
|
|
|
console.log('ok 3');
|
|
|
|
});
|
|
|
|
})();
|