mirror of
https://github.com/nodejs/node.git
synced 2026-10-14 10:26:26 -04:00
test: refactor test-fs-read-stream
* alphabetize modules * replace callback invocation counts with common.mustCall() * add block-scoping * remove commented-out code that uses an identifier not in the test * move exit handlers to relevant blocks to keep tests cohesive * common.noop -> common.mustNotCall() * check results from `data` event PR-URL: https://github.com/nodejs/node/pull/13643 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
1 parent
64812f5728
commit
e74907b3cc
1 file changed
+161
-145
@@ -21,183 +21,199 @@
|
||||
|
||||
'use strict';
|
||||
const common = require('../common');
|
||||
const assert = require('assert');
|
||||
|
||||
const path = require('path');
|
||||
const assert = require('assert');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const fn = path.join(common.fixturesDir, 'elipses.txt');
|
||||
const rangeFile = path.join(common.fixturesDir, 'x.txt');
|
||||
|
||||
const callbacks = { open: 0, end: 0, close: 0 };
|
||||
{
|
||||
let paused = false;
|
||||
let bytesRead = 0;
|
||||
|
||||
let paused = false;
|
||||
let bytesRead = 0;
|
||||
const file = fs.ReadStream(fn);
|
||||
const fileSize = fs.statSync(fn).size;
|
||||
|
||||
const file = fs.ReadStream(fn);
|
||||
const fileSize = fs.statSync(fn).size;
|
||||
|
||||
assert.strictEqual(file.bytesRead, 0);
|
||||
|
||||
file.on('open', function(fd) {
|
||||
file.length = 0;
|
||||
callbacks.open++;
|
||||
assert.strictEqual('number', typeof fd);
|
||||
assert.strictEqual(file.bytesRead, 0);
|
||||
assert.ok(file.readable);
|
||||
|
||||
// GH-535
|
||||
file.pause();
|
||||
file.resume();
|
||||
file.pause();
|
||||
file.resume();
|
||||
});
|
||||
file.on('open', common.mustCall(function(fd) {
|
||||
file.length = 0;
|
||||
assert.strictEqual('number', typeof fd);
|
||||
assert.strictEqual(file.bytesRead, 0);
|
||||
assert.ok(file.readable);
|
||||
|
||||
file.on('data', function(data) {
|
||||
assert.ok(data instanceof Buffer);
|
||||
assert.ok(!paused);
|
||||
file.length += data.length;
|
||||
|
||||
bytesRead += data.length;
|
||||
assert.strictEqual(file.bytesRead, bytesRead);
|
||||
|
||||
paused = true;
|
||||
file.pause();
|
||||
|
||||
setTimeout(function() {
|
||||
paused = false;
|
||||
// GH-535
|
||||
file.pause();
|
||||
file.resume();
|
||||
}, 10);
|
||||
});
|
||||
file.pause();
|
||||
file.resume();
|
||||
}));
|
||||
|
||||
file.on('data', function(data) {
|
||||
assert.ok(data instanceof Buffer);
|
||||
assert.ok(!paused);
|
||||
file.length += data.length;
|
||||
|
||||
bytesRead += data.length;
|
||||
assert.strictEqual(file.bytesRead, bytesRead);
|
||||
|
||||
paused = true;
|
||||
file.pause();
|
||||
|
||||
setTimeout(function() {
|
||||
paused = false;
|
||||
file.resume();
|
||||
}, 10);
|
||||
});
|
||||
|
||||
|
||||
file.on('end', function(chunk) {
|
||||
assert.strictEqual(bytesRead, fileSize);
|
||||
assert.strictEqual(file.bytesRead, fileSize);
|
||||
callbacks.end++;
|
||||
});
|
||||
file.on('end', common.mustCall(function(chunk) {
|
||||
assert.strictEqual(bytesRead, fileSize);
|
||||
assert.strictEqual(file.bytesRead, fileSize);
|
||||
}));
|
||||
|
||||
|
||||
file.on('close', function() {
|
||||
assert.strictEqual(bytesRead, fileSize);
|
||||
assert.strictEqual(file.bytesRead, fileSize);
|
||||
callbacks.close++;
|
||||
file.on('close', common.mustCall(function() {
|
||||
assert.strictEqual(bytesRead, fileSize);
|
||||
assert.strictEqual(file.bytesRead, fileSize);
|
||||
}));
|
||||
|
||||
//assert.strictEqual(fs.readFileSync(fn), fileContent);
|
||||
});
|
||||
process.on('exit', function() {
|
||||
assert.strictEqual(file.length, 30000);
|
||||
});
|
||||
}
|
||||
|
||||
const file3 = fs.createReadStream(fn, {encoding: 'utf8'});
|
||||
file3.length = 0;
|
||||
file3.on('data', function(data) {
|
||||
assert.strictEqual('string', typeof data);
|
||||
file3.length += data.length;
|
||||
{
|
||||
const file = fs.createReadStream(fn, {encoding: 'utf8'});
|
||||
file.length = 0;
|
||||
file.on('data', function(data) {
|
||||
assert.strictEqual('string', typeof data);
|
||||
file.length += data.length;
|
||||
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
// http://www.fileformat.info/info/unicode/char/2026/index.htm
|
||||
assert.strictEqual('\u2026', data[i]);
|
||||
}
|
||||
});
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
// http://www.fileformat.info/info/unicode/char/2026/index.htm
|
||||
assert.strictEqual('\u2026', data[i]);
|
||||
}
|
||||
});
|
||||
|
||||
file3.on('close', function() {
|
||||
callbacks.close++;
|
||||
});
|
||||
file.on('close', common.mustCall());
|
||||
|
||||
process.on('exit', function() {
|
||||
assert.strictEqual(1, callbacks.open);
|
||||
assert.strictEqual(1, callbacks.end);
|
||||
assert.strictEqual(2, callbacks.close);
|
||||
assert.strictEqual(30000, file.length);
|
||||
assert.strictEqual(10000, file3.length);
|
||||
console.error('ok');
|
||||
});
|
||||
process.on('exit', function() {
|
||||
assert.strictEqual(file.length, 10000);
|
||||
});
|
||||
}
|
||||
|
||||
const file4 = fs.createReadStream(rangeFile, {bufferSize: 1, start: 1, end: 2});
|
||||
let contentRead = '';
|
||||
file4.on('data', function(data) {
|
||||
contentRead += data.toString('utf-8');
|
||||
});
|
||||
file4.on('end', function(data) {
|
||||
assert.strictEqual(contentRead, 'yz');
|
||||
});
|
||||
{
|
||||
const file =
|
||||
fs.createReadStream(rangeFile, {bufferSize: 1, start: 1, end: 2});
|
||||
let contentRead = '';
|
||||
file.on('data', function(data) {
|
||||
contentRead += data.toString('utf-8');
|
||||
});
|
||||
file.on('end', common.mustCall(function(data) {
|
||||
assert.strictEqual(contentRead, 'yz');
|
||||
}));
|
||||
}
|
||||
|
||||
const file5 = fs.createReadStream(rangeFile, {bufferSize: 1, start: 1});
|
||||
file5.data = '';
|
||||
file5.on('data', function(data) {
|
||||
file5.data += data.toString('utf-8');
|
||||
});
|
||||
file5.on('end', function() {
|
||||
assert.strictEqual(file5.data, 'yz\n');
|
||||
});
|
||||
{
|
||||
const file = fs.createReadStream(rangeFile, {bufferSize: 1, start: 1});
|
||||
file.data = '';
|
||||
file.on('data', function(data) {
|
||||
file.data += data.toString('utf-8');
|
||||
});
|
||||
file.on('end', common.mustCall(function() {
|
||||
assert.strictEqual(file.data, 'yz\n');
|
||||
}));
|
||||
}
|
||||
|
||||
// https://github.com/joyent/node/issues/2320
|
||||
const file6 = fs.createReadStream(rangeFile, {bufferSize: 1.23, start: 1});
|
||||
file6.data = '';
|
||||
file6.on('data', function(data) {
|
||||
file6.data += data.toString('utf-8');
|
||||
});
|
||||
file6.on('end', function() {
|
||||
assert.strictEqual(file6.data, 'yz\n');
|
||||
});
|
||||
{
|
||||
// Ref: https://github.com/nodejs/node-v0.x-archive/issues/2320
|
||||
const file = fs.createReadStream(rangeFile, {bufferSize: 1.23, start: 1});
|
||||
file.data = '';
|
||||
file.on('data', function(data) {
|
||||
file.data += data.toString('utf-8');
|
||||
});
|
||||
file.on('end', common.mustCall(function() {
|
||||
assert.strictEqual(file.data, 'yz\n');
|
||||
}));
|
||||
}
|
||||
|
||||
assert.throws(function() {
|
||||
fs.createReadStream(rangeFile, {start: 10, end: 2});
|
||||
}, /"start" option must be <= "end" option/);
|
||||
|
||||
const stream = fs.createReadStream(rangeFile, { start: 0, end: 0 });
|
||||
stream.data = '';
|
||||
{
|
||||
const stream = fs.createReadStream(rangeFile, { start: 0, end: 0 });
|
||||
stream.data = '';
|
||||
|
||||
stream.on('data', function(chunk) {
|
||||
stream.data += chunk;
|
||||
});
|
||||
|
||||
stream.on('end', function() {
|
||||
assert.strictEqual('x', stream.data);
|
||||
});
|
||||
|
||||
// pause and then resume immediately.
|
||||
const pauseRes = fs.createReadStream(rangeFile);
|
||||
pauseRes.pause();
|
||||
pauseRes.resume();
|
||||
|
||||
let file7 = fs.createReadStream(rangeFile, {autoClose: false });
|
||||
file7.on('data', common.noop);
|
||||
file7.on('end', function() {
|
||||
process.nextTick(function() {
|
||||
assert(!file7.closed);
|
||||
assert(!file7.destroyed);
|
||||
file7Next();
|
||||
stream.on('data', function(chunk) {
|
||||
stream.data += chunk;
|
||||
});
|
||||
});
|
||||
|
||||
function file7Next() {
|
||||
// This will tell us if the fd is usable again or not.
|
||||
file7 = fs.createReadStream(null, {fd: file7.fd, start: 0 });
|
||||
file7.data = '';
|
||||
file7.on('data', function(data) {
|
||||
file7.data += data;
|
||||
});
|
||||
file7.on('end', function(err) {
|
||||
assert.strictEqual(file7.data, 'xyz\n');
|
||||
stream.on('end', common.mustCall(function() {
|
||||
assert.strictEqual('x', stream.data);
|
||||
}));
|
||||
}
|
||||
|
||||
{
|
||||
// pause and then resume immediately.
|
||||
const pauseRes = fs.createReadStream(rangeFile);
|
||||
pauseRes.pause();
|
||||
pauseRes.resume();
|
||||
}
|
||||
|
||||
{
|
||||
let file = fs.createReadStream(rangeFile, {autoClose: false });
|
||||
let data = '';
|
||||
file.on('data', function(chunk) { data += chunk; });
|
||||
file.on('end', common.mustCall(function() {
|
||||
assert.strictEqual(data, 'xyz\n');
|
||||
process.nextTick(function() {
|
||||
assert(!file.closed);
|
||||
assert(!file.destroyed);
|
||||
fileNext();
|
||||
});
|
||||
}));
|
||||
|
||||
function fileNext() {
|
||||
// This will tell us if the fd is usable again or not.
|
||||
file = fs.createReadStream(null, {fd: file.fd, start: 0 });
|
||||
file.data = '';
|
||||
file.on('data', function(data) {
|
||||
file.data += data;
|
||||
});
|
||||
file.on('end', common.mustCall(function(err) {
|
||||
assert.strictEqual(file.data, 'xyz\n');
|
||||
}));
|
||||
process.on('exit', function() {
|
||||
assert(file.closed);
|
||||
assert(file.destroyed);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// Just to make sure autoClose won't close the stream because of error.
|
||||
const file = fs.createReadStream(null, {fd: 13337, autoClose: false });
|
||||
file.on('data', common.mustNotCall());
|
||||
file.on('error', common.mustCall());
|
||||
process.on('exit', function() {
|
||||
assert(!file.closed);
|
||||
assert(!file.destroyed);
|
||||
assert(file.fd);
|
||||
});
|
||||
}
|
||||
|
||||
// Just to make sure autoClose won't close the stream because of error.
|
||||
const file8 = fs.createReadStream(null, {fd: 13337, autoClose: false });
|
||||
file8.on('data', common.noop);
|
||||
file8.on('error', common.mustCall());
|
||||
{
|
||||
// Make sure stream is destroyed when file does not exist.
|
||||
const file = fs.createReadStream('/path/to/file/that/does/not/exist');
|
||||
file.on('data', common.mustNotCall());
|
||||
file.on('error', common.mustCall());
|
||||
|
||||
// Make sure stream is destroyed when file does not exist.
|
||||
const file9 = fs.createReadStream('/path/to/file/that/does/not/exist');
|
||||
file9.on('data', common.noop);
|
||||
file9.on('error', common.mustCall());
|
||||
|
||||
process.on('exit', function() {
|
||||
assert(file7.closed);
|
||||
assert(file7.destroyed);
|
||||
|
||||
assert(!file8.closed);
|
||||
assert(!file8.destroyed);
|
||||
assert(file8.fd);
|
||||
|
||||
assert(!file9.closed);
|
||||
assert(file9.destroyed);
|
||||
});
|
||||
process.on('exit', function() {
|
||||
assert(!file.closed);
|
||||
assert(file.destroyed);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user