node/test/parallel/test-stream2-readable-non-empty-end.js
Rich Trott 6263c00828 test: provide duration/interval to timers
There are places in the code base where setTimeout() or
setInterval() are called with just a callback and no duration/interval.
The timers module will use a value of `1` in that situation.

An unspecified duration or interval can be confusing. Did the original
author forget to provide a value? Did they intend to use setImmediate()
or process.nextTick() instead of setTimeout()? And so on.

This change provides a duration or interval of `1` to all calls in the
codebase where it is missing. `parallel/test-timers.js` still tests the
situation where `setTimeout()` and `setInterval()` are called with
`undefined` and other non-numeric values for the duration/interval.

PR-URL: https://github.com/nodejs/node/pull/9472
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2017-01-06 15:22:51 -08:00

52 lines
1.1 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const Readable = require('_stream_readable');
var len = 0;
var chunks = new Array(10);
for (var i = 1; i <= 10; i++) {
chunks[i - 1] = Buffer.allocUnsafe(i);
len += i;
}
var test = new Readable();
var n = 0;
test._read = function(size) {
var chunk = chunks[n++];
setTimeout(function() {
test.push(chunk === undefined ? null : chunk);
}, 1);
};
test.on('end', thrower);
function thrower() {
throw new Error('this should not happen!');
}
var bytesread = 0;
test.on('readable', function() {
var b = len - bytesread - 1;
var res = test.read(b);
if (res) {
bytesread += res.length;
console.error('br=%d len=%d', bytesread, len);
setTimeout(next, 1);
}
test.read(0);
});
test.read(0);
function next() {
// now let's make 'end' happen
test.removeListener('end', thrower);
test.on('end', common.mustCall(function() {}));
// one to get the last byte
var r = test.read();
assert(r);
assert.equal(r.length, 1);
r = test.read();
assert.equal(r, null);
}