node/test/parallel/test-stream2-large-read-stall.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

55 lines
1.2 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
// If everything aligns so that you do a read(n) of exactly the
// remaining buffer, then make sure that 'end' still emits.
var READSIZE = 100;
var PUSHSIZE = 20;
var PUSHCOUNT = 1000;
var HWM = 50;
const Readable = require('stream').Readable;
var r = new Readable({
highWaterMark: HWM
});
var rs = r._readableState;
r._read = push;
r.on('readable', function() {
console.error('>> readable');
do {
console.error(' > read(%d)', READSIZE);
var ret = r.read(READSIZE);
console.error(' < %j (%d remain)', ret && ret.length, rs.length);
} while (ret && ret.length === READSIZE);
console.error('<< after read()',
ret && ret.length,
rs.needReadable,
rs.length);
});
r.on('end', common.mustCall(function() {}));
var pushes = 0;
function push() {
if (pushes > PUSHCOUNT)
return;
if (pushes++ === PUSHCOUNT) {
console.error(' push(EOF)');
return r.push(null);
}
console.error(' push #%d', pushes);
if (r.push(Buffer.allocUnsafe(PUSHSIZE)))
setTimeout(push, 1);
}
process.on('exit', function() {
assert.equal(pushes, PUSHCOUNT + 1);
});