-
Notifications
You must be signed in to change notification settings - Fork 29.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: refactor test-fs-read-stream-inherit #10246
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
test: refactor test-fs-read-stream-inherit
Refactor to take advantage of block scoping to isolate tests. Checks in exit handlers now reside with the relevant test block. Where test cases start and end is more clear. Also: Some use of `common.mustCall()` and improved wrapping/indentation.
- Loading branch information
commit 378436135252b99bc120fbc0fb6c68c627cfe73c
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,174 +7,189 @@ const fs = require('fs'); | |
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; | ||
|
||
const file = fs.ReadStream(fn); | ||
{ | ||
const file = fs.ReadStream(fn); | ||
|
||
file.on('open', function(fd) { | ||
file.length = 0; | ||
callbacks.open++; | ||
assert.strictEqual(typeof fd, 'number'); | ||
assert.ok(file.readable); | ||
file.on('open', common.mustCall(function(fd) { | ||
file.length = 0; | ||
assert.strictEqual(typeof fd, 'number'); | ||
assert.ok(file.readable); | ||
|
||
// GH-535 | ||
file.pause(); | ||
file.resume(); | ||
file.pause(); | ||
file.resume(); | ||
}); | ||
// GH-535 | ||
file.pause(); | ||
file.resume(); | ||
file.pause(); | ||
file.resume(); | ||
})); | ||
|
||
file.on('data', function(data) { | ||
assert.ok(data instanceof Buffer); | ||
assert.ok(!paused); | ||
file.length += data.length; | ||
file.on('data', function(data) { | ||
assert.ok(data instanceof Buffer); | ||
assert.ok(!paused); | ||
file.length += data.length; | ||
|
||
paused = true; | ||
file.pause(); | ||
paused = true; | ||
file.pause(); | ||
|
||
setTimeout(function() { | ||
paused = false; | ||
file.resume(); | ||
}, 10); | ||
}); | ||
setTimeout(function() { | ||
paused = false; | ||
file.resume(); | ||
}, 10); | ||
}); | ||
|
||
|
||
file.on('end', function(chunk) { | ||
callbacks.end++; | ||
}); | ||
file.on('end', common.mustCall(function() {})); | ||
|
||
|
||
file.on('close', function() { | ||
callbacks.close++; | ||
}); | ||
file.on('close', common.mustCall(function() {})); | ||
|
||
const file3 = fs.createReadStream(fn, Object.create({encoding: 'utf8'})); | ||
file3.length = 0; | ||
file3.on('data', function(data) { | ||
assert.strictEqual(typeof data, 'string'); | ||
file3.length += data.length; | ||
process.on('exit', function() { | ||
assert.strictEqual(file.length, 30000); | ||
}); | ||
} | ||
|
||
for (let i = 0; i < data.length; i++) { | ||
// http://www.fileformat.info/info/unicode/char/2026/index.htm | ||
assert.strictEqual(data[i], '\u2026'); | ||
} | ||
}); | ||
|
||
file3.on('close', function() { | ||
callbacks.close++; | ||
}); | ||
|
||
process.on('exit', function() { | ||
assert.strictEqual(callbacks.open, 1); | ||
assert.strictEqual(callbacks.end, 1); | ||
assert.strictEqual(callbacks.close, 2); | ||
assert.strictEqual(file.length, 30000); | ||
assert.strictEqual(file3.length, 10000); | ||
console.error('ok'); | ||
}); | ||
|
||
const file4 = fs.createReadStream(rangeFile, Object.create({bufferSize: 1, | ||
start: 1, end: 2})); | ||
assert.strictEqual(file4.start, 1); | ||
assert.strictEqual(file4.end, 2); | ||
let contentRead = ''; | ||
file4.on('data', function(data) { | ||
contentRead += data.toString('utf-8'); | ||
}); | ||
file4.on('end', function(data) { | ||
assert.strictEqual(contentRead, 'yz'); | ||
}); | ||
|
||
const file5 = fs.createReadStream(rangeFile, Object.create({bufferSize: 1, | ||
start: 1})); | ||
assert.strictEqual(file5.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 file3 = fs.createReadStream(fn, Object.create({encoding: 'utf8'})); | ||
file3.length = 0; | ||
file3.on('data', function(data) { | ||
assert.strictEqual(typeof data, 'string'); | ||
file3.length += data.length; | ||
|
||
for (let i = 0; i < data.length; i++) { | ||
// http://www.fileformat.info/info/unicode/char/2026/index.htm | ||
assert.strictEqual(data[i], '\u2026'); | ||
} | ||
}); | ||
|
||
file3.on('close', common.mustCall(function() {})); | ||
|
||
process.on('exit', function() { | ||
assert.strictEqual(file3.length, 10000); | ||
}); | ||
} | ||
|
||
{ | ||
const options = Object.create({bufferSize: 1, start: 1, end: 2}); | ||
const file4 = fs.createReadStream(rangeFile, options); | ||
assert.strictEqual(file4.start, 1); | ||
assert.strictEqual(file4.end, 2); | ||
let contentRead = ''; | ||
file4.on('data', function(data) { | ||
contentRead += data.toString('utf-8'); | ||
}); | ||
file4.on('end', function() { | ||
assert.strictEqual(contentRead, 'yz'); | ||
}); | ||
} | ||
|
||
{ | ||
const options = Object.create({bufferSize: 1, start: 1}); | ||
const file5 = fs.createReadStream(rangeFile, options); | ||
assert.strictEqual(file5.start, 1); | ||
file5.data = ''; | ||
file5.on('data', function(data) { | ||
file5.data += data.toString('utf-8'); | ||
}); | ||
file5.on('end', function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should wrap callbacks with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added more |
||
assert.strictEqual(file5.data, 'yz\n'); | ||
}); | ||
} | ||
|
||
// https://github.com/joyent/node/issues/2320 | ||
const file6 = fs.createReadStream(rangeFile, Object.create({bufferSize: 1.23, | ||
start: 1})); | ||
assert.strictEqual(file6.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'); | ||
}); | ||
|
||
assert.throws(function() { | ||
fs.createReadStream(rangeFile, Object.create({start: 10, end: 2})); | ||
}, /"start" option must be <= "end" option/); | ||
|
||
const stream = fs.createReadStream(rangeFile, Object.create({ start: 0, | ||
end: 0 })); | ||
assert.strictEqual(stream.start, 0); | ||
assert.strictEqual(stream.end, 0); | ||
stream.data = ''; | ||
|
||
stream.on('data', function(chunk) { | ||
stream.data += chunk; | ||
}); | ||
|
||
stream.on('end', function() { | ||
assert.strictEqual(stream.data, 'x'); | ||
}); | ||
{ | ||
const options = Object.create({bufferSize: 1.23, start: 1}); | ||
const file6 = fs.createReadStream(rangeFile, options); | ||
assert.strictEqual(file6.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'); | ||
}); | ||
} | ||
|
||
// pause and then resume immediately. | ||
const pauseRes = fs.createReadStream(rangeFile); | ||
pauseRes.pause(); | ||
pauseRes.resume(); | ||
|
||
let file7 = fs.createReadStream(rangeFile, Object.create({autoClose: false })); | ||
assert.strictEqual(file7.autoClose, false); | ||
file7.on('data', function() {}); | ||
file7.on('end', function() { | ||
process.nextTick(function() { | ||
assert(!file7.closed); | ||
assert(!file7.destroyed); | ||
file7Next(); | ||
}); | ||
}); | ||
|
||
function file7Next() { | ||
// This will tell us if the fd is usable again or not. | ||
file7 = fs.createReadStream(null, Object.create({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'); | ||
{ | ||
assert.throws(function() { | ||
fs.createReadStream(rangeFile, Object.create({start: 10, end: 2})); | ||
}, /"start" option must be <= "end" option/); | ||
} | ||
|
||
{ | ||
const options = Object.create({start: 0, end: 0}); | ||
const stream = fs.createReadStream(rangeFile, options); | ||
assert.strictEqual(stream.start, 0); | ||
assert.strictEqual(stream.end, 0); | ||
stream.data = ''; | ||
|
||
stream.on('data', function(chunk) { | ||
stream.data += chunk; | ||
}); | ||
|
||
stream.on('end', function() { | ||
assert.strictEqual(stream.data, 'x'); | ||
}); | ||
} | ||
|
||
// Just to make sure autoClose won't close the stream because of error. | ||
const file8 = fs.createReadStream(null, Object.create({fd: 13337, | ||
autoClose: false })); | ||
file8.on('data', function() {}); | ||
file8.on('error', common.mustCall(function() {})); | ||
// pause and then resume immediately. | ||
{ | ||
const pauseRes = fs.createReadStream(rangeFile); | ||
pauseRes.pause(); | ||
pauseRes.resume(); | ||
} | ||
|
||
// Make sure stream is destroyed when file does not exist. | ||
const file9 = fs.createReadStream('/path/to/file/that/does/not/exist'); | ||
file9.on('data', function() {}); | ||
file9.on('error', common.mustCall(function() {})); | ||
{ | ||
let file7 = | ||
fs.createReadStream(rangeFile, Object.create({autoClose: false })); | ||
assert.strictEqual(file7.autoClose, false); | ||
file7.on('data', function() {}); | ||
file7.on('end', function() { | ||
process.nextTick(function() { | ||
assert(!file7.closed); | ||
assert(!file7.destroyed); | ||
file7Next(); | ||
}); | ||
}); | ||
|
||
process.on('exit', function() { | ||
assert(file7.closed); | ||
assert(file7.destroyed); | ||
function file7Next() { | ||
// This will tell us if the fd is usable again or not. | ||
file7 = fs.createReadStream(null, Object.create({fd: file7.fd, start: 0 })); | ||
file7.data = ''; | ||
file7.on('data', function(data) { | ||
file7.data += data; | ||
}); | ||
file7.on('end', function() { | ||
assert.strictEqual(file7.data, 'xyz\n'); | ||
}); | ||
} | ||
process.on('exit', function() { | ||
assert(file7.closed); | ||
assert(file7.destroyed); | ||
}); | ||
} | ||
|
||
assert(!file8.closed); | ||
assert(!file8.destroyed); | ||
assert(file8.fd); | ||
// Just to make sure autoClose won't close the stream because of error. | ||
{ | ||
const options = Object.create({fd: 13337, autoClose: false}); | ||
const file8 = fs.createReadStream(null, options); | ||
file8.on('data', function() {}); | ||
file8.on('error', common.mustCall(function() {})); | ||
process.on('exit', function() { | ||
assert(!file8.closed); | ||
assert(!file8.destroyed); | ||
assert(file8.fd); | ||
}); | ||
} | ||
|
||
assert(!file9.closed); | ||
assert(file9.destroyed); | ||
}); | ||
// Make sure stream is destroyed when file does not exist. | ||
{ | ||
const file9 = fs.createReadStream('/path/to/file/that/does/not/exist'); | ||
file9.on('data', function() {}); | ||
file9.on('error', common.mustCall(function() {})); | ||
|
||
process.on('exit', function() { | ||
assert(!file9.closed); | ||
assert(file9.destroyed); | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose this can be checked in
file.on('close')
like what's being done for file4.