Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
test: refactor test-stream-big-push
* use common.mustCall()
* specify setTimeout() duration of 1ms
* remove unused `n` function argument
  • Loading branch information
Trott committed Dec 11, 2016
commit c0ea26b4a20eea1f464a340b6a52c893f9b861af
25 changes: 7 additions & 18 deletions test/parallel/test-stream-big-push.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const stream = require('stream');
const str = 'asdfasdfasdfasdfasdf';
Expand All @@ -10,29 +10,25 @@ const r = new stream.Readable({
});

let reads = 0;
let eofed = false;
let ended = false;

r._read = function(n) {
function _read() {
if (reads === 0) {
setTimeout(function() {
r.push(str);
});
}, 1);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering, why are we doing these?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might just be me, but I find an unspecified duration or interval confusing. I always spend a moment wondering if it is a mistake. Did the original author simply forget to provide a value? Did they intend to use setImmediate() or even process.nextTick() instead of setTimeout()? And so on. By making it explicit, we at least make it clear that a 1ms timer is what was intended and not an error.

reads++;
} else if (reads === 1) {
var ret = r.push(str);
assert.strictEqual(ret, false);
reads++;
} else {
assert(!eofed);
eofed = true;
r.push(null);
}
};
}

r.on('end', function() {
ended = true;
});
r._read = common.mustCall(_read, 3);

r.on('end', common.mustCall(function() {}));

// push some data in to start.
// we've never gotten any read event at this point.
Expand All @@ -55,10 +51,3 @@ r.once('readable', function() {
chunk = r.read();
assert.strictEqual(chunk, null);
});

process.on('exit', function() {
assert(eofed);
assert(ended);
assert.strictEqual(reads, 2);
console.log('ok');
});